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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

7 REPLIES 7
data_null__
Jade | Level 19

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

TABLES VAR1-VAR15;

or perhaps.

TABLES VAR:;

BrentFulton
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

BrentFulton
Calcite | Level 5

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

art297
Opal | Level 21

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;

BrentFulton
Calcite | Level 5

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

art297
Opal | Level 21

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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