BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I am trying to merge three datasets with inequal no of observations. Dataset A has 10 observations, Dataset B has 15 observations and dataset C has 3 observations. The objective of merging is to have a final dataset with a variable present in dataset C and it should have all the variables present in A.

Note: There are no common variables in dataset A and C.

I first sort dataset A and B with the common variable and merge them using the same common variable. The resultant dataset D is sorted by the same variable as in dataset C and are merged using the common variable.

After the merge the final dataset has blank obs in it and also when I am trying to sort the final data set using the same variable. It is giving me an error saying that there is an error in the sorting variable.

Please can you let me know what is wrong here?

Thanks,
APS
1 REPLY 1
1162
Calcite | Level 5
Have you tried this:

data X;
merge D (in=a) C (in=b);
by common_var;
if b;
run;

The "if b" should return only records where C has a record.


I've also found good success with PROC SQL and the LEFT JOIN / RIGHT JOIN / FULL JOIN functionality. I seem to recall that the way a DATA step and SQL merge is different, so sometimes one might be better suited to your application.

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!

New Learning Events in April

 

Join us for two new fee-based courses: Administrative Healthcare Data and SAS via Live Web Monday-Thursday, April 24-27 from 1:00 to 4:30 PM ET each day. And Administrative Healthcare Data and SAS: Hands-On Programming Workshop via Live Web on Friday, April 28 from 9:00 AM to 5:00 PM ET.

LEARN MORE

Discussion stats
  • 1 reply
  • 775 views
  • 0 likes
  • 2 in conversation