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

how can i create loop in SAS using iterative method and print the results

我想在 SAS 中创建一个迭代循环来计算每个 id 我的数据的平均值,如下所示:

data sample

在 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;
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;

您也可以将 var(mean) 替换为 mean(value)= my_mean