I have data with daily date data. I would like to identify 3 days interval (Day_3), see below:
Date Day_3
01012020 1
01022020 1
01032020 1
01042020 2
01052020 2
01062020 2
etc.
Thank you.
Expanding from my previous post:
data have;
input group $ date :mmddyy8.;
format date yymmdd10.;
datalines;
A 01012020
A 01062020
A 01062020
A 01062020
A 01102020
A 01102020
A 01182020
;
data want;
set have;
by group;
retain first;
if first.group
then first = date;
day_3 = int((date-first)/3)+1;
drop first;
run;
If you don't have groups, do the init at _N_ = 1 instead of first.group.
If your data is daily, then it is as simple as
data have;
input Date :mmddyy8.;
format date mmddyy10.;
datalines;
01012020
01022020
01032020
01042020
01052020
01062020
;
data want;
set have;
if mod(_N_, 3) = 1 then day_3 + 1;
run;
I don't follow this logic. Why is Day_3 equal o 4 and 6 in the last few obs?
Because it's in the 4th or 6th 3-day group from the beginning.
@PeterClemmensen wrote:
I don't follow this logic. Why is Day_3 equal o 4 and 6 in the last few obs?
Expanding from my previous post:
data have;
input group $ date :mmddyy8.;
format date yymmdd10.;
datalines;
A 01012020
A 01062020
A 01062020
A 01062020
A 01102020
A 01102020
A 01182020
;
data want;
set have;
by group;
retain first;
if first.group
then first = date;
day_3 = int((date-first)/3)+1;
drop first;
run;
If you don't have groups, do the init at _N_ = 1 instead of first.group.
@Emma8 wrote:
It should count the actual calendar date not by _n_
Sorry for that, edited my post.
Sometimes the behavior of SAS Studio and Safari with regards to copy/pasting drives me crazy.
And if you need to do this for some kind of groups, see here:
data want;
set have;
by group;
if first.group
then do;
day_3 = 1;
count = 0;
end;
else do;
count + 1;
if mod(count,3) = 0 then day_3 + 1;
end;
drop count;
run;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: