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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.