BookmarkSubscribeRSS Feed
thoughtshubham
Calcite | Level 5
 

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

2 REPLIES 2
Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 882 views
  • 0 likes
  • 3 in conversation