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;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: