【问题标题】:Looping around a whole data step in SAS在 SAS 中循环整个数据步骤
【发布时间】:2019-03-19 23:24:47
【问题描述】:

所以我有以下代码:

%let macroVar = Var1 Var2;

Data new1;
Set old1 (keep= count &macroVar.);
Run;

Proc means data = new1 nway missing noprint;
Class var1;
Var count;
Output out= out_var1 sum=;

Proc means data = new1 nway missing noprint;
Class var2;
Var count;
Output out= out_var2 sum=;

如何使用我一开始设置的宏变量在一个数据步骤中写出两个 proc 方法?

非常感谢

【问题讨论】:

  • 您是否尝试过创建一个宏来循环遍历变量列表?

标签: sas


【解决方案1】:

为什么不在一个 PROC MEANS 调用中完成呢?

%let varlist=sex age ;
proc means data=sashelp.class missing noprint;
  class &varlist;
  ways 1;
  var height;
  output out=want sum=;
run;

结果:

Obs    Sex    Age    _TYPE_    _FREQ_    Height

 1             11       1         2       108.8
 2             12       1         5       297.2
 3             13       1         3       184.3
 4             14       1         4       259.6
 5             15       1         4       262.5
 6             16       1         1        72.0
 7      F       .       2         9       545.3
 8      M       .       2        10       639.1

如果你真的必须有单独的输出数据集,那么从上面的输出生成它们会更快。

%macro sum_counts(varlist,data=,var=count);
%local i n;
%let n=%sysfunc(countw(&varlist));

proc means data=&data missing noprint ;
  class &varlist;
  ways 1;
  var &var;
  output out=_summary_ sum=;
run;

%do i=1 %to &n;
  data new&i;
    set _summary_;
    where _type_=2**(&n-&i);
    keep %scan(&varlist,&i) &var;
  run;
%end;

%mend sum_counts;

例子:

263   options mprint;
264   %sum_counts(varlist=sex age,data=sashelp.class,var=height);
MPRINT(SUM_COUNTS):   proc means data=sashelp.class missing noprint ;
MPRINT(SUM_COUNTS):   class sex age;
MPRINT(SUM_COUNTS):   ways 1;
MPRINT(SUM_COUNTS):   var height;
MPRINT(SUM_COUNTS):   output out=_summary_ sum=;
MPRINT(SUM_COUNTS):   run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK._SUMMARY_ has 8 observations and 5 variables.

MPRINT(SUM_COUNTS):   data new1;
MPRINT(SUM_COUNTS):   set _summary_;
MPRINT(SUM_COUNTS):   where _type_=2**(2-1);
MPRINT(SUM_COUNTS):   keep sex height;
MPRINT(SUM_COUNTS):   run;

NOTE: There were 2 observations read from the data set WORK._SUMMARY_.
      WHERE _type_=2;
NOTE: The data set WORK.NEW1 has 2 observations and 2 variables.

MPRINT(SUM_COUNTS):   data new2;
MPRINT(SUM_COUNTS):   set _summary_;
MPRINT(SUM_COUNTS):   where _type_=2**(2-2);
MPRINT(SUM_COUNTS):   keep age height;
MPRINT(SUM_COUNTS):   run;

NOTE: There were 6 observations read from the data set WORK._SUMMARY_.
      WHERE _type_=1;
NOTE: The data set WORK.NEW2 has 6 observations and 2 variables.

【讨论】:

  • 我知道这在获取数据方面会起作用,但是它使我的下一个分析变得更加困难。如果我可以将它们创建为两个单独的文件,那么整个过程的效率会更高。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多