I Have created a macro for creating patient profiles.
%macro patprof ( subjid= );
..
..
..
..
%mend;
I have 550 patients in my study and I am calling my macro for every individual patient.
for example :-
%patprof(subjid= 101);
%Patprof(subjid=102);
%patprof(subjid=109);
is there any way that which improves my macro code so that I don't need to call my macro all 550 times for each patient.
any help is appreciated.
Ok, so then here is something that could work (I am using sashelp.class data set, and I want to split the data based on the name):
proc sql;
select cat(" '" || name ||" ' ") into: list_name separated by ","
from sashelp.class;
quit;
%macro nada();
%do i = 1 %to 19;
data name_&i.;
set sashelp.class;
if name = %scan("&list_name", &i., ",");
run;
%end;
%mend;
%nada();
Ok, so this is what I am hoping to do:
For you the above code could translate into something like this:
proc sql;
select subjid into: list_ids separated by ","
from your_master_data;
quit;
This macro variable called list_ids should be a string that looks like this: 101, 102, 109,143,165,..., 550
Then the next part:
%macro nada();
%do i = 1 %to 550;
data step....
....
if subjid = %scan("&list_ids", &i., ",");
...
run;
%end;
%mend;
%nada();
So, you want to add the %do i = 1 %to 550:
But you also want to add the if subjid = %scan("&list_ids", &i., ","); in your data step.
Let me know if this makes any sense.
Anca.
One quick way is to include a do-loop in your macro BUT this requires that your IDs are consecutive:
%macro patprof ( );
%do subjid = 1 %to 550;
..
..
..
..
%end;
%mend;
Now call the macro just once:
%patprof();
The problem is subjids are not consecutive..
Ok, so then here is something that could work (I am using sashelp.class data set, and I want to split the data based on the name):
proc sql;
select cat(" '" || name ||" ' ") into: list_name separated by ","
from sashelp.class;
quit;
%macro nada();
%do i = 1 %to 19;
data name_&i.;
set sashelp.class;
if name = %scan("&list_name", &i., ",");
run;
%end;
%mend;
%nada();
Ok, so this is what I am hoping to do:
For you the above code could translate into something like this:
proc sql;
select subjid into: list_ids separated by ","
from your_master_data;
quit;
This macro variable called list_ids should be a string that looks like this: 101, 102, 109,143,165,..., 550
Then the next part:
%macro nada();
%do i = 1 %to 550;
data step....
....
if subjid = %scan("&list_ids", &i., ",");
...
run;
%end;
%mend;
%nada();
So, you want to add the %do i = 1 %to 550:
But you also want to add the if subjid = %scan("&list_ids", &i., ","); in your data step.
Let me know if this makes any sense.
Anca.
Thanks a lot Anca. It was really helpful.
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.