## counting patient visits with two by groups

Regular Contributor
Posts: 182

# counting patient visits with two by groups

Hi!  I am interested in ouputing a sum value to a new variable based on a condition.

1. I have a table with 100 observations-  patientid with all their encounters.

2.  I want to sum all married patients with ER visit in each year 2012- 2014 and output that sum by year to the last.patientid of each year.

3. The end table should still have 100 observations...just with two new sum variables.

I've tried, but my table keeps reducing to one patientid with ER sum or the table stays at 100 with the ER sum on each observation line.

This is the last code i tried, but not correct.  Not  sure where to insert output statement (not correct here!) or if this type of do statement is possible??

DATA ER1_sum;
set var;
by patientid;
where  Marital_status='Married';
if ER1=1 then do;
if year='2012' then do;
if first.patientid=1 then n_ER12=0;
n_ER12 +1;
if last.patientid=1 then output;
end;
if year='2013' then do;
if first.patientid=1 then n_ER13=0;
n_ER13 +1;
if last.patientid=1 then output;

end;

if year='2014' then do;
if first.patientid=1 then n_ER14=0;
n_ER14 +1;
if last.patientid=1 then output;

end;

end;
run;

Super User
Posts: 13,500

## Re: counting patient visits with two by groups

Provide some example input and what you want for output for that input.

And are your sure that you need a dataset? This sounds like it should just be a report.

I also think that you mean to count your patients as it is very difficult to "sum all married patients".

Regular Contributor
Posts: 182

## Re: counting patient visits with two by groups

Yes, count is correct.  I think I need a data set because i will need to build graphs later.  I think I can take out the marital status and just count the ER visits by year.  I can add a conditional statement later..

data for ER l is binary...if there was an ER visit the ER=1.

ID 3. ER \$ 1. Year \$ 4.

000  0  2012

000  1  2014

001  1  2012

001  1  2012

001  0  2013

001  1  2013

002  1  2012

002  1  2014

002  1  2014

Want to look like this...

ID 3. ER \$ 1. Year \$ 4.  Marital \$ 1. ER_12 1.  ER_13 1.  ER_14  1.

000  0  2012 1 . .

000  1  2014  . . 1

001  1  2012  . . .

001  1  2012  . . .

001  1  2012 3 . .

001  0  2013  . . .

001  1  2013 .1 .

002  1  2012 1. .

002  1  2014 . . .

002  1  2014 . . 2

Discussion stats
• 2 replies
• 207 views
• 0 likes
• 2 in conversation