how do we merge 3 datasets in sas by an observation which is found in exactly 2 of the 3 data sets
Please expand on what you have and what you need. There are many ways of combining data sets and if there are any rules you need to provide them. Example data sets to represent EACH of your 3 sets and the desired result. The data sets should be in the form of data step code with enough variables and records to demonstrate the cases you expect to encounter in the full problem.
It doesn't really help. You haven't given examples of what the data sets would look like.
Here is a guess that assumes your data sets are already sorted:
data want;
merge d1 (in=in1) d2 (in=in2) d3 (in=in3);
by common_variable;
if in1 + in2 + in3 = 2;
run;
Given those examples, what do you want the resulting file to look like?
Art, CEO, AnalystFinder.com
That's why I asked you what you want the resulting file to look like. Rather than simply repeating your description, show the desired resulting file in the form of a datastep.
Art, CEO, AnalystFinder.com
I have NO idea what you are trying to explain with your incorrect use of the compress function, however, you use proc sorts with no by variable and try to merge 3 files that are different than those which you said you have. So, other than the new variable you are trying to create, does the following do the merge you wanted?:
proc sort data=ds1; by ptid; run; proc sort data=ds2; by ptid; run; proc sort data=ds3; by ptid; run; data want; merge ds1a (in=in1) ds2a (in=in2) ds3a (in=in3); by ptid; if in1 + in2 + in3 = 2; run;
Art, CEO, AnalystFinder.com
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.