DATA Step, Macro, Functions and more

select records by group with idenitcal variable values in the group

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 124
Accepted Solution

select records by group with idenitcal variable values in the group

I need to select obervations which are groupd by variable 'id', where the values for the 2nd variable 'Assignment' is identical i.e 'Z' at all instances of the 'date variable for the id i.e id=2. Please see table below of the original dataset and final dataset needed. Any suggestions on how I can go about this? Please advise.

 

Original_dataset

 

 

ID

Assignment

Date

1

Z

date1

1

D

date2

1

B

date3

1

B

date4

2

Z

date1

2

Z

date2

2

Z

date5

3

Z

date2

3

M

date4

3

Z

date6

 

 

 

Final dataset

 

 

ID

Assignment

Date

2

Z

date1

2

Z

date2

2

Z

date5


Accepted Solutions
Solution
‎04-28-2017 12:40 PM
Valued Guide
Posts: 797

Re: select records by group with idenitcal variable values in the group

i think you want all observations for any ID which only has assignment='Z' (i.e. count of non-z's =0).  If so, then this will work:

 

data want;
  do notz=0 by 0 until (last.id);
    set have;
    by id ;
    if assignment^='Z' then notz=notz+1;
  end;

  do until (last.id);
    set have;
    by id;
    if notz=0 then output;
  end;
run;

View solution in original post


All Replies
Super User
Posts: 10,550

Re: select records by group with idenitcal variable values in the group

I'm not seeing and grouping going on just selection.

 

Data want;

   set have;

    where Id=2 and assignment='Z';

run;

 

That may be Id='2' above, I cannot tell whether your Id variable is numeric or character.

PROC Star
Posts: 1,570

Re: select records by group with idenitcal variable values in the group

Like this?

 


proc sql;
select *    
from HAVE
group by ID
having count(unique ASSIGNMENT)=1;
quit;
Solution
‎04-28-2017 12:40 PM
Valued Guide
Posts: 797

Re: select records by group with idenitcal variable values in the group

i think you want all observations for any ID which only has assignment='Z' (i.e. count of non-z's =0).  If so, then this will work:

 

data want;
  do notz=0 by 0 until (last.id);
    set have;
    by id ;
    if assignment^='Z' then notz=notz+1;
  end;

  do until (last.id);
    set have;
    by id;
    if notz=0 then output;
  end;
run;
Frequent Contributor
Posts: 124

Re: select records by group with idenitcal variable values in the group

Thanks @    . Sorry I wasn; able to reply earlier. I was running into an issue with my login. I did solve the concern just like you did                

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 128 views
  • 0 likes
  • 4 in conversation