- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I am trying to write a do loop outside a data step. For example, assume my data has 15 variables named var1, var2, ...var15. My main question is how to feed "i" into the macro.
%macro m1 (var_use);
proc freq data=data1;
tables var&var_use;
*other commands;
run;
%mend m1;
do i = 1 to 15;
%m1 (&i);
end;
run;
Thanks,
Brent Fulton
UC Berkeley
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use %DO to loop in macro language.
Say you want the macro to loop over the variables specified in the input parameter.
%macro m1 (varlist);
%local i next ;
%let i=1;
%do i=1 %to %sysfunc(countw(&varlist));
%let next=%scan(&varlist,&i);
proc freq data=data1;
tables &next;
run;
*other commands;
%end;
%mend m1;
%m1(age sex race);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Why don't you just list all VARn variables on the TABLES statement...
TABLES VAR1-VAR15;
or perhaps.
TABLES VAR:;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I could do that, but my macro is more complex. Is there any way to feed "i" into the macro?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use %DO to loop in macro language.
Say you want the macro to loop over the variables specified in the input parameter.
%macro m1 (varlist);
%local i next ;
%let i=1;
%do i=1 %to %sysfunc(countw(&varlist));
%let next=%scan(&varlist,&i);
proc freq data=data1;
tables &next;
run;
*other commands;
%end;
%mend m1;
%m1(age sex race);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you Tom this was helpful, because it could be applied to variables in general...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You could use call execute to call your macro. E.g.:
*create some test data;
data data1;
set sashelp.class (rename=(
sex=var1
age=var2
height=var3
weight=var4));
run;
%macro m1 (var_use);
proc freq data=data1;
tables var&var_use;
*other commands;
run;
%mend m1;
data _null_;
do i = 1 to 4;
x = cats( '%m1(',i,')' ) ;
call execute ( x ) ;
end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you Art, your answer is correct and helpful as well, but Sas only allows one correct answer...
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Brent, Tom was clearly first, deserved the correct answer tag, and I have more than enough points as it is. I was simply offering a way to accomplish the task of passing values from one's code to a macro as that question gets asked repeatedly. Regardless, thanks for the recognition.