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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

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;
Emma8
Quartz | Level 8
Sorry, my actual date does not consequentially increase and also duplicate dates, so when skipped then still would like to count for the lag, for example, see below (between a start and end date--below start is 01012020 and end is 01182020):
datalines Day_3
01012020 1
01062020 2
01062020 2
01062020 2
01102020 4
01102020 4
01182020 6
;
PeterClemmensen
Tourmaline | Level 20

I don't follow this logic. Why is Day_3 equal o 4 and 6 in the last few obs?

Kurt_Bremser
Super User

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?


 

Emma8
Quartz | Level 8
By the actual calendar date
Emma8
Quartz | Level 8
Although the actual date from 2 January 2020 to 6 January 2020 etc. skipped in the data, I want to keep track from the beginning date to the end date
Kurt_Bremser
Super User

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
Quartz | Level 8
It should count the actual calendar date not by _n_
Kurt_Bremser
Super User

@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.

Kurt_Bremser
Super User

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;

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2529 views
  • 3 likes
  • 3 in conversation