BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mano
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
AncaTilea
Pyrite | Level 9

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.

View solution in original post

4 REPLIES 4
AncaTilea
Pyrite | Level 9

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();


Smiley Happy

mano
Calcite | Level 5

The problem is subjids are not consecutive..

AncaTilea
Pyrite | Level 9

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.

mano
Calcite | Level 5

Thanks a lot Anca. It was really helpful.Smiley Happy

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 4 replies
  • 2405 views
  • 0 likes
  • 2 in conversation