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
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;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.