<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get the year in numerical format in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858784#M42242</link>
    <description>&lt;P&gt;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 &amp;amp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make this a valid SAS date value, remove the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%sysfunc(year(&amp;amp;firstDayOY));
%let Minyear=%sysfunc(year(&amp;amp;firstDayOPY));

%put &amp;amp;=firstDayOY which is really %sysfunc(putn(&amp;amp;firstDayOY,yymmddn.));
%put &amp;amp;=firstDayOPY which is really %sysfunc(putn(&amp;amp;firstDayOPY,yymmddn.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then you can compute the year via&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%sysfunc(year(&amp;amp;firstDayOY));
%let Minyear=%sysfunc(year(&amp;amp;firstDayOPY));
%put &amp;amp;=firstDayOY &amp;amp;=firstDayOPY &amp;amp;=MaxYear &amp;amp;=Minyear ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Feb 2023 17:21:06 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-02-14T17:21:06Z</dc:date>
    <item>
      <title>How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858777#M42239</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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(&amp;amp;firstDayOY);
%let Minyear=year(&amp;amp;firstDayOPY.);

%put &amp;amp;=firstDayOY  &amp;amp;=firstDayOPY &amp;amp;=MaxYear &amp;amp;=Minyear ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2023 17:01:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858777#M42239</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2023-02-14T17:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858783#M42241</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76331"&gt;@alepage&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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(&amp;amp;firstDayOY);
%let Minyear=year(&amp;amp;firstDayOPY.);

%put &amp;amp;=firstDayOY  &amp;amp;=firstDayOPY &amp;amp;=MaxYear &amp;amp;=Minyear ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What do you mean by MIN?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp; &lt;STRONG&gt;But you would have to give the YEAR() function call an actual DATE value&lt;/STRONG&gt; and not the string you generated with the YYMMDDN format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to extract the YEAR from an 8 digit string in YYYYMMDD style then just use the %SUBSTR() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%substr(&amp;amp;firstDayOY,1,4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;REMEMBER: Macro variables just contain text.&amp;nbsp; 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.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 17:46:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858783#M42241</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-14T17:46:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858784#M42242</link>
      <description>&lt;P&gt;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 &amp;amp;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To make this a valid SAS date value, remove the format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%sysfunc(year(&amp;amp;firstDayOY));
%let Minyear=%sysfunc(year(&amp;amp;firstDayOPY));

%put &amp;amp;=firstDayOY which is really %sysfunc(putn(&amp;amp;firstDayOY,yymmddn.));
%put &amp;amp;=firstDayOPY which is really %sysfunc(putn(&amp;amp;firstDayOPY,yymmddn.));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and then you can compute the year via&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%sysfunc(year(&amp;amp;firstDayOY));
%let Minyear=%sysfunc(year(&amp;amp;firstDayOPY));
%put &amp;amp;=firstDayOY &amp;amp;=firstDayOPY &amp;amp;=MaxYear &amp;amp;=Minyear ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 17:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858784#M42242</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-14T17:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858794#M42243</link>
      <description>&lt;P&gt;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:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let MaxYear=%substr(&amp;amp;firstDayOY.,1,4);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Feb 2023 17:44:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858794#M42243</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-14T17:44:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858804#M42244</link>
      <description>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 &lt;BR /&gt;</description>
      <pubDate>Tue, 14 Feb 2023 18:45:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858804#M42244</guid>
      <dc:creator>alepage</dc:creator>
      <dc:date>2023-02-14T18:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858806#M42245</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76331"&gt;@alepage&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;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 &lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Macro variables are always text. If you write code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do year=&amp;amp;minyear %to &amp;amp;maxyear;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then SAS will replace the references to the macro variables with the text they contain.&amp;nbsp; So it is equivalent to writing code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do year=2022 %to 2023;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which SAS (actually the implicit call to the %EVAL() function) will happily see has being integers.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 18:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858806#M42245</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-14T18:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the year in numerical format</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858833#M42246</link>
      <description>&lt;P&gt;The macro language has only one data type,&amp;nbsp;&lt;U&gt;text&lt;/U&gt;. In the code you &lt;EM&gt;create&lt;/EM&gt; 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.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2023 21:03:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-get-the-year-in-numerical-format/m-p/858833#M42246</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-14T21:03:04Z</dc:date>
    </item>
  </channel>
</rss>

