DATA Step, Macro, Functions and more

do loop outside data step

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

do loop outside data step

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
Solution
‎10-13-2011 09:28 AM
Super User
Super User
Posts: 6,501

Re: do loop outside data step

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

View solution in original post


All Replies
Respected Advisor
Posts: 3,777

do loop outside data step

Why don't you just list all VARn variables on the TABLES statement...

TABLES VAR1-VAR15;

or perhaps.

TABLES VAR:;

Occasional Contributor
Posts: 14

do loop outside data step

I could do that, but my macro is more complex. Is there any way to feed "i" into the macro?

Solution
‎10-13-2011 09:28 AM
Super User
Super User
Posts: 6,501

Re: do loop outside data step

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

Occasional Contributor
Posts: 14

Re: do loop outside data step

Thank you Tom this was helpful, because it could be applied to variables in general...

PROC Star
Posts: 7,363

Re: do loop outside data step

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;

Occasional Contributor
Posts: 14

Re: do loop outside data step

Thank you Art, your answer is correct and helpful as well, but Sas only allows one correct answer...

PROC Star
Posts: 7,363

Re: do loop outside data step

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.

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 4499 views
  • 6 likes
  • 4 in conversation