简体   繁体   English

在SAS中使PROC MEANS语句产生变量而不是数据集

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

I need to obtain the sum of all the values in a column ("var1" in the code below). 我需要获取列中所有值的总和(以下代码中的“ var1”)。 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? 是否可以使上面的OUTPUT语句将总和存储在新变量中,而不是将其写入整个新数据集中? If so, what is the syntax for this? 如果是这样,其语法是什么?

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. 在我看来,这很容易通过PROC SQL步骤而不是PROC MEANS来完成。 Your PROC MEANS does't look correct as well, VAR1 doesn't belong there and would produce an error. 您的PROC MEANS看起来也不正确,VAR1不属于那里,并且会产生错误。

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. 您可以使用&sum_var1访问代码中其他部分的变量,该变量将解析为变量值。 It's worth noting that all macro variables are stored as text. 值得注意的是,所有宏变量都存储为文本。

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. 您可以按照Reeza的建议将值作为文本字符串存储到宏变量中,但是如果要将其存储为变量,则该变量必须位于数据集中。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM