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

Good morning!

 

I have a question about using macro in sas. I want to check the log-log plot for many variables(more than 10). The program is the same for each of them. So instead of have 10 same repeated programs. I want to use macro, but I don't knew how to use extract the elemnt in macro variable one by one. My code is:

 

%let plot= age sex hight;

 

%macro plot;

%do i=1 %to 10;

proc lifetest data=modeldata plot=(lls) ;

time followup*pass(0);

strata &plot[i];

ods select 'LOGLOGS Plot';

run;

%end;

%mend plot;

 

But it doesn't work. I feel the problem is wirh strata &plot[i];

Can you help me fix this code? Or come up with a better way to do so?

 

Thank you very much!!

 

Best wishes,

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Macro variables are just strings, not arrays. So use some simple string processing functions.

 

%macro plot(varlist);
%local i;
%do i=1 %to %sysfunc(countw(&varlist));
proc lifetest data=modeldata plot=(lls) ;
time followup*pass(0);
strata %scan(&varlist,&i) ;
ods select 'LOGLOGS Plot';
run;
%end;
%mend plot;

%plot(age sex hight);
 

View solution in original post

3 REPLIES 3
Astounding
PROC Star

You'll have to show us what the program would look like without macro language.  You only have to show the program for 1 variable, but it has to be working code.  If the code you show doesn't work for 1 variable, macro language would only generate nonworking code for 10 variables ... not a useful result.

Tom
Super User Tom
Super User

Macro variables are just strings, not arrays. So use some simple string processing functions.

 

%macro plot(varlist);
%local i;
%do i=1 %to %sysfunc(countw(&varlist));
proc lifetest data=modeldata plot=(lls) ;
time followup*pass(0);
strata %scan(&varlist,&i) ;
ods select 'LOGLOGS Plot';
run;
%end;
%mend plot;

%plot(age sex hight);
 
Xiaoningdemao
Quartz | Level 8
Dear Tom,

Thank you very much!!!
This is exactly what I want!!!

Best wishes.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1963 views
  • 2 likes
  • 3 in conversation