Rasmus Larsen
Rasmus Larsen

Reputation: 6117

Put/print mean of a variable to log in SAS

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;

Upvotes: 1

Views: 2979

Answers (3)

whymath
whymath

Reputation: 1394

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;

Upvotes: 0

cherry_bueno
cherry_bueno

Reputation: 208

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;

Upvotes: 2

DomPazz
DomPazz

Reputation: 12465

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;

Upvotes: 1

Related Questions