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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.