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?
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.
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.
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.
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;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.