Input data:
group flag
1 .
1 .
1 1
1 .
2 .
2 1
2 .
3 .
3 1
3 .
3 1
3 .
3 .
;
I want the following output
so for every group, if there is a flag=1 then all the flag_new should be a 1 for everyone in that group. If missing then all the values should be 0
Output desired:
group flag new_flag
1 . 1
1 . 1
1 1 1
1 . 1
2 . 0
2 . 0
2 . 0
3 . 1
3 1 1
3 . 1
3 1 1
3 . 1
3 . 1
Why is the output for group 2 a zero when there was one non-missing in the input?
PS: Don't type in a code box if you are not going to show us code.
if I knew how to do it or had code I would type it in. I mean how else are you supposed to ask a question? thank you for your response.
You just did it. You typed your comment.
But why does group 2 have a zero in the output but a non-missing in the input?
data want;
merge have have (keep=group flag where=(flag^=.));
by group;
if flag=. then flag=0;
run;
This works as expected if (1) data are sorted by group, (2) no group has any variation in non-missing values of flag.
data have;
input group flag;
cards;
1 .
1 .
1 1
1 .
2 .
2 .
2 .
3 .
3 1
3 .
3 1
3 .
3 .
;
proc sql;
create table want as
select *,max(flag)=1 as new_flag
from have
group by group;
quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.