BookmarkSubscribeRSS Feed
gonzy31
Calcite | Level 5

I am trying to create a count variable to count the number of patient visits without insurance for each different physician. However, my count variable is not reliably resetting with each new physician (phycode) -- sometimes is and sometimes is not.  Is there a way to fix this?  

 

Here is my code:

proc sort data=IN.AggregateDoc_0514;
by phycode;
run;
Data medicaidpts;
Set IN.AggregateDoc_0514;
keep phycode patcode count insurance;
if insurance=0;
by phycode;
retain count;
count+1;
if first.phycode then count=1;
Run;

 

Here is a bit of the output I keep getting:

PHYCODE PATCODE insurance count

11701
111202
112503
112804
113005
13101
13102
13203
13404
13405
13806
13807
131008
131109
1312010
1313011
1314012
1315013
1315014
1316015
1322016
1322017
1325018
146019
148020
152021
 
 

 

4 REPLIES 4
Reeza
Super User

You don't show what you start with.

 

You may be running into an order of operations issue, ie you're counting before resetting or filtering before resetting. 

 

gonzy31
Calcite | Level 5

Hi! Thanks -- I started with :

 

proc sort data=IN.AggregateDoc_0514;
by phycode;
run;

 

 

Reeza
Super User
The starting data, not your code. We don't know what the input data is compared to the output.
Astounding
PROC Star

You need to add two pieces.  Let SAS know when a new physician begins, and set the count variable at that point.  It's actually not difficult:

 

proc sort data=IN.AggregateDoc_0514;
by phycode;
run;
Data medicaidpts;
Set IN.AggregateDoc_0514;

by phycode;

if first.phycode then count=1;
keep phycode patcode count insurance;
if insurance=0;
by phycode;
retain count;
count+1;

Run;

 

So you were missing the BY statement, and the reference to reset (if first.phycode  then ,...) came too late.  If the first patient for a physician had insurance, your IF statement (if insurance=0) would delete the data for that patient, and never reach the statement that resets COUNT.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 465 views
  • 0 likes
  • 3 in conversation