Hi all,
I wanted to examine data with subset cohort included step by step. For example, there are five cohorts, A-E, and I wanted to run regression with the first cohort, and then I would run regression with an additional cohort each time. How can I conduct this using MACRO or iteration efficiently?
proc reg data=mydata;
model dependent=independents;
where cohort in ('A');
run;
proc reg data=mydata;
model dependent=independents;
where cohort in ('A','B');
run;
proc reg data=mydata;
model dependent=independents;
where cohort in ('A','B','C');
run;
proc reg data=mydata;
model dependent=independents;
where cohort in ('A','B','C','D');
run;
proc reg data=mydata;
model dependent=independents;
where cohort in ('A','B','C','D','E');
run;
I would think I can probably start with creating the list like below:
proc sql noprint;
select distinct quote(cohort) into :st_list separated by ','
from mydata;
quit;
OK, here's a bit more of what a macro could look like. All of this would follow the code you originally posted. It assumes you remove the comma and use a blank as the "separated by" character.
%macro regloop;
%local n subset;
%do n=1 to &sqlobs;
%let subset = &subset %scan(&stlist, &n);
proc reg data=mydata;
model dependent = independents;
where cohort in (&subset);
run;
%end;
%mend region;
%regloop
This will generate 5 PROC REGs, just like you have. But the cohort values (and the actual number of PROC REGs) will depend on the data set you are processing.
I assume that the code you showed works. What would a macro do that this code does not do? You mention "iteration", what would be iterating?
I also question the statistical methodology here about adding cohort after cohort into the data in sequential fashion like you are doing. I would imagine a better way to see the impact of cohort is to add it into the model so that you can have different intercepts and/or different slopes for the different cohorts.
@lichee wrote:
I'm testing out my code, but cannot make it work. Just not good at iteration, especially when it's in macro.
Its still not clear to me what "iteration" you mean, where in this problem is there "iteration"? Please describe in detail.
OK, here's a bit more of what a macro could look like. All of this would follow the code you originally posted. It assumes you remove the comma and use a blank as the "separated by" character.
%macro regloop;
%local n subset;
%do n=1 to &sqlobs;
%let subset = &subset %scan(&stlist, &n);
proc reg data=mydata;
model dependent = independents;
where cohort in (&subset);
run;
%end;
%mend region;
%regloop
This will generate 5 PROC REGs, just like you have. But the cohort values (and the actual number of PROC REGs) will depend on the data set you are processing.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.