DATA Step, Macro, Functions and more

Start over count when a new value is reached

Reply
New Contributor
Posts: 2

Start over count when a new value is reached

Hello,

I have the following data:

personID              caretype              date

1                            primary                18000

1                            primary                18001

1                            specialty              18005

1                            primary                18007

1                            specialty              18008

1                            specialty              18009

1                            primary                18020

I would like to create a count variable where the count starts over every time it reaches the caretype value of "primary".  For example, I would like my data to look like the following:

personID              caretype              date                      count

1                            primary                18000                   1

1                            primary                18001                   1

1                            specialty              18005                   2

1                            primary                18007                   1

1                            specialty              18008                   2

1                            specialty              18009                   3

1                            primary                18020                   1

Any ideas on how to do this?

Thank you!

Super Contributor
Posts: 1,636

Re: Start over count when a new value is reached

data have;

input personID              caretype $              date;

cards;

1                            primary                18000

1                            primary                18001

1                            specialty              18005

1                            primary                18007

1                            specialty              18008

1                            specialty              18009

1                            primary                18020

;

data want;

  set have;

  if upcase(caretype)="PRIMARY" then count=1;

    else count+1;

run;

proc print;run;

Occasional Contributor
Posts: 9

Re: Start over count when a new value is reached

I think the retain statement needs to be used to make the count work....

data test;

input personID              caretype $              date;

cards;

1                            primary                18000

1                            primary                18001

1                            specialty              18005

1                            primary                18007

1                            specialty              18008

1                            specialty              18009

1                            primary                18020

;


data test2;
set test;
retain count;

if caretype = "primary" then count = 1;
else count = count + 1;
run;

Super Contributor
Posts: 1,636

Re: Start over count when a new value is reached

you don't have to use retain if you change

count = count + 1;

to

count+1;

New Contributor
Posts: 2

Re: Start over count when a new value is reached

Perfect, thank you!

Ask a Question
Discussion stats
  • 4 replies
  • 260 views
  • 1 like
  • 3 in conversation