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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 571 views
  • 1 like
  • 3 in conversation