If you work with dates in SAS, it’s sometimes necessary to extract the month of a date to, for example, count the number of observations per month. In this article, we discuss how to extract the week, month, year, etc. in SAS from a Date variable. Also, we show how to get the hour, minute, and second from a Date Time variable. We support all the functions we discuss in this article with examples and SAS code.
Contents
Extract the Week, Month, and Year from a Date
Before we show how to extract the week, month, or year from a SAS date variable, we create a data set with dates first. We use a SAS data step and the datalines statement to create a data set with 3 dates, namely 08-Jan-2019, 26-Jun-2020, and 05-Dec-2020.
data work.dates; format my_date date9.; input my_date date9.; datalines; 08JAN2019 26JUN2020 05DEC2020 ; run;
SAS provides a range of easy and intuitive build-in functions to extract a specific part from a date variable. The following functions exist:
- year(): to get the year of a Date variable.
- qtr(): to get the quarter of a Date variable.
- month(): to get the month of a Date variable.
- week(): to get the week of a Date variable.
- day(): to get the day of a Date variable.
Remember, all these functions need a SAS date variable as an argument. If you work with a Date Time variable, then scroll down for examples of these functions with Date Time variables.
In the SAS code below, we apply all the SAS functions mentioned above. The image shows the result of this code. In contract to the qtr(), month(), and week() functions that extract the quarter, month, and week of the year of the SAS date variable, the day() function returns the day of the month of the SAS date variable.
data work.dates_extract; set work.dates; my_year = year(my_date); my_quarter = qtr(my_date); my_month = month(my_date); my_week = week(my_date); my_day = day(my_date); run;
Special Extractions from a Date
Although the year(), month(), and week() are useful, there exist situations where these functions don’t provide a solution to your problem. Here we discuss some of them.
Extract the Month and Day as a Text String
All the functions mentioned above return numbers, that is to say, month() returns a 1 for January, a 2 for February, etc. However, if you need a string with the name of the month, i.e., “January” or “February”, you can use the put statement with the monName-format.
my_month_txt = put(my_date, monName.)
If you need the name of the day, e.g., “Monday”, “Tuesday”, etc., you use the put statement in combination with the dowName-format.
my_weekday_txt = put(my_date, dowName.)
The image below shows the result of the SAS code. Note that the variables created by the put function (my_month_txt and my_weekday_txt) have, in some cases, leading blanks. To left-align all values in these columns, you can use the strip function.
Extract the Weekday of a SAS Date
With the weekday() function you can extract the number that corresponds to the day of the week. Note that in SAS the week starts on Sunday. For this reason, Tuesday is day 3 of the week, and Friday the 6th day of the week. Depending on your preferences you extract the weekday from a SAS date with the following code:
my_weekday_num = weekday(my_date) /* week starts on Sunday */
my_weekday_num = weekday(my_date) – 1 /* week starts on Monday */
Extract the Day of the Year in SAS
As mentioned before, the day() function returns a 1 if the SAS date is the first day of the month, a 2 is the SAS date is the second date of the month, etc. However, if you need to know which day a SAS date is in a given year, you can use the following code:
my_day_of_year = my_date-intnx(“year”, my_date, 0, “b”)+1
As you probably know, dates in SAS are stored as numbers. For example, 1-Jan-1960 is day 0 and 4-Oct-2020 is day 22015. With the INTNX function, you can calculate the numeric representation of the first day of the year. If you know this, you can easily calculate how many days have been passed since the beginning of the year.
data work.dates_extract_special; set work.dates; my_month_txt = put(my_date, monName.); my_weekday_txt = put(my_date, dowName.); my_weekday_num = weekday(my_date); my_day_of_year = my_date-intnx("year", my_date, 0, "b")+1; run;
Extract the Week, Month, and Year from a Datetime
Until now, we have only shown how to extract the week, month, year, etc. from a SAS Date variable. However, it’s also very common to work with Datetime variables in SAS. For the examples that follow, we use the following Datetime variables.
data work.datetime; format my_datetime datetime20.; input my_datetime datetime20.; datalines; 12JAN2019:03:40:20 28AUG2020:18:13:44 04NOV2020:22:57:38 ; run;
You can easily extract the month, year, etc. from a Datetime variable using the same functions as discussed before. However, you need to convert the Datetime variable to a Date variable fist. You can do this with the datepart-function.
- year(datepart()): to get the year of a Datetime variable.
- qtr(datepart()): to get the quarter of a Datetime variable.
- month(datepart()): to get the month of a Datetime variable.
- week(datepart()): to get the week of a Datetime variable.
- day(datepart()): to get the day of a Datetime variable.
data work.datetime_extract_1; set work.datetime; my_year = year(datepart(my_datetime)); my_quarter = qtr(datepart(my_datetime)); my_month = month(datepart(my_datetime)); my_week = week(datepart(my_datetime)); my_day = day(datepart(my_datetime)); run;
Extract the Hour, Minute, and Second from a Datetime
If you work with a Datetime variable, you can use the following SAS functions to extract the hour, minute, or second:
- hour(): to get the hour of a Datetime variable.
- minute(): to get the minute of a Datetime variable.
- second(): to get the second of a Datetime variable.
Note that to use these functions, it’s not necessary to convert the Datetime variable to a Date variable first.
data work.datetime_extract_2; set work.datetime; my_hour = hour(my_datetime); my_minute = minute(my_datetime); my_second = second(my_datetime); run;
2 thoughts on “Easily Extract the Week, Month, and Year in SAS”
Comments are closed.