I should calculate the interval between injections. My data is like this below.
ID | date of injection |
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 |
The results should be like this: Also should have interval (week)
ID | interval (days) |
A | 42 |
A | 47 |
A | 90 |
A | 28 |
B | 43 |
B | 45 |
B | 85 |
C | 36 |
C | 42 |
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.
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.
Thanks a lot. It was just an example.
@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.
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
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
I really appreciate
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.
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.