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

Hello,

I need to create multiple datasets, each one the product of a merge of two tables. Then I need to print the output of this merge. I have to do this around 60 times so I am hoping I can use a macro to do this, but I am new to them and have not worked with them before.

Here is my code that I will need to run over and over again and the bolded objects will change each time.

data merge_one;

     merge tbl1 (in=a) tbl2 (in=b);

     by id;

     if not b;

proc print data=merge_one;

     where id like '01%';

     var id date staff;

run;

any help would be much appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

Hi Victoria,

I have updated my code:

%macro test(likevar);

%do i=11 %to 50;

data merge_tblb&i.;

     merge tbl1 (in=a) tblb&i. (in=b);

     by id;

     if not b;

proc print data=&syslast;

     where id like "&likevar.%";

     var id date staff;

run;

%end;

%mend test;

%test

Linlin

View solution in original post

7 REPLIES 7
Amir
PROC Star

Hi,

You can try creating a macro function based on the existing code, e.g. (untested):

%macro mymerge(dsname,tname,likevar);

  data &dsname;

       merge tbl1 (in=a) &tname (in=b);

       by id;

       if not b;

  proc print data=&dsname;

       where id like "&likevar%";

       var id date staff;

  run;

%mend mymerge;

Please note the use of double quotes (") instead of the single quotes (') used with the like operator, as macro variables do not resolve when surrounded by single quotes.

Then you can invoke the macro function, e.g.:

%mymerge(aaa,bbb,ccc);

%mymerge(ddd,eee,fff);

%mymerge(ggg,hhh,iii);

etc...

If there is some pattern to the data set names and values to be supplied, then there is a chance that all 60 invocations could be automated in a loop.

Regards,

Amir.

victoriav
Calcite | Level 5

Hi Thank you!

The data set names do have a pattern. The pattern is TBLB11.....TBLB50.

So I was going to do something like

%macro mymerge(tname,likevar);

   %do I=11 %to 50;

  data tblb&I;

       merge tbl1 (in=a) &tname (in=b);

       by id;

       if not b;

  proc print data= tblb&I;

       where id like "&likevar%";

       var id date staff;

  run;

%END

%mend mymerge;


Thank you for the note on the double quotes-I think that was one of my problems. This was so fun! I will try this way and Linlin's way (I am less familiar with her code but I would love to learn!) as well and report back soon.

Victoria

Linlin
Lapis Lazuli | Level 10

Hi Victoria,

I have updated my code:

%macro test(likevar);

%do i=11 %to 50;

data merge_tblb&i.;

     merge tbl1 (in=a) tblb&i. (in=b);

     by id;

     if not b;

proc print data=&syslast;

     where id like "&likevar.%";

     var id date staff;

run;

%end;

%mend test;

%test

Linlin

Haikuo
Onyx | Level 15

LinLin,

Your first post has addressed an practical approach, as long as the library info is available, you don't need to look for naming pattern (what if there is none?). The only part was missing in your post is how to get tables names, so why not just use some metadata?

proc sql;

select distinct memname into :dsn separated by ' ' from dictionary.columns where libname='WORK';

QUIT;

Haikuo

Linlin
Lapis Lazuli | Level 10

Hi Haikuo,

My first post doesn’t work because there is no macro variable for

  where id like "&likevar.%";

Thanks - Linlin

Linlin
Lapis Lazuli | Level 10

Your need a ";" after "END".

%macro mymerge(tname,likevar);

   %do I=11 %to 50;

  data tblb&I;

       merge tbl1 (in=a) &tname (in=b);

       by id;

       if not b;

  proc print data= tblb&I;

       where id like "&likevar%";

       var id date staff;

  run;

%END

%mend mymerge;

victoriav
Calcite | Level 5

Thanks Linlin, your code worked! I appreciate your help. Also thanks to Amir.

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
  • 1097 views
  • 3 likes
  • 4 in conversation