<?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 SAS Macros not resolving... but was working fine yesterday... in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890835#M352003</link>
    <description>&lt;P&gt;Hello SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been working on a project that queries data each quarter and I wanted to create a macro that will make a column with the current federal quarter and fiscal year column. Essentially this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mreynaud_0-1692896560167.png" style="width: 144px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87026i4A90EC3FB0984964/image-dimensions/144x190?v=v2" width="144" height="190" role="button" title="mreynaud_0-1692896560167.png" alt="mreynaud_0-1692896560167.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So I started by making macros. At the time of creating them, they were resolving and the columns were created in my dataset. However, I was puzzled to find that I received this warning/error from SAS when revisiting my program this morning:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mreynaud_1-1692896674710.png" style="width: 468px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87027iAB117E4677400D4F/image-dimensions/468x152?v=v2" width="468" height="152" role="button" title="mreynaud_1-1692896674710.png" alt="mreynaud_1-1692896674710.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I used to create the macros:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* Create quarter and fiscal year macro variable.*/
%macro GetQuarterFromDate();;
	%let date = %sysfunc(today()); /* Get today's date */
    %let quarter = 0;
    %let month = %sysfunc(month(&amp;amp;date));
    
    %if &amp;amp;month &amp;lt;= 3 %then %let quarter = 3;
    %else %if &amp;amp;month &amp;lt;= 6 %then %let quarter = 4;
    %else %if &amp;amp;month &amp;lt;= 9 %then %let quarter = 1;
    %else %if &amp;amp;month &amp;lt;= 12 %then %let quarter = 2;

    %put Quarter &amp;amp;quarter;
%mend;

%GetQuarterFromDate(); 

%macro GetFiscalYearFromDate();
	%let date = %sysfunc(today()); /* Get today's date */

    %let startYear = %eval(%sysfunc(year(&amp;amp;date)) - (%sysfunc(month(&amp;amp;date)) &amp;lt;= 6));
    %let endYear = %eval(&amp;amp;startYear + 1);
    %let fiscalYear = FY &amp;amp;startYear.-&amp;amp;endYear.;
    
    %put &amp;amp;fiscalYear;
%mend;

%GetFiscalYearFromDate();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is the code I used when creating a column with the macro as its value in a dataset. I swapped out my original dataset to SASHELP.stocks, if you want to test:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data quar_fisc;
    set sashelp.stocks;

    %GetQuarterFromDate;
    QuarterColumn = catx(' ', "Quarter", &amp;amp;quarter); /* Concatenate text with quarter value */

    %GetFiscalYearFromDate;
    FiscalYearColumn = "&amp;amp;fiscalYear";
run;

data final;
set quar_fisc;
CombinedColumn = catx(', ', QuarterColumn, FiscalYearColumn);
 drop QuarterColumn FiscalYearColumn;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Aug 2023 17:13:31 GMT</pubDate>
    <dc:creator>mreynaud</dc:creator>
    <dc:date>2023-08-24T17:13:31Z</dc:date>
    <item>
      <title>SAS Macros not resolving... but was working fine yesterday...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890835#M352003</link>
      <description>&lt;P&gt;Hello SAS Community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been working on a project that queries data each quarter and I wanted to create a macro that will make a column with the current federal quarter and fiscal year column. Essentially this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mreynaud_0-1692896560167.png" style="width: 144px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87026i4A90EC3FB0984964/image-dimensions/144x190?v=v2" width="144" height="190" role="button" title="mreynaud_0-1692896560167.png" alt="mreynaud_0-1692896560167.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So I started by making macros. At the time of creating them, they were resolving and the columns were created in my dataset. However, I was puzzled to find that I received this warning/error from SAS when revisiting my program this morning:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mreynaud_1-1692896674710.png" style="width: 468px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/87027iAB117E4677400D4F/image-dimensions/468x152?v=v2" width="468" height="152" role="button" title="mreynaud_1-1692896674710.png" alt="mreynaud_1-1692896674710.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the code I used to create the macros:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/* Create quarter and fiscal year macro variable.*/
