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 ;

 

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.

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 ;

 

BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
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.
BASUG is hosting free webinars ! Check out our recordings of past webinars: https://www.basug.org/videos. Be sure to subscribe to our email list for notification of future BASUG events.
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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 725 views
  • 7 likes
  • 4 in conversation