SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1072|回复: 0
打印 上一主题 下一主题

Make a frequency function in SAS/IML

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2012-3-16 13:49:44 | 只看该作者

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"/>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2025-5-6 20:51 , Processed in 0.137741 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表