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 Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/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 Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/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 Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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