Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Subset records based on the occurrence of one observation but also additional ones

suppose to have the following:

  ID       Rep    Year     
 0001      exA    2015
 0001      exA    2015
 0002      exA    2015
 0002      exB    2015
 0002      exA    2015
 0002      exC    2015 
 0003      exC    2016
 0003      exA    2016
 0003      exD    2016
 0003      exA    2016 
.....     ...    ....

is there a way to subset IDs based on the appearance of exA in Rep column but also other ex*? This, only for 2015. ID 0001 that has only exA should not be extracted. The criteria is the presence of exA (mandatory) but also other ex*

Desired output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  ID       Rep    Year           
 0002      exA    2015       
 0002      exB    2015
 0002      exA    2015
 0002      exC    2015 

>Solution :

Try this

data have;
input ID $ Rep $ Year;
datalines;
0001 exA 2015 
0001 exA 2015 
0002 exA 2015 
0002 exB 2015 
0002 exA 2015 
0002 exC 2015 
0003 exC 2016 
0003 exA 2016 
0003 exD 2016 
0003 exA 2016 
;

proc sql;
   create table want as
   select * from have
   where year = 2015
   group by ID
   having sum(Rep = 'exA') > 0
      and count(distinct Rep) > 1
   ;
quit;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading