To get the year of a date variable in a SAS data step, the easiest way is to use the SAS year() function.
data data_with_year;
set data_with_dates;
yr = year(d);
run;
When working with data, many times we are working with dates and need to make adjustments to our data depending on which year something occurred.
We can find the year of a date in SAS very easily. With the SAS year() function, we can get the year of a date variable.
Let’s say we have the following SAS dataset.
data data_with_dates;
input d date9.;
format d date9.;
datalines;
31DEC2021
24OCT2020
12DEC2019
07JUN2019
17FEB2021
12JAN2021
03MAR2020
;
run;
We can get the year from a date variable with the SAS year() function. Below is SAS code with a data step getting the years from the variable “d”.
data data_with_year;
set data_with_dates;
yr = year(d);
run;
The resulting dataset with the years of the date variable is as follows.
d yr
1 31DEC2021 2021
2 24OCT2020 2020
3 12DEC2019 2019
4 07JUN2019 2019
5 17FEB2021 2021
6 12JAN2021 2021
7 03MAR2020 2020
Hopefully this article has been helpful for you to understand how to get the year from a date variable in SAS.