SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
chinna0369
Pyrite | Level 9

Hi all, 

 

I have below data

 

data have1;
input id;
datalines;
1287 
1805 
1881 
1881 
;
run;

data have2;
input id;
datalines;
1287 
1805 
1805 
1805 
1881 
1892 
1892 
1892 
;
run;

I want records, below are the conditions:

1. if have1 and have2 has same id then I want those records from have1.

2. if have1 has the id and have2 doesn't have it then we want it.

3. if have1 doesn't have the id and have2 has it then we want those records.

 

For example below:

data want;
input id;
datalines;
1287
1805
1881
1881
1892
1892
1892
;
run;

Thanks,

Adi

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Does this represent your actual data? Do you have only the ID variable in your data sets?

ballardw
Super User

@chinna0369 wrote:

Hi all, 

 

I have below data

 

data have1;
input id;
datalines;
1287 
1805 
1881 
1881 
;
run;

data have2;
input id;
datalines;
1287 
1805 
1805 
1805 
1881 
1892 
1892 
1892 
;
run;

I want records, below are the conditions:

1. if have1 and have2 has same id then I want those records from have1.

2. if have1 has the id and have2 doesn't have it then we want it.

3. if have1 doesn't have the id and have2 has it then we want those records.

 

For example below:

data want;
input id;
datalines;
1287
1805
1881
1881
1892
1892
1892
;
run;

Thanks,

Adi


I am having a very hard time telling what you want and whether you want 1, 2 or 3 data sets.

Your current list of requirements basically says that you want everything, at least given the limited data.

 

This creates 3 datasets, each matching a different one of your requirements. Not: you did not provide data that has any records for the second requirement.

proc sort data=have1;
   by id;
run;

proc sort data=have2;
  by id;
run;

data want1
     want2
     want3
;
   merge have1 (in=in1)
         have2 (in=in2)
   ;
by id; if in1 and in2 then output want1; if in1 and not in2 then output want2; if not in1 and in2 then output want3; run;

The IN= option creates a temporary variable that indicates whether the current record has a contribution from the given data set. This is a 1/0, i.e. true/false value, so can be used to perform conditional actions based on the status of the variable(s).

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 918 views
  • 0 likes
  • 3 in conversation