## counting patient visits by patientid and age

Solved
Regular Contributor
Posts: 182

# counting patient visits by patientid and age

Hi!  I need to create a count visits by patient and age.  I have attempted with the following syntax, but age is not counting correctly

data util_sum;
set asthma.household_female;
by patientid patage;
if first.patientid=1 then N_util=0;
if utilization=1 then N_util +1;
if last.patientid=1 then output;
keep patientid patage n_util;
run;

I also tried using proc means, but age is still not counting correctly.

proc means sum data=utilization
by patientid;
class patage;
var utilization;
output out=var_sum sum=;
run;

Help is greatly appreciated!!

Accepted Solutions
Solution
‎05-23-2016 03:02 PM
Super User
Posts: 6,762

## Re: counting patient visits by patientid and age

[ Edited ]

What is the definition of a "visit"?  Is it any instance of utilization=1?

If so, your DATA step is very close.  You refer to:

first.patientid

last.patientid

Instead, you should be referring to:

first.patage

last.patage

The PROC MEANS approach could work (assuming that you add the missing semicolon).  But it would be safer to add a WHERE statement:

where utilization=1;

On the one hand, that would protect against suspect values for UTILIZATION such as 2 or -1.  On the other hand, that would lead to further complications.  You would no longer get totals of 0 as part of the output.  It's probably safer to stick with the DATA step.

All Replies
Super User
Posts: 13,523

## Re: counting patient visits by patientid and age

Please describe how "age is still not counting correctly".

You may need to decide which AGE to use for a count as your patients age increases between visits. So do you want

1) AGE at each visit and count each visit

2) Count age only at first visit

3) Count age only at latest visit

or something else?

Provide some example data and the desired result for that example.

Solution
‎05-23-2016 03:02 PM
Super User
Posts: 6,762

## Re: counting patient visits by patientid and age

[ Edited ]

What is the definition of a "visit"?  Is it any instance of utilization=1?

If so, your DATA step is very close.  You refer to:

first.patientid

last.patientid

Instead, you should be referring to:

first.patage

last.patage

The PROC MEANS approach could work (assuming that you add the missing semicolon).  But it would be safer to add a WHERE statement:

where utilization=1;

On the one hand, that would protect against suspect values for UTILIZATION such as 2 or -1.  On the other hand, that would lead to further complications.  You would no longer get totals of 0 as part of the output.  It's probably safer to stick with the DATA step.

🔒 This topic is solved and locked.