BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MaryA_Marion
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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.

rogerjdeangelis
Barite | Level 11
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

rogerjdeangelis
Barite | Level 11
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.
Reeza
Super User

@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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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