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

Hi all, 

I am looking to create a new count variable in the data set using ID and Date variables. Basically I want to count same ID with same Dates as 1 and same IDs with different Date as 2, 3 , etc. based on number of different dates each ID has. I would really appreciate your help! 

 

Thank you so much in advance!

 

Here is what I want for the count variable:

ID     Date                        Count

1      3/2/2020                     1

1      3/2/2020                     1

1      3/2/2020                     1

2      3/3/2020                     1

2      3/4/2020                     2

2      3/5/2020                     3

3     3/2/2020                      1

4     3/3/2020                     1

4     3/3/2020                     1

4     3/4/2020                     2

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have;
input ID     Date   :mmddyy10.;*                     Count;
format date mmddyy10.;
cards;
1      3/2/2020                     1

1      3/2/2020                     1

1      3/2/2020                     1

2      3/3/2020                     1

2      3/4/2020                     2

2      3/5/2020                     3

3     3/2/2020                      1

4     3/3/2020                     1

4     3/3/2020                     1

4     3/4/2020                     2
;

data want;
 set have;
 by id date;
 if first.id then count=1;
 else if first.date then count+1;
run;

View solution in original post

1 REPLY 1
novinosrin
Tourmaline | Level 20
data have;
input ID     Date   :mmddyy10.;*                     Count;
format date mmddyy10.;
cards;
1      3/2/2020                     1

1      3/2/2020                     1

1      3/2/2020                     1

2      3/3/2020                     1

2      3/4/2020                     2

2      3/5/2020                     3

3     3/2/2020                      1

4     3/3/2020                     1

4     3/3/2020                     1

4     3/4/2020                     2
;

data want;
 set have;
 by id date;
 if first.id then count=1;
 else if first.date then count+1;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 2242 views
  • 0 likes
  • 2 in conversation