BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Zakharkou
Calcite | Level 5

Dear SAS Community,

 

I have the following data set:

 

id var1 var2
1 1 0
1 0 1
1 1 0
1 1 0
2 0 1
2 0 1

 

How can I flag (for example flag=1) the whole group (ID) if var1 and var2 are equal to 1 at least once?

 

many thanks in advance!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

First thought for DATA step was DOW loop, but SQL could do it too:

 

data have ;
  input id var1 var2 ;
  cards ;
1 1 0
1 0 1
1 1 0
1 1 0
2 0 1
2 0 1
3 1 0
4 1 1
;
run ;

proc sql ;
  select *
        ,(max(var1)=1 and max(var2)=1) as flag
  from have
  group by id
 ;
quit ;

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

7 REPLIES 7
Quentin
Super User

First thought for DATA step was DOW loop, but SQL could do it too:

 

data have ;
  input id var1 var2 ;
  cards ;
1 1 0
1 0 1
1 1 0
1 1 0
2 0 1
2 0 1
3 1 0
4 1 1
;
run ;

proc sql ;
  select *
        ,(max(var1)=1 and max(var2)=1) as flag
  from have
  group by id
 ;
quit ;

 

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
s_lassen
Meteorite | Level 14

Your SQL solution will only work if 1 is always the maximum value - if there are larger values as well, e.g. 2 or 888, your solution will not find the ones.

Quentin
Super User
Agree, I’m assuming that the OP is deriving a flag from other flags.
The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
s_lassen
Meteorite | Level 14

Actually, @Zakharkou wanted to find out if there was a 1 in the group, not any positive value, so the SQL solution should be written as 

proc sql ;
  select *
        ,(max(var1 = 1) and max(var2 = 1) as flag
  from have
  group by id
 ;
quit ;
s_lassen
Meteorite | Level 14

You can do it with a datastep, like this:

data want;                                                                                                                              
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    if var1=1 then flag1=1;                                                                                                             
    if var2=1 then flag2=1;                                                                                                             
    end;                                                                                                                                
  flag=flag1 and flag2;                                                                                                                 
  do until(last.id);                                                                                                                    
    set have;                                                                                                                           
    by id;                                                                                                                              
    output;                                                                                                                             
    end;                                                                                                                                
  drop flag1 flag2;                                                                                                                     
run;
Zakharkou
Calcite | Level 5

Thank you for the numerous solutions. Those 2 variables were indeed flags, so Quentin's solution is the easiest. The solution about the data step is also very interesting. Thanks again to all involved.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 7 replies
  • 1113 views
  • 7 likes
  • 4 in conversation