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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

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