0% found this document useful (0 votes)
83 views2 pages

Calculate Date Differences in SAS

The document provides answers to questions about calculating date and time differences in SAS. It recommends using the DHMS() function to combine SAS date and time variables into datetime variables. Then the INTCK() function can calculate differences between datetime values in various units like seconds, minutes, hours, days, weeks, months or years. For calculating ages, it is better to use INTCK() with the unit 'MONTH' instead of 'YEAR' and adjust if the dates have the same month.

Uploaded by

jbreddysas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views2 pages

Calculate Date Differences in SAS

The document provides answers to questions about calculating date and time differences in SAS. It recommends using the DHMS() function to combine SAS date and time variables into datetime variables. Then the INTCK() function can calculate differences between datetime values in various units like seconds, minutes, hours, days, weeks, months or years. For calculating ages, it is better to use INTCK() with the unit 'MONTH' instead of 'YEAR' and adjust if the dates have the same month.

Uploaded by

jbreddysas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question Answer

I have pairs of SAS date and time You can use the DHMS() function to combine SAS date and time
variables in a table, but I want to variables into a SAS datetime variable. Note that SAS time
calculate the differences between them. variables actually contain the number of seconds elapsed since
Is there an easy way to do this? midnight, so you don't need to split up the time into hours,
minutes and seconds first for DHMS(). The INTCK() function can
then be used to calculate the difference between the 2 SAS
datetime values:
DATA _null_;
INFORMAT date1 date2 DATE9.
time1 time2 TIME5.
unit $10.;
FORMAT dt1 dt2 DATETIME.;
INPUT date1 time1 date2 time2 unit;
dt1=DHMS(date1,0,0,time1);
dt2=DHMS(date2,0,0,time2);
difference=INTCK(unit,dt1,dt2);
PUT unit= dt1= dt2= difference=;
DATALINES;
21mar2003 21:05 22mar2003 00:30 SECOND
21mar2003 21:05 22mar2003 00:30 MINUTE
21mar2003 21:05 22mar2003 00:30 HOUR
21mar2003 21:05 24mar2003 10:25 DTDAY
21mar2003 21:05 22may2003 10:25 DTWEEK
21mar2003 21:05 22oct2004 10:25 DTMONTH
21mar2003 21:05 22jan2009 10:25 DTYEAR
;
RUN;
Warning: INTCK() returns the number of unit boundaries crossed,
e.g. for HOUR, 21:59 to 22:01 = 1, but 21:01 to 21:59 = 0,
because the former example crosses 22:00, but the latter has both
values within the same hour.
How can I calculate the week of the year You can simulate a WEEK function by using the SAS functions
from a SAS date? INTCK and INTNX, e.g.:
DATA getweek;
datevar=TODAY();
week=INTCK('WEEK',
INTNX('YEAR',datevar,0),
datevar)+1;
RUN;
I have a SAS table containing historical It is not possible to directly use monthly dates in DO...END loops,
information in a single record: as each months haa a varying number of days. However, you can
id startdate1 enddate1 still generate successive months by using the number of months
startdate2 enddate2
relative to the startdate1 value using the INTCK and INTNX
001 01Jan2000 15Apr2002
28Jun2000 16Sep2001 functions. The additional information can be calculated by
002 01Feb2000 07Feb2002 comparing the values of the 1st day of the same month for each
19Jul2000 04Oct2000 date:
How do I convert this data to multiple DATA monthly (KEEP=id date month flag2);
monthly summary records between the SET history;
month_count=INTCK('MONTH',startdate1,enddate1);
startdate1 and enddate1 values, and startmonth2=INTNX('MONTH',startdate2,0);
include extra information from endmonth2=INTNX('MONTH',enddate2,0);
DO month=0 TO month_count;
startdate2 and enddate2.
date=INTNX('MONTH',startdate1,month);
IF startmonth2 LE date LE endmonth2
THEN flag2=1;
ELSE flag2=0;
OUTPUT;
END;
FORMAT date MONYY7.;
RUN;
I want to select the previous 5 weekdays Probably the easiest way is to generate the previous weekdays,
using an IN clause containing Julian date and then store the Julian date values in a SAS macro variable, e.g.:
values, but I am forced to hardcode the DATA _null_;
LENGTH datestr $42;
values:
IF procdate IN (1997204,1997205, datestr='(';
1997206,1997209, DO d=(TODAY()-1) TO (TODAY()-7);
1997210) IF (1 LT WEEKDAY(d) LT 7)
THEN DO; THEN datestr=TRIM(datestr) !!
......etc PUT(d,JULIAN7.) !!
',';
END;
Is there a way to automate this? SUBSTR(datestr,LEN(TRIM(datestr)),1)=')';
CALL SYMPUT('datestr',TRIM(datestr));
STOP;
RUN;

The macro variable can then be used to replace your list of dates,
e.g.:

IF procdate IN &datestr
THEN DO;
......etc
When I use You should avoid using the INTCK function with 'YEAR' to
INTCK('YEAR',birthday,TODAY()) to calculate age, as it returns the number of times January 1st occurs
calculate someone's age it doesn't always between the 2 dates. Using 'MONTH' instead of 'YEAR' will give
give the correct answer. Am I doing the number of 1st of the month days between the 2 dates, but this
something wrong? can be adjusted by comparing the day of the month of each date,
e.g.:
DATA getage;
birthday='25Dec1972'd;
datevar=TODAY();
/* Divide months by 12 to get years */
age=INT(INTCK('MONTH',birthday,datevar)/12);
/* Adjust age if the 2 dates have the same month */
/* and the birthday is still to come in this month
*/
IF MONTH(birthday)=MONTH(datevar) THEN
age=age-(DAY(birthday)>DAY(datevar));
RUN;

You might also like