BookmarkSubscribeRSS Feed
SeanZ
Obsidian | Level 7

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.

2 REPLIES 2
LinusH
Tourmaline | Level 20

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
Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1896 views
  • 6 likes
  • 3 in conversation