02-01-2018 12:41 PM - last edited on 02-01-2018 12:57 PM by Reeza
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;
Data dummy; Merge A (IN = X) B (IN=Y); by ID; If X and Y; run;
1 5 C 2 4
2 7 D 2 5
1 5 Cc 2 4
2 7 Dd 2 5
02-01-2018 12:51 PM
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.
02-01-2018 12:54 PM
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.