BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas_student1
Quartz | Level 8

Hello,

I have a question on how to use a merge statement in SQL. That I would like to "convert" the following merge statment in SQL.

 

data want;

merge have1 (in=a) have2 (in=b);

by var1;

        if a=1 and b=1 then match='All';

else if a=1 and b=0 then match='have1';

else if a=0 and b=1 then match='have2';

run;

 

I know one can use the where the statement in SQL but that would be equivalent to the match='All' statement above, right? I want to also be able to see where there is data in one table but not the other and vise versa? Is it a join statment? But I also would like to create a new varaible like 'match' so it can flag the rows that match between two datasets and those that dont.

 

Any recommendation would be great!

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Yes, a join is what you need, most likely a full join, I can't seem to paste links for some reason so search sql join and you should see a diagram with red circles. 

 

 

Use a CASE statement with your logic. 

You won't have the automatic variables but you can use the BY variables. 

 

Assuming VAR1 has no missing values, the following is the CASE logic.

 

case when missing(var1) then 'status 1'
       when missing(b.var1) then 'status 2'
     when a.var1=b.var1) then 'match'
else 'CHECKME' end as match

 

 

View solution in original post

1 REPLY 1
Reeza
Super User

Yes, a join is what you need, most likely a full join, I can't seem to paste links for some reason so search sql join and you should see a diagram with red circles. 

 

 

Use a CASE statement with your logic. 

You won't have the automatic variables but you can use the BY variables. 

 

Assuming VAR1 has no missing values, the following is the CASE logic.

 

case when missing(var1) then 'status 1'
       when missing(b.var1) then 'status 2'
     when a.var1=b.var1) then 'match'
else 'CHECKME' end as match

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 1 reply
  • 1035 views
  • 0 likes
  • 2 in conversation