BookmarkSubscribeRSS Feed
AshokD
Obsidian | Level 7

I have two datasets.

DATASET :- A

EE_ID    PRJ_CD

100055   406

100055   475

100055   492

300028   475

 

DATASET :- B

 

EE_ID    SSN       EMP_CD

100055 0100055   E

300028 0300028   E

 

I have sorted & merged the above two datasets by EE_ID.

DATA
FINAL
;
MERGE
A

B ;
BY
EE_ID
;

IF A THEN OUTPUT;

RUN;

 

Output:-

EE_ID     PRJ_CD   SSN        EMP_CD

100055    475
100055    492
100055    406        0100055    E
300028    475        0300028    E

 

 

SSN and EMP_CD is getting populated for only one row and the remaining two rows are empty for the EE_ID 100055.

 

My expectation is :-

EE_ID    PRJ_CD SSN          EMP_CD

100055 475         0100055     E
100055 492         0100055     E
100055 406         0100055     E
300028 475         0300028    

 

Any help would be appreciated.

 

Thanks!

2 REPLIES 2
Kurt_Bremser
Super User

a) don't write code in capitals; causes eyesore

b) use the "little running man" icon while composing a post to open a window for entering code; this makes example code more readable

c) use proper indentation to make code more readable

d) use the in= dataset option to create boolean variables that indicate when a dataset contributes to a merge or set:

data have_a;
input ee_id prj_cd;
cards;
100055 406
100055 475
100055 492
300028 475
;
run;

data have_b;
input ee_id ssn emp_cd $;
cards;
100055 0100055 E
300028 0300028 E
;
run;

data want;
merge
  have_a (in=a)
  have_b (in=b)
;
by ee_id;
if a;
run;

proc print noobs;
run;

The output from the above code looks like this:

 ee_id    prj_cd      ssn     emp_cd

100055      406     100055      E   
100055      475     100055      E   
100055      492     100055      E   
300028      475     300028      E   

 

Astounding
PROC Star

The result you are describing looks like what you would get if you played with the program many times in many ways, in an attempt to get it to work.  What actually happened somewhere along the way was that the variables SSN and EMP_CD got added to the data set A (somewhere along the way).  Then when you merge, those values get read in and overwrite the values from data set B.  

 

DROP the variables you don't need from the data set A.  That should take care of the problem.

sas-innovate-2024.png

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.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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