I have a set of patients who have hospital visits at different ages. Each patient may have hospital visit more than once. I want to flag THOSE INDIVIDUALS(IDs) who have ONLY hospital visits at the age of less than 18. If an individual has hospital visits at age below 18 and above 18, I don’t want to flag them.
For example:
Data I have:
Patient_ID | Age |
1 | 14 |
1 | 16 |
1 | 18 |
2 | 15 |
2 | 17 |
3 | 16 |
4 | 19 |
Data I Want
Patient_ID | Age | Flag |
1 | 14 | 0 |
1 | 16 | 0 |
1 | 18 | 0 |
2 | 15 | 1 |
2 | 17 | 1 |
3 | 16 | 1 |
4 | 19 | 0 |
Thank you very much.
Hi @SR11 Since your dataset seems to be nicely sorted by Patient_id age in ascending order, makes it a straight forward boolean-
data have;
input
Patient_ID
Age ;
cards;
1
14
1
16
1
18
2
15
2
17
3
16
4
19
;
data Want;
do _n_=1 by 1 until(last.patient_id);
set have;
by patient_id;
flag=age<18;
end;
do _n_=1 to _n_;
set have;
output;
end;
run;
proc sql;
create table want as
select * , flag = max(age) < 18
from have
group by patient_id
order by patient_ID, age;
quit;
The logic simplifies to the maximum age for a person must be less than 18. You can use SQL to find the maximum age per ID (group by) and then use it to calculate your flag.
@SR11 wrote:
I have a set of patients who have hospital visits at different ages. Each patient may have hospital visit more than once. I want to flag THOSE INDIVIDUALS(IDs) who have ONLY hospital visits at the age of less than 18. If an individual has hospital visits at age below 18 and above 18, I don’t want to flag them.
For example:
Data I have:
Patient_ID
Age
1
14
1
16
1
18
2
15
2
17
3
16
4
19
Data I Want
Patient_ID
Age
Flag
1
14
0
1
16
0
1
18
0
2
15
1
2
17
1
3
16
1
4
19
0
Thank you very much.
Hi @SR11 Since your dataset seems to be nicely sorted by Patient_id age in ascending order, makes it a straight forward boolean-
data have;
input
Patient_ID
Age ;
cards;
1
14
1
16
1
18
2
15
2
17
3
16
4
19
;
data Want;
do _n_=1 by 1 until(last.patient_id);
set have;
by patient_id;
flag=age<18;
end;
do _n_=1 to _n_;
set have;
output;
end;
run;
And just in case if your age variable isn't sorted-
data Want;
do _n_=1 by 1 until(last.patient_id);
set have;
by patient_id;
flag=flag<>age;
end;
flag=flag<18;
do _n_=1 to _n_;
set have;
output;
end;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.