• 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
  • Ruby
  • 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 Proper Case – Capitalize First Letter of Word with propcase Function
  • 2.  SAS data _null_ – Create a SAS Dataset with No Records and Variables
  • 3.  SAS contains() – Check if Variable Contains a String in Where Statement
  • 4.  SAS prxmatch – Find Pattern in String Using Regex (Regular Expression)
  • 5.  How to Select First 100 Observations of SAS Dataset
  • 6.  SAS Percent Format – Formatting Number as Percent in SAS Dataset
  • 7.  SAS ceil – Round Up to Ceiling of Number in a SAS Data Step
  • 8.  Date Format ddmmmyyyy in SAS
  • 9.  SAS left() Function – Left Align Character Variables in Data Step
  • 10.  SAS yymmdd10. Date Format

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy