DATA Step, Macro, Functions and more

how to merge a dataset using macro loop

Reply
Frequent Contributor
Posts: 122

how to merge a dataset using macro loop

I coded a macro loop to get a value size for each month(loop), and then want to merge all size for every month into one dataset, but I have difficulty to achieve this purpose. Let me illustrate my attempt.For each loop I will get one dataset containing firmid and size, but for each loop the firmid may be different (most of them are overlapped). For instance, month 1 would be like

firmid          size

1               100

2               150

5               130

month 2 would be something like

firmid          size

1               120

2               140

4               100

What I want to acheive is something like

firmid          size(m1)          size(m2)

1               100                    120

2               150                    140

4               .                         100

5               130                    .

Please help. Thanks.

Super User
Posts: 5,429

Re: how to merge a dataset using macro loop

Yes, ou could merge or SQL full join those results.

One way to do it dynamically is that in your loop (depending on the logic there...?) is to build som kind of dynamic SAS-code, which you execute in some point, i.e. using call execute.

Do you have the same data source for each loop? If so, what is the logic for creating size? Perhaps there is an easier way to solve the whole problem...

Data never sleeps
Super User
Posts: 5,504

Re: how to merge a dataset using macro loop

Since you're asking about a basic merge, I prefer to skip references to macro language and skip trying to automate the process.  Here's a simple solution for two data sets.  Assuming they are already sorted by FIRMID:

data want;

   merge month1 (rename=(size=size_m1))

               month2 (rename=(size=size_m2));

   by firmid;

run;

It should be easy enough to extend that to more than 2 data sets.

Good luck.

Ask a Question
Discussion stats
  • 2 replies
  • 884 views
  • 6 likes
  • 3 in conversation