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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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