BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SR11
Obsidian | Level 7

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. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
Reeza
Super User
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. 

 

 


 

novinosrin
Tourmaline | Level 20

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;

novinosrin
Tourmaline | Level 20

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;

SR11
Obsidian | Level 7
Thank you very much. I am really grateful.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 998 views
  • 1 like
  • 3 in conversation