问题
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