Hi,
I want to merge 3 datasets dataA, dataB and dataC, keeping records only found in dataA or dataB and exclude anything found in dataC. What approch would be more effective? SET or MERGE? I'm not able to run code at the moment so as to test the effeciency. Thus, I am trying to figure out through support and Help the merging funcions. I could do it with two datasets but still struggle with keeping only records found in A and B. I'd appreciate your ideas
Close, but no cigar
Double check your output, especially if you have duplicate X's in any of your datasets.
Data Merged_data
Merge dataA(IN=fromdataA)
dataB(IN=fromdataB)
dataC(in=fromdataC)
BY x;
*Create indicator variables to show in which dataset the observation is present;
SourceA=fromdataA;
SourceB=fromdataB;
SourceC=fromdataC;
*keep only from A and B;
if fromdataA AND fromdataB;
run;
It's an exercise and the description doesn't give more details on that. In my understanding it is asked to merge the datasets side by side
Well, set appends data sets so you're looking for a MERGE then. Look at the dataset IN option,example 4.
http://www.ats.ucla.edu/stat/sas/modules/merge.htm
So, something like the following would be correct? inputs and numbers are totally random
Data dataA
input x, y, z;
cards;
5, 10, 15
55, 24, 29
run;
proc sort data=dataA;
by x;
run;
Data dataB
input x, y, z;
cards;
105, 110, 150
run;
proc sort data=dataB;
by x;
run;
Data dataB
input x, y, z;
cards;
665, 1110, 1150
780, 455, 447
159, 668, 31
;
run;
proc sort data=dataC
by x;
run;
Data Merged_data
Merge dataA(IN fromdataA) dataB(IN fromdataB) dataC
BY x;
fromdataA=fromdataB;
run;
Close, but no cigar
Double check your output, especially if you have duplicate X's in any of your datasets.
Data Merged_data
Merge dataA(IN=fromdataA)
dataB(IN=fromdataB)
dataC(in=fromdataC)
BY x;
*Create indicator variables to show in which dataset the observation is present;
SourceA=fromdataA;
SourceB=fromdataB;
SourceC=fromdataC;
*keep only from A and B;
if fromdataA AND fromdataB;
run;
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.
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.