Sentry Page Protection
Variable Attributes [11-17]
Understand how SAS keep track of Date and Time
In SAS, each individual date is represented by a unique integer.
SAS date starts at January 1, 1960, which is represented by the integer 0.
The integer 1 represents the date of January 2, 1960, which is 1 day after January 1, 1960.
Similarly, the integer 1000 represents the date of September 27, 1962, which is 1000 days after January 1, 1960.
- 0 = January 1, 1960
- 1 = January 2, 1960
- 1000 = September 27 1962
and - -1000 = April 6, 1957
- 20089 = January 1, 2015
SAS uses this system to keep track of the date, as opposed to storing the actual text (e.g. 01JAN2015 or 2015-01-01) in the system.
We often need to calculate the duration between 2 dates:
Example
- Duration between treatment Date
- Duration between Loan Start and End date
- Duration between marketing campaign start and end date
The calculation of the duration would not be possible if the data is kept as a character value (e.g. 01JAN2015 or 2015-01-01).
Keeping track of the date using the numeric system allows the duration to be calculated. This is often needed in our analysis.
Keeping track of Time value in SAS
Dealing with Time value is quite the same.
In SAS, each time point is represented by a unique integer as well:
- 0 = 0:00:00
- 1 = 0:00:01
- 360 = 0:06:00
- 3600 = 1:00:00
When dealing with the time value, the integer 1 represents 1 second.
The value, 3600, represents 3600 seconds, which is 1 hour (1:00:00).
Date Format
Let's take a look at how the date and time data are stored internally in a data set.
Example
As discussed, SAS uses a numeric system to keep track of the date.
The value 10001 represents 10001 days after January 1, 1960, which is May 20, 1987.
Showing the dates in these 5-digit numbers doesn't make much sense.
What you need to do is to assign a proper date format to the variable so that the data set shows the actual date that the number represents.
Example
Data DFormat2;
Set DFormat;
Format Date YYMMDD10.;
Run;
(YYMMDD10.) is a very common date format. It displays the date as the actual date as opposed to the random 5-digit numbers that SAS uses internally to keep track of the date.
In the next session, you will learn a few more common date and time formats that are commonly used in SAS.
Exercise
The DENAPPT data set contains a set of data related to the current dental visits.
The DENAPPT data set contains a set of data related to the current dental visits.
- Date: Date of current visit
- Time: Time of current visit
- NumDays: The number of days for the next appointment
Find out the dates for the next appointments.
E.g. if the current visit is on 2015-01-01 and the # of days for next appointment is 30, the next visit date should be 2015-01-31.
Create any data set or variable if needed.
Need some help?
HINT:
Since the dates are stored in a numeric variable, you can simply add the NumDays to the date to find out the next appointment. Don't forget to format the new variable, however.
SOLUTION:
Data DenAppt2;
Set DenAppt;
Format NextAppt YYMMDD10.;
NextAppt = Date + NumDays;
Run;
Fill out my online form.