%macro GetQuarterFromDate();;
	%let date = %sysfunc(today()); /* Get today's date */
    %let quarter = 0;
    %let month = %sysfunc(month(&amp;amp;date));
    
    %if &amp;amp;month &amp;lt;= 3 %then %let quarter = 3;
    %else %if &amp;amp;month &amp;lt;= 6 %then %let quarter = 4;
    %else %if &amp;amp;month &amp;lt;= 9 %then %let quarter = 1;
    %else %if &amp;amp;month &amp;lt;= 12 %then %let quarter = 2;

    %put Quarter &amp;amp;quarter;
%mend;

%GetQuarterFromDate(); 

%macro GetFiscalYearFromDate();
	%let date = %sysfunc(today()); /* Get today's date */

    %let startYear = %eval(%sysfunc(year(&amp;amp;date)) - (%sysfunc(month(&amp;amp;date)) &amp;lt;= 6));
    %let endYear = %eval(&amp;amp;startYear + 1);
    %let fiscalYear = FY &amp;amp;startYear.-&amp;amp;endYear.;
    
    %put &amp;amp;fiscalYear;
%mend;

%GetFiscalYearFromDate();&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is the code I used when creating a column with the macro as its value in a dataset. I swapped out my original dataset to SASHELP.stocks, if you want to test:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data quar_fisc;
    set sashelp.stocks;

    %GetQuarterFromDate;
    QuarterColumn = catx(' ', "Quarter", &amp;amp;quarter); /* Concatenate text with quarter value */

    %GetFiscalYearFromDate;
    FiscalYearColumn = "&amp;amp;fiscalYear";
run;

data final;
set quar_fisc;
CombinedColumn = catx(', ', QuarterColumn, FiscalYearColumn);
 drop QuarterColumn FiscalYearColumn;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Aug 2023 17:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890835#M352003</guid>
      <dc:creator>mreynaud</dc:creator>
      <dc:date>2023-08-24T17:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros not resolving... but was working fine yesterday...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890836#M352004</link>
      <description>Within the macro you're creating local macro variables. Once they macro is complete, these variables disappear. If you want them to persist after the fact, you can create them as global macro variables. &lt;BR /&gt;&lt;BR /&gt;%global fiscalYear;&lt;BR /&gt;&lt;BR /&gt;Add those types of lines into your code to be able to use the macro variables outside of the macro. &lt;BR /&gt;&lt;BR /&gt;Google Macro Variable Scope for more details.</description>
      <pubDate>Thu, 24 Aug 2023 17:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890836#M352004</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-08-24T17:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros not resolving... but was working fine yesterday...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890889#M352011</link>
      <description>&lt;P&gt;You seem still a little confused about how macro processing works.&amp;nbsp; It makes no sense to call those&amp;nbsp;&amp;nbsp; %GetQuarterFromDate and&amp;nbsp; %GetFiscalYearFromDate&amp;nbsp;macros in the middle a data step.&amp;nbsp; Since the only thing they generate is macro variable values they will completed processes before the data step start running any way.&amp;nbsp;You should call them before you start writing the data step.&amp;nbsp; Now the order of the lines in the program file will more closely match the order that they will be executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you want the value of QUARTER and FISCALYEAR to be available after the macros have finished running you need to have created those macro variables before the macros are called.&amp;nbsp; Otherwise the default action of making unknown new macro variables as LOCAL will apply and they will disappear when the macro call ends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let quarter=;
%let fiscalyear=;
%GetQuarterFromDate;
%GetFiscalYearFromDate;
 
data quar_fisc;
    set sashelp.stocks;

   QuarterColumn = catx(' ', "Quarter", &amp;amp;quarter); /* Concatenate text with quarter value */

   FiscalYearColumn = "&amp;amp;fiscalYear";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 19:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890889#M352011</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-24T19:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macros not resolving... but was working fine yesterday...</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890892#M352012</link>
      <description>&lt;P&gt;Reeza answered your main question.&amp;nbsp; As an aside, to calculate the quarter instead of using a macro, you could use a single macro expression, like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put The quarter for today is: %sysfunc(intnx(quarter,%sysfunc(today()),2),qtr) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That is basically "calculate the quarter of the quarter that is two quarters ahead of today."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your DATA step, you could have the assignment statement be:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;QuarterColumn = "Quarter %sysfunc(intnx(quarter,%sysfunc(today()),2),qtr)" ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Aug 2023 19:17:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macros-not-resolving-but-was-working-fine-yesterday/m-p/890892#M352012</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-08-24T19:17:53Z</dc:date>
    </item>
  </channel>
</rss>

