Hi,
I am trying to go from:
SSN TEST
1 1
2 4
2 2
3 2
TO:
SSN TEST FLAG
1 1 .
2 4 1
2 2 1
3 2 .
So, I want to create a flag that if the test=4 then each flag for that specific person will be 1.
This is what I currently have, but it is not grouping it by person and is continuing down the entire list, which is not what i want.
data want;
set have;
by ssn;
if test=4 then flag=1;
retain flag;
run;
If you know that test = 4 is the first in each group, a simpler single-pass solution could be
data have;
input SSN TEST;
datalines;
1 1
2 4
2 2
3 2
;
data want;
set have;
by ssn;
if first.ssn then flag = .;
if test = 4 then flag = 1;
retain flag;
run;
So if TEST = 4 for any obs in a group, then flag = 1 for all obs in that group?
If so then do
data have;
input SSN TEST;
datalines;
1 1
2 4
2 2
3 2
;
data want;
do _N_ = 1 by 1 until (last.ssn);
set have;
by ssn;
if test = 4 then flag = 1;
end;
do _N_ = 1 to _N_;
set have;
output;
end;
run;
If you know that test = 4 is the first in each group, a simpler single-pass solution could be
data have;
input SSN TEST;
datalines;
1 1
2 4
2 2
3 2
;
data want;
set have;
by ssn;
if first.ssn then flag = .;
if test = 4 then flag = 1;
retain flag;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.