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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1630 views
  • 0 likes
  • 2 in conversation