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.
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.
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.
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.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.