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

%let firstDayOY=%sysfunc(intnx(YEAR,%sysfunc(today()), -0,B), YYMMDDN.);
%let firstDayOPY=%sysfunc(intnx(YEAR,%sysfunc(today()), -1,B), YYMMDDN.);


%let MaxYear=year(&firstDayOY);
%let Minyear=year(&firstDayOPY.);

%put &=firstDayOY  &=firstDayOPY &=MaxYear &=Minyear ;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

As others have already said, you can't use the YEAR function on a formatted date. But since you used the YYMMDDN format with no length (which means it defaults to 8), you know that the first 4 digits will contain the year. Therefore use %SUBSTR:

%let MaxYear=%substr(&firstDayOY.,1,4);

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

@alepage wrote:
How to get MinYear and MaxYear

%let firstDayOY=%sysfunc(intnx(YEAR,%sysfunc(today()), -0,B), YYMMDDN.);
%let firstDayOPY=%sysfunc(intnx(YEAR,%sysfunc(today()), -1,B), YYMMDDN.);


%let MaxYear=year(&firstDayOY);
%let Minyear=year(&firstDayOPY.);

%put &=firstDayOY  &=firstDayOPY &=MaxYear &=Minyear ;

What do you mean by MIN?

 

If you want to run the YEAR() function in pure macro code you need to use the %SYSFUNC() macro function, just like you did to run the INTNX() and TODAY() function calls.  But you would have to give the YEAR() function call an actual DATE value and not the string you generated with the YYMMDDN format.

 

If you want to extract the YEAR from an 8 digit string in YYYYMMDD style then just use the %SUBSTR() function.

%let MaxYear=%substr(&firstDayOY,1,4);

 

REMEMBER: Macro variables just contain text.  The value of MAXYEAR will only be interpreted as a NUMBER if you use in a place where a four digit string is interpreted as a number.

PaigeMiller
Diamond | Level 26

To use the YEAR function (or any other calendar function), the value must be (MUST BE) as valid SAS date value, which means that it is the number of days since 01JAN1960. So, if &firstDayOY is equal to 20230101 (which is what your code returns), it is not a valid SAS date value, despite the fact that it looks like a valid date to humans.

 

To make this a valid SAS date value, remove the format.

 

%let MaxYear=%sysfunc(year(&firstDayOY));
%let Minyear=%sysfunc(year(&firstDayOPY));

%put &=firstDayOY which is really %sysfunc(putn(&firstDayOY,yymmddn.));
%put &=firstDayOPY which is really %sysfunc(putn(&firstDayOPY,yymmddn.));

 

 

and then you can compute the year via

 

%let MaxYear=%sysfunc(year(&firstDayOY));
%let Minyear=%sysfunc(year(&firstDayOPY));
%put &=firstDayOY &=firstDayOPY &=MaxYear &=Minyear ;

 

--
Paige Miller
Kurt_Bremser
Super User

As others have already said, you can't use the YEAR function on a formatted date. But since you used the YYMMDDN format with no length (which means it defaults to 8), you know that the first 4 digits will contain the year. Therefore use %SUBSTR:

%let MaxYear=%substr(&firstDayOY.,1,4);
alepage
Barite | Level 11
It is funny because, it was exactly my first idea. But does sas will interpret this as a numeric or a string ? Probably like a string. Will it make a difference in a loop, i.e. from MinYear to MaxYear
Tom
Super User Tom
Super User

@alepage wrote:
It is funny because, it was exactly my first idea. But does sas will interpret this as a numeric or a string ? Probably like a string. Will it make a difference in a loop, i.e. from MinYear to MaxYear

Macro variables are always text. If you write code like:

%do year=&minyear %to &maxyear;

Then SAS will replace the references to the macro variables with the text they contain.  So it is equivalent to writing code like this:

%do year=2022 %to 2023;

Which SAS (actually the implicit call to the %EVAL() function) will happily see has being integers.

Kurt_Bremser
Super User

The macro language has only one data type, text. In the code you create with the macro facility, you have to make sure that it is syntactically valid and makes sense semantically. So if macro variable minyear contains the text 2022, it can be used in an iterative DO statement as a from or to value.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 826 views
  • 2 likes
  • 4 in conversation