How to evaluate a macro variable after variable was created in proc summary?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

How to evaluate a macro variable after variable was created in proc summary?

How to evaluate a macro variable after variable was created in proc summary?  SEE BELOW

 

SAS CODE

 

data summaryOut; set summaryOut;
file print;
put meanY; run;

 

meanY is in summaryOut dataset. I want a function to evaluate meanY (=23083).
data _null_; set summaryOut;
%let meanY=%SYSFUNC(meanY);
%put &meanY;
*meanY=23083.164953;
run; quit;

 

87 data _null_; set summaryOut;
88 %let meanY=%EVAVL(meanY);
ERROR: Expected open parenthesis after macro function name not found.
89 %put &meanY;
SYMBOLGEN: Macro variable MEANY resolves to
91 run;


Accepted Solutions
Solution
‎03-19-2017 10:22 PM
Super User
Posts: 5,353

Re: How to evaluate a macro variable after variable was created in proc summary?

So far, you don't have a macro variable holding any meaningful information.  PROC SUMMARY does not create macro variables. 

 

Your first DATA step shows the value of a DATA step variable MEANY.  If you want to get that value into a macro variable, you could use:

 

data _null_;

set summaryOut;

call symputx('meany', meany);

run;

%put &meanY;

 

That gives you a macro variable holding the contents of the DATA step variable.

View solution in original post


All Replies
Solution
‎03-19-2017 10:22 PM
Super User
Posts: 5,353

Re: How to evaluate a macro variable after variable was created in proc summary?

So far, you don't have a macro variable holding any meaningful information.  PROC SUMMARY does not create macro variables. 

 

Your first DATA step shows the value of a DATA step variable MEANY.  If you want to get that value into a macro variable, you could use:

 

data _null_;

set summaryOut;

call symputx('meany', meany);

run;

%put &meanY;

 

That gives you a macro variable holding the contents of the DATA step variable.

Valued Guide
Posts: 505

Re: How to evaluate a macro variable after variable was created in proc summary?

How to evaluate a macro variable after variable was created in proc summary?

inspired by
https://goo.gl/hPzyHa
https://communities.sas.com/t5/General-SAS-Programming/How-to-evaluate-a-macro-variable-after-variable-was-created-in/m-p/342401


HAVE
====

Up to 40 obs WORK.CLASS total obs=5

Obs     Y

 1     14
 2     13
 3     13
 4     14
 5     14

WANT ( Something like this )
============================

  data _null_;
   set summaryout
   meany = &meany;
   put " then mean of y is " meany;
 run;quit;

FULL SOLUTION
=============

data class;
 set sashelp.class(keep=age obs=5 rename=age=y);
run;quit;

proc summary data=class;
var y;
output out=summaryout(keep=meany) mean=meanY;
run;quit;

Up to 40 obs from summaryout total obs=1

Obs     MEANY

 1      13.6


%symdel mac_meany;
data _null_;
 * create macro variable from summaryout;
 if _n_=0 then do;
     %let rc=%sysfunc(dosubl('
        data _null_;
          set summaryout;
          call symputx("mac_meany",put(meany,best.),"G");
        run;quit;
    '));
    %put &=mac_meany;
  end;

  meany = &mac_meany;

  put " The mean of y is " meany;

run;quit;

The mean of y is 13.6

Valued Guide
Posts: 505

Re: How to evaluate a macro variable after variable was created in proc summary?

My solution in creates a variable meany rom a macro variable that can be used in the datastep.

You could use the meany to compare against the individual Ys.
Super User
Posts: 19,023

Re: How to evaluate a macro variable after variable was created in proc summary?


MaryA_Marion wrote:

How to evaluate a macro variable after variable was created in proc summary?  SEE BELOW

 

SAS CODE

 

data summaryOut; set summaryOut;
file print;
put meanY; run;

 

meanY is in summaryOut dataset. I want a function to evaluate meanY (=23083).
data _null_; set summaryOut;
%let meanY=%SYSFUNC(meanY);
%put &meanY;
*meanY=23083.164953;
run; quit;

 

87 data _null_; set summaryOut;
88 %let meanY=%EVAVL(meanY);
ERROR: Expected open parenthesis after macro function name not found.
89 %put &meanY;
SYMBOLGEN: Macro variable MEANY resolves to
91 run;


Unfortunately your code doesn't make sense. 

 

It looks like you're trying to create a macro variable for a mean and then use it in another step. Rather than a macro, a merge works as well, depending on usage of course.

 

*Add average value to a dataset;
*Solution 1 - PROC MEANS + Data step;

proc means data=sashelp.class noprint;
    output out=avg_values mean(height)=avg_height;
run;

data class;
    set sashelp.class;

    if _n_=1 then
        set avg_values;
run;

proc print data=class;
run;

*Solution 2 - PROC SQL - note the warning in the log;
PROC SQL;
Create table want as
select *, mean(height) as avg_height
from sashelp.class;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 260 views
  • 3 likes
  • 4 in conversation