BookmarkSubscribeRSS Feed
sbopster
Calcite | Level 5
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
5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
sbopster
Calcite | Level 5

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.

PaigeMiller
Diamond | Level 26

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?

--
Paige Miller
mkeintz
PROC Star
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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
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;
How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1174 views
  • 0 likes
  • 4 in conversation