BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Is there a way to recover the original non-merge-matched observations without any additional variables?

For example, suppose that I am trying to obtain three separate data sets using the following codes:

data July31_11
July31_10
July31_01;
Merge one (in=ones) two (in=twos);
by id sex age state;
if ones=1 and twos=1 then output July31_11;
if ones=1 and twos=0 then output July31_10;
if ones=0 and twos=1 then output July31_01;
run;

My problem is that although the July31_10 and July31_01 give me the non-merge-matched cases (cases whether one of the data set did not contribute to the current observation because of missing values or other reasons), these will contain anyway the same total number of variables as the July31_11 data set will have.

I am interested in obtain (“recover”) the non-contributing cases in their original form (as they was in their respective original data set) with the original set of variables.

Do you have any suggestion?

Thanks
1 REPLY 1
Olivier
Pyrite | Level 9
Hi Caramel.
I suggest you collect the variables names before merging, and then only KEEP them in the output datasets. Either you name each list of variable names, or do it automatically -- as the program below suggests to do.
[pre]
PROC CONTENTS DATA=one OUT=dico_one NOPRINT ;
RUN ;
PROC CONTENTS DATA=two OUT=dico_two NOPRINT ;
RUN ;
PROC SQL NOPRINT ;
SELECT LEFT(TRIM(name)) INTO list_one SEPARATED BY " "
FROM dico_one ;
SELECT LEFT(TRIM(name)) INTO list_two SEPARATED BY " "
FROM dico_two ;
QUIT ;
data July31_11
July31_10 (KEEP=&list_one)
July31_01 (KEEP=&list_two) ;
Merge one (in=ones) two (in=twos);
by id sex age state;
if ones=1 and twos=1 then output July31_11;
if ones=1 and twos=0 then output July31_10;
if ones=0 and twos=1 then output July31_01;
run;
[/pre]
Olivier

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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