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

Hi,

I have data that look like this.

id date

1 1/1/2001

1 1/1/2001

1 1/2/2002

1 1/2/2002

1 1/2/2003

1 1/2/2003

2 1/1/2005

2 1/1/2005

2 1/1/2005

2 1/2/2005

2 1/2/2005

2 1/3/2006

2 1/3/2006

How do I count by id and then by date? like so

id date        count

1 1/1/2001     1

1 1/1/2001     1

1 1/2/2002     2

1 1/2/2002     2

1 1/2/2003     3

1 1/2/2003     3

2 1/1/2005     1

2 1/1/2005     1

2 1/1/2005     1

2 1/2/2005     2

2 1/2/2005     2

2 1/3/2006     3

2 1/3/2006     3

so it counts every new date within the id but starts over at 1 for every new id? I tried the following but to no avail.

data work.a;

by id;

if first.id then count=0;

if count=0 then do;

fdate=date;

count+1;

end;

edate=date;

diff=edate=fdate;

if diff>0 then count+1;

run;

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Untested - but should work. Using "count+1" implicitly makes count a retained variable.

data work.a;

  by id date;

  if first.id then count=1;

  else if first.date then count+1;

run;

View solution in original post

2 REPLIES 2
Patrick
Opal | Level 21

Untested - but should work. Using "count+1" implicitly makes count a retained variable.

data work.a;

  by id date;

  if first.id then count=1;

  else if first.date then count+1;

run;

saurabhc
Fluorite | Level 6

Hi Patrick,

Thank you. It works. I can now use the properties of count to manipulate the observations.

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
  • 2 replies
  • 15132 views
  • 3 likes
  • 2 in conversation