DATA Step, Macro, Functions and more

Macro to create patient profiles

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Macro to create patient profiles

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.


Accepted Solutions
Solution
‎02-25-2013 11:17 AM
Super Contributor
Posts: 543

Re: Macro to create patient profiles

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


All Replies
Super Contributor
Posts: 543

Re: Macro to create patient profiles

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

Contributor
Posts: 28

Re: Macro to create patient profiles

The problem is subjids are not consecutive..

Solution
‎02-25-2013 11:17 AM
Super Contributor
Posts: 543

Re: Macro to create patient profiles

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.

Contributor
Posts: 28

Re: Macro to create patient profiles

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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