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

I'm looking how I can keep only those with var1=1 and var2=1. If one participants has var1=1 and var2=1, I was to keep all rows of the participants.

This is the initial dataset

data new;
infile cards missover;
input id var1 var2;
cards;
11 1 0
11 1 0
12 1 1
12 1 1
12 0 1
13 1 1
14 0 1
15 1 1
16 1 1
17 0 1
17 0 1
17 1 1
17 0 0
run;

The final dataset should be like this:

data new1;
infile cards missover;
input id var1 var2;
cards;
12 1 1
12 1 1
12 0 1
13 1 1
15 1 1
16 1 1
17 0 1
17 0 1
17 1 1
17 0 0
run;

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

This is a good example of applying a self-merge of a subset of a dataset with the entire dataset, as in:

 

data new;
infile cards missover;
input id var1 var2;
cards;
11 1 0
11 1 0
12 1 1
12 1 1
12 0 1
13 1 1
14 0 1
15 1 1
16 1 1
17 0 1
17 0 1
17 1 1
17 0 0
run;

data new1;
  merge new (where=(var1=1 and var2=1)  in=inkeep)
        new;
  by id;
  if inkeep;
run;

 

Why does this work:

 

  1. The MERGE with a BY statement tells SAS to match records (based on ID) satisfying the first argument of merge with the second argument.
  2. The "IN=" parameter sets the dummy variable INKEEP to 1 if the match-merge has any observation satisfying "var1=1 and var2=1".
  3. Now you might be worried about a "collision" of data values.  For example the first obs matching var1=1 and var2=1 might not be the first obs overall for a given ID.  But MERGE works such that data values in the 2nd argument superseded the data values in the first argument for variable having the same name.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

This is a good example of applying a self-merge of a subset of a dataset with the entire dataset, as in:

 

data new;
infile cards missover;
input id var1 var2;
cards;
11 1 0
11 1 0
12 1 1
12 1 1
12 0 1
13 1 1
14 0 1
15 1 1
16 1 1
17 0 1
17 0 1
17 1 1
17 0 0
run;

data new1;
  merge new (where=(var1=1 and var2=1)  in=inkeep)
        new;
  by id;
  if inkeep;
run;

 

Why does this work:

 

  1. The MERGE with a BY statement tells SAS to match records (based on ID) satisfying the first argument of merge with the second argument.
  2. The "IN=" parameter sets the dummy variable INKEEP to 1 if the match-merge has any observation satisfying "var1=1 and var2=1".
  3. Now you might be worried about a "collision" of data values.  For example the first obs matching var1=1 and var2=1 might not be the first obs overall for a given ID.  But MERGE works such that data values in the 2nd argument superseded the data values in the first argument for variable having the same name.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
sandrube
Fluorite | Level 6

Thank you very much for the solution!!!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 586 views
  • 1 like
  • 2 in conversation