BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dustychair
Pyrite | Level 9

Hi all,

I think I have a problem with "set" statement when it is in a macro. I have a macro as below and it calculates for only the last statement (speaking). I don't know what I am missing.


%macro gain(g);
data a501_403_v1;
set a501_403;
&g._ss_gain=&g._ss_501-&g._ss_403;
&g._pl_gain=&g._pl_501-&g._pl_403;
run;

%mend;
%gain (listening);
%gain (reading);
%gain (writing);
%gain (speaking);

 

Thank you

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your logic is messed up.  You are re-creating the same output dataset each time you call the macro, that is why only the last one still exists.

 

Your problem doesn't really look like something that needs a macro , but if you did want to use a macro you need to have it only generate the two assignment statements.  Then you can call it four times in the middle of a single data step.

%macro gain(g);
&g._ss_gain=&g._ss_501-&g._ss_403;
&g._pl_gain=&g._pl_501-&g._pl_403;
%mend;

data a501_403_v1;
  set a501_403; 
%gain (listening);
%gain (reading);
%gain (writing);
%gain (speaking);
run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Your logic is messed up.  You are re-creating the same output dataset each time you call the macro, that is why only the last one still exists.

 

Your problem doesn't really look like something that needs a macro , but if you did want to use a macro you need to have it only generate the two assignment statements.  Then you can call it four times in the middle of a single data step.

%macro gain(g);
&g._ss_gain=&g._ss_501-&g._ss_403;
&g._pl_gain=&g._pl_501-&g._pl_403;
%mend;

data a501_403_v1;
  set a501_403; 
%gain (listening);
%gain (reading);
%gain (writing);
%gain (speaking);
run;
dustychair
Pyrite | Level 9
Great! Thank you so much!
ballardw
Super User

It might be a good idea to post some example data and what you expect the result to be.

 

One suspects that a macro is not needed and likely an array solution might work

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 672 views
  • 0 likes
  • 3 in conversation