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;
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.);
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.
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.);
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;
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.
Ready to level-up your skills? Choose your own adventure.