BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Hello,

I am using the following sas code and the current year is equal to 1965 instead of 2023 ? How to solve that issue.

 

%let today=%sysfunc(putn(%sysfunc(mdy(1,1,2023)),yymmdd10.));
%let curr_year=%sysfunc(year(%sysfunc(putn(%sysfunc(mdy(1,1,2023)),yymmdd10.))));
%put &curr_year;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Because the number 2,021 is in the year 1965, not the year 2023.

1    %let today=%sysfunc(mdy(1,1,2023),yymmdd10.);
2    %put &=today;
TODAY=2023-01-01
3    %put %sysfunc(putn(&today,comma7.));
2,021
4    %put %sysfunc(putn(&today,yymmdd10.));
1965-07-14

Do not format the values when the formats are not needed.

%let today=%sysfunc(mdy(1,1,2023));
%let curr_year=%sysfunc(year(&today));
%put &curr_year;

 

PS You can use the optional second argument to %SYSFUNC() to avoid needing to use PUTN() if you do need a formatted value.  Example:

%let month=%sysfunc(month(&today),z2.);

 

View solution in original post

3 REPLIES 3
ballardw
Super User

Why are you attempting to extract a YEAR value from a formatted string like 2023/01/01. The Year function expects a NUMBER as the argument so that string will be converted to a number as 2023 divided by 1 divided by 1,  or the numeric value of 2023 which the date 16 Jul 1965.

 

Either parse the string with substr (or %substr) or scan/%scan or don't format the value and use the Year function on the direct result of the MDY function (which is pretty weird ) or an actual date value.

 

%let curr_year=%sysfunc(year(%sysfunc(mdy(1,1,2023))));
%put &curr_year;

The only time the macro variable "dates" should be formatted is for use where PEOPLE read them.

If you need to use any of the DATE, TIME or DATETIME functions then do not format them. As you have seen the results are likely going to be wrong.

Tom
Super User Tom
Super User

Because the number 2,021 is in the year 1965, not the year 2023.

1    %let today=%sysfunc(mdy(1,1,2023),yymmdd10.);
2    %put &=today;
TODAY=2023-01-01
3    %put %sysfunc(putn(&today,comma7.));
2,021
4    %put %sysfunc(putn(&today,yymmdd10.));
1965-07-14

Do not format the values when the formats are not needed.

%let today=%sysfunc(mdy(1,1,2023));
%let curr_year=%sysfunc(year(&today));
%put &curr_year;

 

PS You can use the optional second argument to %SYSFUNC() to avoid needing to use PUTN() if you do need a formatted value.  Example:

%let month=%sysfunc(month(&today),z2.);

 

Kurt_Bremser
Super User

If you want to use these macro variables for anything other than human-readable display, then no formatting is needed. The raw numbers work perfectly well in code.

%let today = %sysfunc(mdy(1,1,2023));
%let curr_year = %sysfunc(year(&today.));
%put &curr_year;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 904 views
  • 1 like
  • 4 in conversation