DATA Step, Macro, Functions and more

why these code giving different result?

Reply
Regular Learner
Posts: 1

why these code giving different result?

[ Edited ]
 

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

Super User
Posts: 10,544

Re: why these code giving different result?

Posted in reply to thoughtshubham

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,921

Re: why these code giving different result?

Posted in reply to thoughtshubham

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.

Ask a Question
Discussion stats
  • 2 replies
  • 106 views
  • 0 likes
  • 3 in conversation