在SAS proc sql中进行递增操作

huangapple go评论92阅读模式
英文:

Incrementing inside SAS proc sql

问题

如何逐步定义我的sum函数的参数,而不必写出它们的全部内容?

英文:

I want to create a summary by group (represented by reporting_date) and it has repeating arguments and there should be a better way to do it than shown below:

  1. proc sql;
  2. create table summary_cmr as
  3. select distinct reporting_date, sum(cmr1) as bads_1, sum(cmr2) as bads_2, sum(cmr3) as bads_3, sum(cmr4) as bads_4
  4. from have
  5. group by 1;
  6. quit;

How can I incrementally define the arguments of my sum function, without having to write all of them?

答案1

得分: 3

只使用SAS代码而不是SQL代码。然后,您可以使用变量列表。

  1. proc summary data=have nway;
  2. class reporting_date;
  3. output out=summary_cmr sum(cmr1-cmr4)=bads_1-bads_4 ;
  4. run;
英文:

Just use SAS code instead of SQL code. Then you can use variable lists.

  1. proc summary data=have nway;
  2. class reporting_date;
  3. output out=summary_cmr sum(cmr1-cmr4)=bads_1-bads_4 ;
  4. run;

答案2

得分: 1

%宏 SUMN(N) ;
%本地 I ;
%DO I = 1 %TO &N ;
sum(cmr&I) as bads_&I,
%END ;
sum(0) as _EOL
%MEND ;

proc sql ;
create table summary_cmr (drop=_EOL) as
select reporting_date, %SUMN(10)
from have
group by 1 ;
quit ;

英文:

One way would be to define them using a macro loop, and call the macro in PROC SQL

<pre>
%MACRO SUMN(N) ;
%LOCAL I ;
%DO I = 1 %TO &N ;
sum(cmr&I) as bads_&I,
%END ;
sum(0) as _EOL
%MEND ;

proc sql ;
create table summary_cmr (drop=_EOL) as
select reporting_date, %SUMN(10)
from have
group by 1 ;
quit ;
</pre>

答案3

得分: 1

你可以使用PROC MEANS来自动命名变量:

  1. proc means data=have missing noprint;
  2. class reporting_date;
  3. var cmr1-cmr3;
  4. output out=summary_cmr(where=(_TYPE_ &gt; 0) )
  5. sum= / autoname;
  6. run;

变量将如下所示:

  1. cmr1_Sum cmr2_Sum cmr3_Sum
  2. ... ... ...
英文:

You can use PROC MEANS to autoname variables:

  1. proc means data=have missing noprint;
  2. class reporting_date;
  3. var cmr1-cmr3;
  4. output out=summary_cmr(where=(_TYPE_ &gt; 0) )
  5. sum= / autoname;
  6. run;

The variables will look as follows:

  1. cmr1_Sum cmr2_Sum cmr3_Sum
  2. ... ... ...

huangapple
  • 本文由 发表于 2023年3月31日 15:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定