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

Hi,

I currently have a  macro set up that 'profiles' my customers - %Profile(&Group)

The macro runs through several datastep procedures and essentially creates a standard output data set named Profile_&Group.

If I have 3 profiles to create I would run the macro 3 times for example:

%Profile(HighValue) ; (output: Profile_HighValue)

%Profile(MedValue) ; (output: Profile_MedValue)

%Profile(LowValue) ; (output: Profile_LowValue)

After the macro is complete I have a separate code I would run manually to merge the output datasets into a summary table:

Data Profile_Summary ; Merge Profile_HighValue Profile_MedValue Profile_LowValue ;

by ID;

run ;

Sometimes I would only have 2 groups (High,Med) or possibly 5+ groups to profile and merge to one single dataset.

My question:

What is the best way to incorporate my datastep to merge the profile tables together within the macro?

Thanks for your help.

KD

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You could write a new macro that would control all the processing.  One example:

%macro control (mylist);

   %local i next_name;

   %do i=1 %to %sysfunc(countw(&mylist));

         %let next_name = %scan(&mylist, &i);

         %profile (&next_name)

   %end;

   data profile_summary;

   merge %do i=1 %to %sysfunc(countw(&mylist));

      %let next_name = %scan(&my_list, &i);

      profile_&next_name

   %end;

   ;

   by ID;

   run;

%mend control;

%control (HighValue LowValue MedValue)

This is untested code, so you may find a tweak is in order.  But it sounds like the program form is exactly what you are asking for.  Good luck.

View solution in original post

7 REPLIES 7
Astounding
PROC Star

You could write a new macro that would control all the processing.  One example:

%macro control (mylist);

   %local i next_name;

   %do i=1 %to %sysfunc(countw(&mylist));

         %let next_name = %scan(&mylist, &i);

         %profile (&next_name)

   %end;

   data profile_summary;

   merge %do i=1 %to %sysfunc(countw(&mylist));

      %let next_name = %scan(&my_list, &i);

      profile_&next_name

   %end;

   ;

   by ID;

   run;

%mend control;

%control (HighValue LowValue MedValue)

This is untested code, so you may find a tweak is in order.  But it sounds like the program form is exactly what you are asking for.  Good luck.

DangIT
Fluorite | Level 6

Thank you Astounding, this worked perfectly (just had one typo in above code).

data_null__
Jade | Level 19

can you use?

data Summary;

     merge profile_:;

     by ID;

     run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Great idea if it works, you could also do:

data _null_;

     set sashelp.vtables (where=(libname="WORK" and index(MEMNAME,"PROFILE")>0);

     by libname;

     if first.libname then call execute('data profile_summary; merge ');

     call execute(" "||strip(memname));

     if last.libname then call execute('; by id; run;');

run;

data_null__
Jade | Level 19

RW9 wrote:

Great idea if it works, you could also do:

I use it all the time.:smileyshocked:

DangIT
Fluorite | Level 6

How did you make that work?? I don't think SAS recognizes the the colon for datasets.

data_null__
Jade | Level 19

23         data class_1 class_2 Class_3;

24            set sashelp.class;

25            run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.

NOTE: The data set WORK.CLASS_1 has 19 observations and 5 variables.

NOTE: The data set WORK.CLASS_2 has 19 observations and 5 variables.

NOTE: The data set WORK.CLASS_3 has 19 observations and 5 variables.

NOTE: DATA statement used (Total process time):

      real time           0.04 seconds

      cpu time            0.02 seconds

     

26         data class;

27            merge class_:;

28            by name;

29            run;

NOTE: There were 19 observations read from the data set WORK.CLASS_1.

NOTE: There were 19 observations read from the data set WORK.CLASS_2.

NOTE: There were 19 observations read from the data set WORK.CLASS_3.

NOTE: The data set WORK.CLASS has 19 observations and 5 variables.

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
  • 7 replies
  • 1066 views
  • 5 likes
  • 4 in conversation