DATA Step, Macro, Functions and more

MERGE issue

Reply
N/A
Posts: 0

MERGE issue

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
Super Contributor
Posts: 260

Re: MERGE issue

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 1 reply
  • 95 views
  • 0 likes
  • 2 in conversation