You already know how to use SAS’s built-in formats to improve the readability of dates in SAS output.
However, before wrapping up our discussion of labels and formats,
I wanted to give you some instruction on some of the special circumstances you may encounter when working with dates.
If you want to follow along, and I suggest that you do,
we will be using the hospital survey data for all of the examples in this lecture.
Before getting started, please make sure to call up the formats for the hospital survey data.
If you aren’t sure what I’m talking about, please go back and watch the previous videos about labels and formats.
In this first example, we want to calculate a participant’s exact age in years from their date of birth.
SAS has a convenient way to do this.
Anytime we create a new variable, we will do so in a DATA step.
In this case we are creating a calculated variable.
That is, we aren’t entering or importing the values for the variable directly.
The values will be determined based on some calculation or function performed on an existing variable or set of variables.
As you can see here, we aren’t entering values for age.
We are calculating age by feeding SAS two existing variables, “dob” and “date”, and telling SAS to perform the YRDIF function.
The general form of the YRDIF function is the SAS keyword YRDIF,
followed by parentheses. Inside the parentheses are three positions, separated by commas.
In the first position, you should type the name of the variable that contains the start date for your calculation of interest.
In the second position, you should type the name of the end date for your calculation of interest.
And finally, in the third position, in quotes, is a keyword that tells SAS how to calculate the date difference.
So, we would read this as age (our new variable) equals the difference in years between the variables date of birth and date.
The third part of the function where you see the word ACTUAL in quotes,
tells SAS to calculate the actual number of days between dates in calculating the number of years.
For more information about this, see the link to “SAS functions and call routines” in the online SAS documentation.
Now let’s submit the PRINT procedure for the variables dob and age, and see how they look.
And it worked.
We now know each participant’s exact age, down to four decimal places) on the day they took the survey.
But what if we don’t want the age down to 4 decimal places?
Suppose we just want an integer value for age.
Here we have the exact same code as above, but this time we put the YRDIF function inside the integer function.
This should give us the participant’s age as of their last birthday before the survey date.
Let’s run it and PROC PRINT date of birth, exact age, and age at last birthday.
And it works, but what if we decide that we prefer to have their age rounded to the nearest integer value for year,
instead of age at last birthday?
Here we again have the same code, except this time we have the YRDIF function inside the ROUND function.
Let’s run it again and take a look.
And there you have it.
Three different ways to calculate and display age from date of birth.
The form you choose will depend on your needs.
Another question, what if we want to know their age, as of a certain day, for example January 1st, 2013?
Well you could create a new variable, and make the value for that variable January 1st 2013 in every observation.
Then you would just run the code we have already been using,
Except that new variable would occupy the second position in the YRDIF function.
An alternative way to accomplish this task is to type the date of interest in the second position in the format:
open quote, two-digit day, three letter abbreviation for month, four-digit year, closed quote, and followed by a letter “d”.
Sometimes, that day you are interested in knowing their age at, may be today.
Well, to do that, just plug the word “today” with an empty set of parentheses into the second position.
Now suppose you want to restrict the data to people born in the month of March,
or people born on the 3rd day of the week,
or people born in a certain year.
To do that, you may want to create a new variable for each participant that identifies just the month the were born
in, or just the year they were born in.
Well SAS also has functions for extracting these parts of dates.
Here we have four functions that will calculate variables from a date for the day of the week (1-7),
day of the month (1-31),
month of the year (1-12),
and the four-digit year.
Before we run this, I want you to notice something new in the VAR list of the PROC PRINT statement.
Here we are using the double dash form of a variable list.
This VAR statement includes all of the variables from Day of week through Year in the order they are stored in the SAS data set.
So, participant #1 was born on a Wednesday.
I know this because the WEEKDAY function we used returns a value of 1 for Sunday, 2 for Monday, and so on.
We also have a variable showing that participant #1 was born on the 9th day of the month,
in the seventh month of the year (July),
and in the year 1969.
We can now use any of these variables to sort or subset our data.
For example, let’s say that we wanted to sort our participants by year of birth.
Pay attention here.
Many procedures we execute in SAS will require you to sort your data first.
We start with PROC and the keyword SORT,
and point SAS to the data set we want to sort.
On the next line we type a by statement.
Many procedures in SAS use a by statement.
Here we are telling SAS that we want the data sorted by year.
And of course a run statement.
And finally, what if your given a data set where the date was recorded in separate variables representing
day, month, and year, and you want to combine these into one date variable.
As you have probably guessed, we can do that too.
Here we are using the MDY function to calculate the variable dob2.
In the first position is the variable containing values for month.
In the second position is the variable containing values for day.
And in the third position is the variable containing values for year.
Notice that the appearance of dob and dob2 differ because they have different formats.
Dob is formatted with the built-in SAS format called “date9.”
And dob2 is formatted with the built-in SAS format called mmddyy10.
In fact, SAS has many different built-in date formats.
This table displays just a few, and shows how they affect stored values.
Let’s start by looking at the MMDDYY formats.
These formats display dates as a numeric month, day, and year.
The number at the end of the format sets the total width of the value to be displayed, including any characters.
Starting from the top, you can see that MMDDYY6 displays the two-digit month,
two-digit day, and two-digit year without any forward slashes.
In MMDDYY8, the increased width allows for the addition of two forward slashes.
In MMDDYY10, the width of 10 allows for two forward slashes and a four-digit year to be displayed.
The YYMMDD10 format is similar except that it displays the four-digit year first,
and separates the values with a dash instead of a forward slash.
Finally, as you’ve already seen in a previous video, the DATE9 format displays the two-digit day,
followed by the three-letter abbreviation for month, and the four-digit year.
For a full list of available date formats, please see SAS’s online documentation.
And that concludes the lecture on working with dates.
Keep in mind that, although most of our examples were in reference to age,
these functions can be useful for dates of all kinds.