Put/print mean of a variable to log in SAS

眉间皱痕 提交于 2020-06-17 03:55:10

问题


How do i print the mean of a variable to the log in SAS?

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

This is what I've tried:

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

回答1:


You can use 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;

If you are OK writing the value to the open output location then just use:

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



回答2:


You can use the MEANS procedure to calculate the mean of the pounds variable, followed by a CALL SYMPUT routine to assign the value to a macro variable, and finally a %PUT to print it in the log.

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;



回答3:


In data step, you can use putlog statement to print the value on log. Based on @cherry_bueno 's answer, the putlog version is:

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;


来源:https://stackoverflow.com/questions/34790611/put-print-mean-of-a-variable-to-log-in-sas

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