Making the PROC MEANS statement in SAS produce a variable instead of a dataset

醉酒当歌 提交于 2019-12-25 10:01:57

问题


I need to obtain the sum of all the values in a column ("var1" in the code below). As far as I could determine, this is done as follows:

proc means data = somedata sum var1;
output out = sumtable sum = sum;
run; 

The sum I want to use as a variable in the next step. Is it possible to have the OUTPUT statement above store the sum in a new variable instead of writing it to a whole new dataset? If so, what is the syntax for this?


回答1:


In a word, no. You could store the value as a text string into a macro variable as Reeza suggests, but if you want to store it as a variable then the variable needs to be in a dataset.

It is not hard to bring that variable back into a future data step if you want to use it for something. Just reference the dataset where it is stored.

proc summary data=sashelp.class ;
  var height weight ;
  output out=class_summary sum=total_height total_weight;
run;
data new ;
  set sashelp.class;
  if _n_=1 then set class_summary;
  fraction_of_total_wt = weight / total_weight;
  fraction_of_total_ht = height / total_height;
run;



回答2:


It sounds like you want a macro variable instead of a data step. In my opinion this is most easily done via a PROC SQL step instead of PROC MEANS. Your PROC MEANS does't look correct as well, VAR1 doesn't belong there and would produce an error.

proc sql;
select sum(var1) into :sum_var1
from somedata;
quit;

%put &sum_var1;

You can access the variable in other portions of your code using the &sum_var1 which will resolve to the variable value. It's worth noting that all macro variables are stored as text.



来源:https://stackoverflow.com/questions/33578100/making-the-proc-means-statement-in-sas-produce-a-variable-instead-of-a-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!