Why these code giving different result?
Data B;
Input ID Name$ Weight;
cards;
2 A 2
4 B 3
5 Cc 4
7 Dd 5
;
run;
Data A;
Input ID Name$ Height;
cards;
1 A 1
3 B 2
5 C 2
7 D 2
9 E 2
;
run;
PROC SQL;
Create table dummy as
Select * from A as x inner join B as y
On x.ID = y.ID;
Quit;
output:
ID
Name
Height
Weight
Data dummy;
Merge A (IN = X) B (IN=Y);
by ID;
If X and Y;
run;
Output:
1 5 C 2 4
2 7 D 2 5
ID
Name
Height
Weight
1 5 Cc 2 4
2 7 Dd 2 5
First, read the log. The WARNING you get from the SQL provides the first clue.
There is a difference between the data and SQL steps in the treatment of variables with the same name: SQL discards any further variables (so you get the values of the first dataset), while the data step reserves one slot and fills it in the sequence in which observations are read, so that (in your case) values from the second dataset appear in the output.
Your SQL code selects all the variables from A. The only reason for using any data from B is to compare the IDs and select the matches.
Your MERGE on the other hand, takes on data from both data sets. Where there is a match on ID, NAME from B replaces NAME from A as the final value.
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.