SAS中文论坛

标题: Make a frequency function in SAS/IML [打印本页]

作者: shiyiming    时间: 2012-3-16 13:49
标题: Make a frequency function in SAS/IML
From Dapangmao's blog on sas-analysis

<div class="separator" style="clear: both; text-align: center;"><a href="http://4.bp.blogspot.com/-Q5S7WVe5jKQ/T2KrbFcvUwI/AAAAAAAAA-Y/7Nhjjv23_jo/s1600/SGPlot.png" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="300" width="400" src="http://4.bp.blogspot.com/-Q5S7WVe5jKQ/T2KrbFcvUwI/AAAAAAAAA-Y/7Nhjjv23_jo/s400/SGPlot.png" /></a></div><br />
Aggregation is probably the most popular operation in the data world. R comes with a handy table() function. Usually in SAS, the FREQ procedure would deal with this job. It will be great if SAS/IML has an equivalent function. I just created a user-defined function or module for such a purpose. Since it contains a DO loop, the efficiency is not very ideal -- always 10 times slower than PROC FREQ for a simulated data set of one million records. <br />
<pre style="background-color: #ebebeb; border: 1px dashed rgb(153, 153, 153); color: #000001; font-size: 14px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"><code>
/* 1 - Use IML for simulation and aggregation */
proc iml;
   start freq(invec);
      x = t(unique(invec));
      y = repeat(x, 1, 2);
      do i = 1 to nrow(x);
         y[i, 2] = ncol(loc(invec=y[i, 1]));
      end;
      return(y);
   finish;
   store module = freq;
quit;

proc iml;
   load module = freq;
   /* Simulate a vector with 1 million values */
   test = abs(floor(rannor(1:1e6)*100));
   t0 = time();
      result = freq(test);
   timer = time() - t0;
   print timer;   
   /* Output the test vector as SAS data set */
   create a var{"level" "frequency"};
      append from result;
   close a;
quit;

proc sgplot data = a;
   series x = level y = frequency;
run;

/* 2 - Use PROC FREQ for simulation and aggregation */
data test;
   do i = 1 to 1e6;
      test = abs(floor(rannor(1234)*100));
      output;
   end;
   drop i;
run;

options fullstimer;
proc freq data = test noprint;
   table test / out = b;
run;
</code></pre><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3256159328630041416-7881952112999171992?l=www.sasanalysis.com' alt='' /></div><img src="http://feeds.feedburner.com/~r/SasAnalysis/~4/ZKsrU2VLJZE" height="1" width="1"/>




欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/) Powered by Discuz! X3.2