放置/打印变量的平均值以登录SAS

时间:2016-01-14 13:16:20

标签: sas

如何将变量的平均值打印到SAS中的日志?

data fruit;
input zip fruit & $22. pounds;
datalines;
10034 apples, grapes kiwi  123456
92626  oranges  97654
25414 pears apple      987654
;

这是我尝试过的:

data _null_;
   set fruit;
   put mean(zip);
run;

我通常在R中编程,我会写

mean(fruit$zip)

很难在SAS中使用简单的东西。

3 个答案:

答案 0 :(得分:2)

您可以使用MEANS程序计算磅变量的平均值,然后使用CALL SYMPUT例程将值分配给宏变量,最后使用%PUT打印它在日志中。

proc means data=fruit;
  var pounds;
  output out=testmean mean=fruit_mean;
run;

data _null_;
  set testmean;
  call symput("fruit_avg",fruit_mean);
run;

%put mean of x is &fruit_avg;

答案 1 :(得分:1)

您可以使用PROC SQL。

proc sql noprint; 
/*noprint as you don't want to print to the defined output location, just the log*/

/*format, formats the value.  into :<> puts the value into a macro variable named <>*/
select mean(zip) format=best32.
   into :zip_mean
   from fruit;

/*%put writes a message to the log*/
%put Mean of Zip: &zip_mean;
quit;

如果您可以将值写入打开的输出位置,那么只需使用:

proc sql;
select mean(zip) format=best32.
   from fruit;
quit;

答案 2 :(得分:0)

在数据步骤中,可以使用putlog语句在日志上打印该值。根据@cherry_bueno的答案,putlog版本为:

proc means data=fruit;
  var pounds;
  output out=testmean mean=fruit_mean;
run;

data _null_;
  set testmean;
  putlog 'mean of x is ' fruit_mean;
run;