DATA Step, Macro, Functions and more

use macro to repeat data step with merge?

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

use macro to repeat data step with merge?

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.


Accepted Solutions
Solution
‎07-18-2012 02:02 PM
Super Contributor
Posts: 1,636

Re: use macro to repeat data step with merge?

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


All Replies
Super Contributor
Posts: 282

Re: use macro to repeat data step with merge?

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.

New Contributor
Posts: 3

Re: use macro to repeat data step with merge?

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

Solution
‎07-18-2012 02:02 PM
Super Contributor
Posts: 1,636

Re: use macro to repeat data step with merge?

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

Respected Advisor
Posts: 3,124

Re: use macro to repeat data step with merge?

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

Super Contributor
Posts: 1,636

Re: use macro to repeat data step with merge?

Hi Haikuo,

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

  where id like "&likevar.%";

Thanks - Linlin

Super Contributor
Posts: 1,636

Re: use macro to repeat data step with merge?

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;

New Contributor
Posts: 3

Re: use macro to repeat data step with merge?

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

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 329 views
  • 3 likes
  • 4 in conversation