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.

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