BookmarkSubscribeRSS Feed
InjuryEpi
Calcite | Level 5

I have a program with a macro that allows me to run the whole program for one chosen variable within the data set (i.e., %let choice=var1).  Then I run  the program where var1=1 (i.e., &choice=1). This allows me to create crude and age-adjusted rates, titles, and output by my age grouping for var1. I now want to run this whole program multiple times, once each for var1=1, var2=1, var3=1, .... for 17 vars. Haven't figured it out yet and would love some help. 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Without more details, I can only give you a generic outline of such a program

 

%macro dothis;
    %let i=1 %to 17;
    %let choice=var&i;
    /* Run program */
%end;
%dothis

But (again I need more details) many analyses can be done without a macro and with one run of the program, by using multiple variables in a VAR statement, or with a BY statement, eliminating the need for macros entirely. This is preferable, in situations where it is possible to make the problem work without a macro.

 

So, please, don't be stingy with details. Provide more details about what you are doing, including what analyses you plan to do.

--
Paige Miller
ballardw
Super User

I agree with @PaigeMiller that details as to whether macro is even needed is important.

 

One generic macro approach is a driver macro that calls your other macro.

%macro driver (varlist = );
%do i  = 1 %to %sysfunc(countw( &varlist.));
   %let varname = %scan(&varlist., &i);
   /* this is where the call to the detail macro would go
     hopefully you provided a parameter that allows you
    to use &varname as the variable
  */

%end;
%mend;
/* call would have a space delimited list of variable names*/

%driver (varlist= thisvar thatvar anothervar)

You have not provided any details of where "choice" might come it, what the possibly ranges for each variable might be so cannot incorporate that at all.

 

Another approach would be to create a data set with the name of the variable and the "choice" value and use that to create calls to the macro using CALL EXECUTE in a data step using that data set.

Kurt_Bremser
Super User

When you run the same analysis on a series of variables, it is usually better to transpose the variables and run everything in one step, using _NAME_ in the BY, so you process a series of values.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 700 views
  • 3 likes
  • 4 in conversation