3 Ways to Create a SAS Date / Time
By : Admin_programmer -
The SAS language uses it’s own time scale. First, let’s get a reminder on SAS date, time and datetime concepts. Then, we will see three options to convert or create these values.
1. Reminder on SAS date, time and datetime
Date: A SAS date is an integer equal to the number of days since the 1st of January 1960. Dates before 1960 are negative values. The 1st of January 1960 is equal to zero. So the difference between two SAS dates is a number of days.
Time: A SAS time is a number representing the number of seconds since midnight. It varies from 0 to 86400 (60 seconds * 60 minutes *24 hours). The difference between two SAS time is a number of seconds.
Datetime: A SAS datetime is a number represent the number of seconds since the 1st of January 1960. This number will have a decimal value if tenth of seconds are used. But it remains the exception. The difference between two SAS datetime is a number of seconds.
Variables which contain date, time or datetime are numeric variables.
Benefit: Numbers require less storage space. Compute with these numbers is easier. In addition, the SAS functions work with those numeric values. For example, the
month function extract the month of a SAS date.
Inconvenient and workaround: these date/time/datetime cannot be interpret straight forward by a human eye. To solve this issue, you can apply a format it or create a character variable using the put function and the format. No further detail is given here on this topic.
2. The difference with Excel
Date: While the 1st janvier 1960 stands for the the SAS date zero, the 1st 1900 is used by Excel one (not zero). To go from an Excel date to a SAS date, we need to add 21916.
sas_date = excel_date * 21916;
Time: Here again, there is a difference between a SAS time and an Excel time. A hour in Excel is a number between 0 and 1. a time in SAS is a number between 0 and 86400 i.e. 60 seconds x 60 minutes x 24 hours. 12pm is the equivalent of 0.5 in Excel (half day through) whereas it is 60 (seconds) x 60 (minutes) x 12 (hours) i.e. 43200 seconds in SAS time. To go from an Excel hour to a SAS hour, you just need to multiply the Excel hour by 86400 (seconds).
sas_time = excel_time * 86400;
There is no negative values in Excel. After one day, the day information is lost: 16 hours + 10 hours = 2am in Excel whereas in SAS language it is 26 hours (60x60x26).
Datetime: Maintenant, combinons les deux solutions pour convertir une valeur datetime d’Excel en datetime SAS.
sas_date_time = (excel_date_time - 21916 ) * 86400;
3. Create a SAS date/time/datetime with
Create a date with the
mdy function: the
mdy function creates a SAS date. In the US, the month appears first in a date in the US follow with the day and month. The same order is used by the three numeric parameters of this function: the month, the day and the year. The name of the function
mdy recalls this order too: M for month, D for day and Y for year.
visit_dt = mdy(visit_m,visit_d,visit_y);
Create a time with
has function: the
hms function returns a SAS time. It includes the hours, minutes and seconds.
visit_hour = hms(vist_hr,visit_min,visit_sec);
Create a datetime with the
ohms function: the
dhms function has 4 paramètres : the SAS date, the number of hours, the number of minutes and the number of seconds.
visit_time = dhms(visit_dt, vist_hr,visit_min,visit_sec);
4. Create a date/time/datetime using the
input function and an informat
We can read a character value and ask the system to interpret it as a SAS date using an informat in an
input function. The first parameter is either a character value into quotes or a character variable. The second parameter is the informat.
date. informat: one of the frequently used display for a date is: the day followed by three letters for the month name in English and the 4-digit year. The total length is of 9 characters. The informat is called
newdate = input('25NOV1952',date9.);
By default, the informat
date. has for value 7 (
date7.) which gives a date with a two-digit year. Be careful with the
year cutoff global option which will either give 1952 or 2052 depending of its setting. Check section 5 for more details on this option.
nov_dt = input('25NOV52',date7.);
time. informat: the
time. informat has by default a value of 8. So
time8. are equivalent. For clarity reason, I tend to add the number in my code.
newhour = input('08:15:00',time8.);
If you don’t have any second, you can use
newhour = input('12:15',time5.);
datetime. informat : the default value for a DATETIME. informat is 18. So datetime. and datetime18. are equivalent.
day_hour = input('25NOV1952:08:15:10',datetime18.);
Identify the current value of
yearcutoff: to find out what is the yearcutoff, go and check the log after running this code :
proc options; run;
Change the year cutoff: to change this value, use the global options staffent and the word
yearcutoff = 1920, then :
- Any year smaller than 20 is interpreted as year 2000: 20xx.
- Any year greater than or equal to 20 will be interpreted as a year 1900 : 19xx.
6. Write manually a few date/time/datetime
A quick way to create a SAS date, if and only if, we have very values to enter, is tu use
dt depending on whether it is a date a time or a datetime.
x_date = '25NOV1952'd; x_time = '08:15:00't; x_datetime = '25NOV1952:08:15:10'dt;