Admittedly, I always forget the answer to this question and have to search through old code.
Question: How do I create a list of variables to run one at a time through a procedure.
I want to run the below piece of code once for each individual variable. So it will run 23 models.
Y= Dependent Variable
X1-X23: Independent Variables
PROC LOGISTIC DATA = Dataset_Name ALPHA=0.002;
CLASS Y;
MODEL Y (EVENT="1") = X1 / LACKFIT FIRTH;
RUN;
Do it with a macro:
%macro run_model;
%do i=1 %to 23;
PROC LOGISTIC DATA = Dataset_Name ALPHA=0.002;
CLASS Y;
MODEL Y (EVENT="1") = X&i / LACKFIT FIRTH;
RUN;
%end;
%mend run_model;
%run_model;
Do it with a macro:
%macro run_model;
%do i=1 %to 23;
PROC LOGISTIC DATA = Dataset_Name ALPHA=0.002;
CLASS Y;
MODEL Y (EVENT="1") = X&i / LACKFIT FIRTH;
RUN;
%end;
%mend run_model;
%run_model;
This works very well when your variable names are actually X1 - X23. However, you have to code it differently if you have other names and would call a macro using:
%do_them_all (varlist=a long list of many variable names)
Here's a way to approach that problem:
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.