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 |
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;
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.
Like this?
proc sql;
select *
from HAVE
group by ID
having count(unique ASSIGNMENT)=1;
quit;
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;
Thanks @mkeintz . 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
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.