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

I have a feeling that I might be missing something simple, but I am having a difficult time figuring this out.

I have a dataset that looks something like this and what I am trying to do is create a new variable that is true if any of Var1 is true by each ID.  For example I am for this new variable to be true for all 11 rows and 12 rows. I have been trying to make this work using a do loop, but have not been able to quite get that to work.

 

Data example

input ID Var1

Datalines;

11 1

11 0

11 0

11 0

12 1

12 1

12 1

12 0

13 0

13 0

14 0

;

RUN;

 

Essentially I am looking for an output that looks a bit like this

 

 

Data goal

input ID Var1 group

Datalines;

11 1 1

11 0 1

11 0 1

11 0 1

12 1 1

12 1 1

12 1 1

12 0 1

13 0 0

13 0 0

14 0 0

;

RUN;

 

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The MAX() of a set of boolean values is true when ANY of them is true.

proc sql;
create goal as
select * , max(var1) as group
from example
group by id
;
quit;

The MAX() of a set of boolean values is false when ALL of them are false.

The MIN() of a set of boolean values is true when ALL of them are true.

The MIN() of a set of boolean values is false when ANY of them are false.

The SUM() of a set of boolean values counts how many of them that are true.

The MEAN() of a set of boolean values is the percent of them that are true.

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

The MAX() of a set of boolean values is true when ANY of them is true.

proc sql;
create goal as
select * , max(var1) as group
from example
group by id
;
quit;

The MAX() of a set of boolean values is false when ALL of them are false.

The MIN() of a set of boolean values is true when ALL of them are true.

The MIN() of a set of boolean values is false when ANY of them are false.

The SUM() of a set of boolean values counts how many of them that are true.

The MEAN() of a set of boolean values is the percent of them that are true.

ballardw
Super User

And RANGE = 0 tells you that all the variables have the same value or missing. May all be true or all false but all the same.

smoore3790
Fluorite | Level 6
Thanks so much for the help this worked perfectly!
PeterClemmensen
Tourmaline | Level 20

Here is a do(w) loop approach

 

Data example;
input ID Var1;
Datalines;
11 1
11 0
11 0
11 0
12 1
12 1
12 1
12 0
13 0
13 0
14 0
;

data goal;
   _iorc_ = 0;
   do until (last.ID);
      set example;
      by ID;
      if Var1 then _iorc_ = 1;
   end;

   do until (last.ID);
      set example;
      by ID;
      group = _iorc_;
      output;
   end;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1374 views
  • 1 like
  • 4 in conversation