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

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:

clientyearquarterSpecialtySpec22Spec10spec30
12012122200
12012122200
12012130001
22013210020
22013210020

 

 Thanks for your help.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

 

 

View solution in original post

19 REPLIES 19
ballardw
Super User

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.

GKati
Pyrite | Level 9

Hi 

 

 

 

clientyearquarterSpecialtySpec22Spec10spec30
12012122201
22013210020

 

 

Reeza
Super User

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

 

 

GKati
Pyrite | Level 9

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

GKati
Pyrite | Level 9

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?

GKati
Pyrite | Level 9

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:

 

clientyearquarterSpecialtySpec22Spec10spec30
12012122201
22013210020

 

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?

Yegen
Pyrite | Level 9

 

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;

 

GKati
Pyrite | Level 9

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.

Kurt_Bremser
Super User

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?

GKati
Pyrite | Level 9

Astounding
PROC Star

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?

GKati
Pyrite | Level 9

 

 

Ksharp
Super User
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;
GKati
Pyrite | Level 9

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         

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 19 replies
  • 1708 views
  • 1 like
  • 7 in conversation