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

 

Hello SAS Community members,

 

I am working on patient data for a work project, & the excerpt below shows some of these data:

 

PatientID

Year_of_Visit

Specialty

1111

2005

Specialist 2

1111

2005

Specialist 2

1111

2005

Specialist 3

1111

2005

Specialist 3

1111

2006

Specialist 2

1111

2006

Specialist 2

1112

2006

Specialist 2

1112

2006

Specialist 2

1112

2007

Specialist 2

1112

2007

Specialist 2

1112

2007

Specialist 6

1112

2007

Specialist 6

1112

2009

Specialist 2

1112

2009

Specialist 2

1112

2009

Specialist 2

1112

2009

Specialist 5

1112

2009

Specialist 6

 

As you can note, the same patient may see many specialists in the same year.

 

I am required to calculate the following:

 

1. Mean of the visits per year

2. Mean of the visits by the specialty type per year

 

Honesty, I have no idea how to do those calculations due to my limited experience in SAS! I looked into related topics but my question was not fitting any!

 

I'd be greatly appreciating your kind inputs!

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Do you want the mean of the count of visits or mean of something else.

 

Proc summary data=have;

   class specialty year;

   output out=work.counts;

run;

 

will create several counts with the variable _type_ indicating which group it belongs to.

_type_= 0 will have the overall count, _type_ = 1 and 2 will have summaries by specialty and year only and _type_=3 will have combinations of specialty and year.

 

Use that output data set to take means of the _freq_ variable by _type_ using any of Proc summary, means, report or tabulate.

 

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Do you want the mean of the count of visits or mean of something else.

 

Proc summary data=have;

   class specialty year;

   output out=work.counts;

run;

 

will create several counts with the variable _type_ indicating which group it belongs to.

_type_= 0 will have the overall count, _type_ = 1 and 2 will have summaries by specialty and year only and _type_=3 will have combinations of specialty and year.

 

Use that output data set to take means of the _freq_ variable by _type_ using any of Proc summary, means, report or tabulate.

 

 

Treeva
Calcite | Level 5

Thanks so much, ballardw!

 

Your suggestion successfully answered my first question.

 

For my second question, I had to run proc means by type & specialty to get the mean of visits per year for each specialty.

 

 

This is how I used your code:

 

Proc summary data=freqVisits;

class Specialty Year_of_Visit;

output out=work.counts;

proc print; run;

 

_type_ = 2 gives results similar to proc freq; tables Specialty;

_type_ = 3 stratifies the frequency of visits to each specialty by year.

 

The following to show how the proc summary statement worked:

Specialty

Year_of_Visit

_TYPE_

_FREQ_

Specialist 2

.

2

77

Specialist 7

.

2

1

Specialist 3

.

2

17

Specialist 2

2003

3

1

Specialist 2

2004

3

1

Specialist 2

2007

3

5

Specialist 2

2008

3

3

Specialist 2

2009

3

5

Specialist 2

2010

3

11

Specialist 2

2011

3

11

Specialist 2

2012

3

10

Specialist 2

2013

3

16

Specialist 2

2016

3

11

Specialist 2

2017

3

3

Specialist 7

2010

3

1

Specialist 3

2010

3

2

Specialist 3

2011

3

1

Specialist 3

2013

3

1

Specialist 3

2014

3

2

Specialist 3

2015

3

11

 

/* To calculate (mean ± SD) of visits per year */

proc means data = counts;

var _freq_ ;

where _type_ = 3;

run; 

 

The result was the following:

Analysis Variable: _FREQ_

N

Mean

Std Dev

Minimum

Maximum

17

5.5882352

4.9378490

1.0000000

16.0000000

 

 

 

/* To calculate (mean ± SD)  of visits per year by specialty type*/

proc means data = counts;

var _freq_ ;

by Specialty ;

where _type_ = 3;

run;

 

The results were the following:

 

Specialty=Specialist 2

Analysis Variable: _FREQ_

N

Mean

Std Dev

Minimum

Maximum

11

7.0000000

5.0000000

1.0000000

16.0000000

 

Specialty = Specialist 7

Analysis Variable: _FREQ_

N

Mean

Std Dev

Minimum

Maximum

1

1.0000000

.

1.0000000

1.0000000

 

Specialty = Specialist 3

Analysis Variable: _FREQ_

N

Mean

Std Dev

Minimum

Maximum

5

3.4000000

4.2778499

1.0000000

11.0000000

 

 

Thanks so much again!

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 2 replies
  • 1333 views
  • 0 likes
  • 2 in conversation