如何使用迭代方法在SAS中创建循环并打印结果

时间:2017-10-04 11:53:13

标签: sas ssas sas-macro

我想在SAS中进行迭代循环,以计算每个id我的数据的平均值如下:

data sample

2 个答案:

答案 0 :(得分:0)

在SAS数据步骤中,您使用do循环进行迭代处理https://blogs.sas.com/content/iml/2011/09/07/loops-in-sas.html

但是,为了获得每个想法的平均值,SQL通常要简单得多。

proc sql;
  select id, mean(value)
  from data
  group by id
  ;
quit;

答案 1 :(得分:0)

data begin;
    input id value;
    cards;
    1 10 
    1 20 
    1 5 
    2 9
    2 6
    3 50
    3 20 
    3 44
    3 79
; run;

proc sort data=begin; by id; run; /*This is not needed in our case, but it is good practice to always sort in SAS.*/

proc means data=begin; 
    by id;  /*These are variables you count by*/
    var value; /*The value you count the mean by. You also get additional values. */
run;

您也可以用 mean(value)= my_mean

替换var(mean)