## Number of Claims by quarter of year

Solved
Frequent Contributor
Posts: 107

# Number of Claims by quarter of year

[ Edited ]

Hello Everyone,

I have a large dataset of health insurance claims of the following structure:

data claims;

input client year quarter specialty;

datalines;

1 2012 1 22

1 2012 1 22

1 2012 1 30

2 2013 2 10

2 2013 2 10

;

run;

I would like to:

1. Create variables for each specialty (1 to 90) named spec1, spec 2, etc.

2. Place the sum of claims per quarter of year and client from that specialty in the approrpriate variable cell.

My desired output would look something like this:

 client year quarter Specialty Spec22 Spec10 spec30 1 2012 1 22 2 0 0 1 2012 1 22 2 0 0 1 2012 1 30 0 0 1 2 2013 2 10 0 2 0 2 2013 2 10 0 2 0

Accepted Solutions
Solution
‎06-09-2017 03:23 PM
Super User
Posts: 23,683

## Re: Number of Claims by quarter of year

Use your favourite summary proc (proc means/univariate/summary/sql) to summarize to quarter and then use PROC TRANSPOSE to reformat the data.

All Replies
Super User
Posts: 13,508

## Re: Number of Claims by quarter of year

Is this for a report? If not what exactly will you do with the resulting data? For many purposes changing data to a "wide" format as you requiest complicates a large number of further operations.

Frequent Contributor
Posts: 107

## Re: Number of Claims by quarter of year

[ Edited ]

Hi

 client year quarter Specialty Spec22 Spec10 spec30 1 2012 1 22 2 0 1 2 2013 2 10 0 2 0

Solution
‎06-09-2017 03:23 PM
Super User
Posts: 23,683

## Re: Number of Claims by quarter of year

Use your favourite summary proc (proc means/univariate/summary/sql) to summarize to quarter and then use PROC TRANSPOSE to reformat the data.

Frequent Contributor
Posts: 107

## Re: Number of Claims by quarter of year

I didn't think of that. Thanks for the tip.

Frequent Contributor
Posts: 107

## Re: Number of Claims by quarter of year

Hello

I am trying your method with proc transpose. I need to see the number of claims per CLIENT by specialty and quarter of year. I have about 4 million claims from probably a few million different clients, multiplied by the number of periods.

I have this so far, but I somehow doubt that this is going to work for SAS:

PROC tabulate DATA=work.claims out=claims_out;

CLASS client year quarter specialty;

VAR specialty;

tables client*year*quarter*specialty, N;

title "Claims by Specialty and quarter";

run;

Any suggestions?

Frequent Contributor
Posts: 107

## Summarize clients by quarter of year and specialty

Hello Everyone,

I have a large dataset of health insurance claims of the following structure:

data claims;

input client year quarter specialty;

datalines;

1 2012 1 22

1 2012 1 22

1 2012 1 30

2 2013 2 10

2 2013 2 10

;

run;

I would like to summarize the number of claims per CLIENT by specialty and quarter of years. In other words, I would like to collapse my data by quarters.

My desired output would look something like this:

 client year quarter Specialty Spec22 Spec10 spec30 1 2012 1 22 2 0 1 2 2013 2 10 0 2 0

Reeza on this forum suggested using proc tabulate (or something similar) and then proc transpose. But my problem is that I have 4.5 million claims from milions of clients. And this seems to be too much for SAS to write out.

This is what I've tried so far:

PROC tabulate DATA=work.claims out=claims_out;

CLASS client year quarter;

VAR specialty;

tables client*year*quarter*specialty, N;

title "Claims by Specialty and quarter";

run;

Any suggestions?

Frequent Contributor
Posts: 119

## Re: Summarize clients by quarter of year and specialty

The following code yields the desired outcome:

``````proc sql;
create table work.claims_out as
select *,
sum(Case when specialty=22 then 1 else 0 end) as Spec22,
sum(Case when specialty=10 then 1 else 0 end) as Spec10,
sum(Case when specialty=30 then 1 else 0 end) as Spec30
from work.claims
group by client, year, quarter;
quit;

