BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

Hello

I want to perform full merge of two data sets.

I want to define the rule-

If customer  exist in one table then take Y information from this table.

IF customer exit in both tables then do as following-

IF Y value is not null in both tables then take it from table aa

IF Y value is  null in one  table  then take Y value from the table where it is not null

 

 

what is the way to do it via data step?

what is the way to do it via proc sql?

data aa;
input CustID y;
cards;
111 .
222 20
444 50
555 70
123 35
;
run;
data bb;
input CustID y;
cards;
111 15
222 20
333 35
444 .
666 80
123 27
;
run;
proc sort data=aa; by CustID;Run;
proc sort data=bb; by CustID;Run;
data want;
merge aa bb;
by CustID;
Run;
4 REPLIES 4
Kurt_Bremser
Super User
Ronein
Onyx | Level 15
One to one
Kurt_Bremser
Super User

Then all you need is a DATA step. Expand your MERGE statement like this.

merge
  aa
  bb (rename=(y=y_b))
;

and use the COALESCE function to set y.

In SQL, also use COALESCE (both for aa.custid,bb.custid and aa.y,bb.y).

Tom
Super User Tom
Super User

Sound like you want to use UPDATE and not MERGE.

List the dataset you want to "WIN" last.

data aa;
  input CustID y;
cards;
111 .
123 35
222 20
444 50
555 70
;
data bb;
  input CustID y;
cards;
111 15
123 27
222 20
333 35
444 .
666 80
;

data want ;
  update bb aa;
  by custid;
run;

proc print;
run;

Note that for both the datasets need to be sorted by the BY variable(s).

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 355 views
  • 4 likes
  • 3 in conversation