BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Agha
Calcite | Level 5

I should calculate the interval between injections. My data is like this below.

IDdate of injection
A12/1/2015
A1/19/2016
A4/19/2016
A5/17/2016
B12/17/2019
B2/3/2020
B3/23/2020
B6/15/2020
C9/10/2015
C10/16/2015
C12/23/2015

 

The results should be like this: Also should have interval (week)

IDinterval (days)
A42
A47
A90
A28
B43
B45
B85
C36
C42
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id;
    prev_date=lag(date_of_injection);
    if not first.id then do;
        interval=date_of_injection-prev_date;
        output;
    end;
    drop prev_date;
run;

 

I don't know how you managed to get four intervals for A. When there are four dates for A, there can be only three intervals.

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26
data want;
    set have;
    by id;
    prev_date=lag(date_of_injection);
    if not first.id then do;
        interval=date_of_injection-prev_date;
        output;
    end;
    drop prev_date;
run;

 

I don't know how you managed to get four intervals for A. When there are four dates for A, there can be only three intervals.

--
Paige Miller
Agha
Calcite | Level 5

Thanks a lot. It was just an example.

PaigeMiller
Diamond | Level 26

@Agha wrote:

Thanks a lot. It was just an example.


From now on, please give the correct results for your example, as best you can. Making up numbers and making up rows is not a good thing to do.

--
Paige Miller
SteveDenham
Jade | Level 19

I hope the results table you want is just an example of the format, as the Interval values do not match the days.  As an example, look at the last 2 values for ID = C.  16 OCT to 23 DEC = remaining days in October + all days in November + days prior to injection in December, which is 15 + 30 +22 =67, not the 42 in the table. 

 

Other folks following this will be able to tell you how to convert data in date format to SAS date variables, which can be manipulated by standard arithmetic.

 

SteveDenham

SteveDenham
Jade | Level 19

Blatantly stealing @PaigeMiller 's code, here is what I have:

 

data have;
input id $ date_of_injection $10.;
sasdate=input(date_of_injection,mmddyy10.);       
datalines;
A 12/1/2015
A 1/19/2016
A 4/19/2016
A 5/17/2016
B 12/17/2019
B 2/3/2020
B 3/23/2020
B 6/15/2020
C 9/10/2015
C 10/16/2015
C 12/23/2015
;
run;

data want;
    set have;
    by id;
    prev_date=lag(sasdate);
    if not first.id then do;
        interval=sasdate - prev_date;
        output;
    end;
    drop prev_date;
run;

Which gives output that looks like:

 

id date_of_injection sasdate interval 
A 1/19/2016          20472   49 
A 4/19/2016          20563   91 
A 5/17/2016          20591   28 
B 2/3/2020           21948   48 
B 3/23/2020          21997   49 
B 6/15/2020          22081   84 
C 10/16/2015         20377   36 
C 12/23/2015         20445   68 

 Interval here is the number of days since the previous injection.  Week can be calculated as week=floor(interval/7), or whatever function you want to get the week.

 

SteveDenham

 

 

Agha
Calcite | Level 5

I really appreciate

 

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1072 views
  • 2 likes
  • 3 in conversation