proc sort data=work.claims_out nodupkey;
by client year quarter;
quit;``````

Frequent Contributor
Posts: 107

## Re: Summarize clients by quarter of year and specialty

Thanks Yegen. In other words, this is not possible with Proc Tabulate...

One problem I have with this code, is that I actually have 90 specialties. Is there a way I don't have to manually generate 90 Spec variables?

Thanks again.

Super User
Posts: 10,214

## Re: Summarize clients by quarter of year and specialty

Sort by client, year, quarter and speciality.

Do a proc summary with the same by statement.

Determine the needed columns with a proc sort nodupkey by speciality.

Write the data step that does the transpose (or create it dynamically with call execute from the proc sort result).

How many "specialities" are there, anyway?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 107

Super User
Posts: 6,754

## Re: Summarize clients by quarter of year and specialty

You should be able to get closer to your desired output with a few changes:

PROC tabulate DATA=work.claims out=claims_out;

CLASS client year quarter specialty;

tables client*year*quarter, specialty;

title "Claims by Specialty and quarter";

run;

This is untested code, so may need some tweaking.

This still won't get you SPECIALTY column you indicate, however.  But your logic for obtaining it is not clear.  Should it be the first specialty within that Client / Year  / Quarter  The most frequently occurring specialty?  Should it be removed entirely?

Frequent Contributor
Posts: 107

Super User
Posts: 10,766

## Re: Summarize clients by quarter of year and specialty

``````data claims;
input client year quarter specialty;
datalines;
1 2012 1 22
1 2012 1 22
1 2012 1 30
2 2013 2 10
2 2013 2 10
;
run;
proc freq data=claims noprint;
table client*year*quarter*specialty/out=have nopercent nocum ;
run;
proc sql noprint;
select distinct cats('spec',specialty) into : list separated by ' '
from have;
quit;
data want;
set have;
by client year quarter;
array x{*} &list;
retain &list;

if first.quarter then do;
do i=1 to dim(x);
x{i}=0;
end;
end;

do i=1 to dim(x);
if cats('spec',specialty)=vname(x{i}) then do;
x{i}=count;leave;
end;
end;

if last.quarter;
drop i specialty count percent;
run;``````
Frequent Contributor
Posts: 107

## Re: Summarize clients by quarter of year and specialty

Thanks for your comment Ksharp. I get the following error message.

Data want;

43          set have;

44          by client year quarter;

45          array x{*} &list;

NOTE: Line generated by the macro variable "LIST".

45          spec. spec0 spec1 spec10 spec11 spec13 spec16 spec17 spec18 spec2 spec20 spec21 spec22 spec23 spec24 spec26 spec27

_____

22

201

45       ! spec28 spec29 spec3 spec30 spec35 spec37 spec4 spec5 spec6 spec60 spec61 spec62 spec7 spec8 spec89 spec90

ERROR 22-322: Syntax error, expecting one of the following: a name, a numeric constant, \$, (, ;, _ALL_, _CHARACTER_, _CHAR_,

_NUMERIC_, _TEMPORARY_.

ERROR 201-322: The option is not recognized and will be ignored.

46          retain &list;

NOTE: Line generated by the macro variable "LIST".

46          spec. spec0 spec1 spec10 spec11 spec13 spec16 spec17 spec18 spec2 spec20 spec21 spec22 spec23 spec24 spec26 spec27

_____

22

201

46       ! spec28 spec29 spec3 spec30 spec35 spec37 spec4 spec5 spec6 spec60 spec61 spec62 spec7 spec8 spec89 spec90

ERROR 22-322: Syntax error, expecting one of the following: a name, ;, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.

ERROR 201-322: The option is not recognized and will be ignored.

47

48         if first.quarter then do;

49          do i=1 to dim(x);

50           x{i}=0;

51          end;

52         end;

53          do i=1 to dim(x);

54           if cats('spec',specialty)=vname(x{i}) then do;

55             x{i}=count;leave;

56           end;

57          end;

58

☑ This topic is solved.