• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / SAS / SAS year function – Get Year from Date Variable

SAS year function – Get Year from Date Variable

January 19, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  SAS Uppercase – Make String Letters Uppercase with SAS upcase function
  • 2.  SAS weekday function – Get Day of Week from Date Variable
  • 3.  Get Substring from Right in SAS
  • 4.  SAS Proper Case – Capitalize First Letter of Word with propcase Function
  • 5.  SAS round – Rounding Numbers in a SAS Data Step
  • 6.  Do Loop in SAS Macro Language
  • 7.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 8.  Round Number to 2 Decimal Places in SAS
  • 9.  PROC Format – Create User-Defined Formats for Variables in SAS
  • 10.  SAS If Then Statements with Multiple Variables

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy