BookmarkSubscribeRSS Feed
ywon111
Quartz | Level 8

I am trying to merge two datasets. The issue is the first dataset has missing values in column c1, if missing c1 then merge with c2.

 

data have1;
input ID c1 $ c2 $;
infile datalines missover;
datalines;
1 A A
2 B
3 C
4 . E
;

 

 

data have2;
input c1 $ c2 $;
infile datalines missover;
datalines;
A AA
B BB
C CC
D DD
E EE
;

 

data want;
input ID c1 $ result $;
infile datalines missover;
datalines;
1 A AA
2 B BB
3 C CC
4 E EE
;

2 REPLIES 2
Kurt_Bremser
Super User
data want;
set have1;
if _n_ = 1
then do;
  length result $8;
  declare hash h2 (dataset:"have2 (rename=(c2=result))");
  h2.definekey("c1");
  h2.definedata("result");
  h2.definedone();
end;
c1 = coalescec(c1,c2);
if  h2.find() ne 0 then result = "";
run;

Untested, posted from my tablet.

LinusH
Tourmaline | Level 20

SQL version:

proc sql;
	create table want as
		select id, coalesce(have1.c1, have1.c2) as c1, have2.c2 as result
		from have1
		left join have2
		on coalesce(have1.c1, have1.c2) = have2.c1
	;
quit;

Small remark, since c1 is declared as char, the <.> is stored as a period, not as MISSING.

 

Data never sleeps

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 620 views
  • 1 like
  • 3 in conversation