BookmarkSubscribeRSS Feed
tbick
Calcite | Level 5

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!

4 REPLIES 4
Linlin
Lapis Lazuli | Level 10

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;

PaulLee
Fluorite | Level 6

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;

Linlin
Lapis Lazuli | Level 10

you don't have to use retain if you change

count = count + 1;

to

count+1;

tbick
Calcite | Level 5

Perfect, thank you!

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
  • 1109 views
  • 1 like
  • 3 in conversation