<?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: Error of intnx function in a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416764#M280271</link>
    <description>&lt;P&gt;It's not sameday, I think it's 'SAME' for the last parameter to INTNX&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're also using quotes inconsistently within the formula and you shouldn't ahve %LET in the middle of a data step. In a data step use CALL SYMPUTX instead to create macro variables and it allows you to control the scope as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _null_;
date_fin = '31OCT2017'd;
date_deb = intnx('month',date_fin,-12,"same");
date = intnx('month', date_deb, -12, 'same'); 

call symputx('date_fin', date, 'g');
call symputx('date_deb', date_deb, 'g');

/*if you want them to look like date9*/
call symputx('date_fin2', put(date, date9.), 'g');
call symputx('date_deb2', put(date_deb, date9.) , 'g');

RUN;

%put &amp;amp;date_fin.;
%put &amp;amp;date_deb.;

%put &amp;amp;date_fin2.;
%put &amp;amp;date_deb2.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 28 Nov 2017 17:04:02 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-11-28T17:04:02Z</dc:date>
    <item>
      <title>Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416754#M280269</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello guys, I have a problem&amp;nbsp; about the usage of INTNX function in the MACRO, the codes are as following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEST;
%let date_fin = '31OCT2017'd;
%let date_deb = intnx("month",&amp;amp;date_fin,-12,"sameday");
date = intnx(month, &amp;amp;date_deb., -12, sameday); 
format date date9.;
RUN;&lt;BR /&gt;&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;The objective is : only need to change "date_fin" outside the Macro, in which "date" is&amp;nbsp;calculated automatically with intnx function inside the "%macro blabla ... %mend" macro program&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In works in this test data step. but when I put it in the macro program as&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_fin = '31OCT2017'd;
%let date_deb = intnx("month",&amp;amp;date_fin,-12,"sameday");

%macro test;
%let date = %sysfunc(intnx(month, &amp;amp;date_deb, -12, sameday));

...

%mend test;&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the error in log is as following:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;EM&gt;ERROR: Argument 2 to function INTNX referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.&lt;/EM&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;EM&gt;ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Thank you for your kind help.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Vivi&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 16:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416754#M280269</guid>
      <dc:creator>viviwu</dc:creator>
      <dc:date>2017-11-28T16:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416763#M280270</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That %let in the DATA step is not doing what you think it does -- it does not assign the value as part of the step -- rather, the macro processor handles that, and so requires proper syntax for SAS macros.&amp;nbsp; When using this function in macro code, you need to use %sysfunc and remove the quotes from the literal values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_deb = %sysfunc(
  intnx(
   month,             /* unit of time interval */
   &amp;amp;date_fin.
   -12,                /* number of intervals, negative goes to the past */
   same               /* alignment of interval date. "Same" is for same day of month */
   )
  );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See this &lt;A href="https://blogs.sas.com/content/sasdummy/2017/11/21/compute-date-past-future-intnx/" target="_self"&gt;blog post for more information&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 17:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416763#M280270</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2017-11-28T17:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416764#M280271</link>
      <description>&lt;P&gt;It's not sameday, I think it's 'SAME' for the last parameter to INTNX&amp;gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You're also using quotes inconsistently within the formula and you shouldn't ahve %LET in the middle of a data step. In a data step use CALL SYMPUTX instead to create macro variables and it allows you to control the scope as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _null_;
date_fin = '31OCT2017'd;
date_deb = intnx('month',date_fin,-12,"same");
date = intnx('month', date_deb, -12, 'same'); 

call symputx('date_fin', date, 'g');
call symputx('date_deb', date_deb, 'g');

/*if you want them to look like date9*/
call symputx('date_fin2', put(date, date9.), 'g');
call symputx('date_deb2', put(date_deb, date9.) , 'g');

RUN;

%put &amp;amp;date_fin.;
%put &amp;amp;date_deb.;

%put &amp;amp;date_fin2.;
%put &amp;amp;date_deb2.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 17:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416764#M280271</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-28T17:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416781#M280272</link>
      <description>&lt;P&gt;You could make your life a lot simpler if you used data set variables instead of macro variables wherever possible. If &amp;amp;date_fin is created outside the data set then you could try something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;
    date_fin=&amp;amp;date_fin;
    date_deb=intnx('month',date_fin,-12,'same');
    date = intnx('month',date_deb, -12,'same');

&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Nov 2017 17:38:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416781#M280272</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-11-28T17:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416937#M280273</link>
      <description>&lt;P&gt;I agree with the previous comments.&lt;/P&gt;
&lt;P&gt;As for your code, this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_fin = '31OCT2017'd;
%let date_deb = %sysfunc(intnx(month,%sysevalf(&amp;amp;date_fin),-12,sameday));

%macro test;
  %let date = %sysfunc(intnx(month, &amp;amp;date_deb, -12, sameday));
  %put &amp;amp;=date;
%mend test; %test
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2017 02:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416937#M280273</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-11-29T02:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416942#M280274</link>
      <description>&lt;P&gt;Let's take a detailed look at what the code you posted is doing.&lt;/P&gt;
&lt;P&gt;First you posted code without a macro definition.&amp;nbsp; You posted it in this form.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEST;
%let date_fin = '31OCT2017'd;
%let date_deb = intnx("month",&amp;amp;date_fin,-12,"sameday");
date = intnx(month, &amp;amp;date_deb., -12, sameday); 
format date date9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But since the macro processor does its work before the data step code is even compiled what you really asked SAS to do was this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_fin = '31OCT2017'd;
%let date_deb = intnx("month",&amp;amp;date_fin,-12,"sameday");
DATA TEST;
date = intnx(month, &amp;amp;date_deb., -12, sameday); 
format date date9.;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if we replace the macro variables with their values you are running this code.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEST;
date = intnx(month,  intnx("month", '31OCT2017'd-12,"sameday"), -12, sameday); 
format date date9.;
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;STRONG&gt;Now that code is going to have problems&lt;/STRONG&gt; since the first and last arguments to the outer INTNX() call are the variables MONTH and SAMEDAY which you have not defined anywhere.&amp;nbsp; We can fix that by converting them into string literals.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA TEST;
date = intnx('month',  intnx("month", '31OCT2017'd-12,"sameday"), -12, 'sameday'); 
format date date9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now at least it will run and create a dataset with one observation and one variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you show some more code without a datastep.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_fin = '31OCT2017'd;
%let date_deb = intnx("month",&amp;amp;date_fin,-12,"sameday");

%macro test;
%let date = %sysfunc(intnx(month, &amp;amp;date_deb, -12, sameday));
%mend test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The fact that the third %LET statement is inside a macro definition is irrelevant, so we can just remove that complication.&lt;/P&gt;
&lt;P&gt;If we replace the macro variable references with their values you end up with.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date = %sysfunc(intnx(month, intnx("month",'31OCT2017'd,-12,"sameday"), -12, sameday));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will not work because you have given the INTNX() function that the macro function %SYSFUNC() is calling an invalid value for the second argument.&amp;nbsp; It is expecting a date value (since you specified the MONTH interval), but you have given it a text string.&amp;nbsp; Now it is a text string that worked well when you were using it to generate code as part of a DATA step.&amp;nbsp; But for the %SYSFUNC(INTNX()) call to work it needs to have an actual date value there and not code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Most likely what you want to do is to change your second %LET statement to actually calculate a date value instead of just having SAS code that could eventually calculate a date.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_deb = %sysfunc(intnx(month,&amp;amp;date_fin,-12,sameday));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So for your current value of &amp;amp;DATE_FIN the result will be that DATE_DEB is assigned the value&amp;nbsp;20758 which is the number that SAS uses to represent&amp;nbsp;2016-10-31.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now both your DATA step code (with the fixed INTNX() call) and your %LET statement will generate the same date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could if you want make it a little easier to read SYMBOLGEN and MPRINT results generated when you use DATE_DEB if you generated it in the form of a date literal like you did when you assigned a value to DATE_FIN.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_deb = "%sysfunc(intnx(month,&amp;amp;date_fin,-12,sameday),date9)"d;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now DATE_DEB will have the value "31OCT2016"d instead of 20758.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that both values represent the same number in SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2017 03:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416942#M280274</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-29T03:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416944#M280275</link>
      <description>&lt;P&gt;Note that %SYSEVALF() is not needed where you have it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are using the %SYSFUNC() macro function to call the SAS function INTNX(). And unlike the macro processor SAS knows how to deal with date literals (and floating point arithmetic).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The %SYSEVALF() function would only be needed if you wanted to use pure macro code like.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let tomorrow=%sysevalf('28NOV2017'd +1);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_fin = '31OCT2017'd;
%let date_deb = %sysfunc(intnx(month,%sysevalf(&amp;amp;date_fin),-12,sameday));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Nov 2017 03:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416944#M280275</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-29T03:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: Error of intnx function in a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416990#M280276</link>
      <description>That's exatcly what I need! Thank you so much Tom!</description>
      <pubDate>Wed, 29 Nov 2017 09:02:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-of-intnx-function-in-a-macro-variable/m-p/416990#M280276</guid>
      <dc:creator>viviwu</dc:creator>
      <dc:date>2017-11-29T09:02:07Z</dc:date>
    </item>
  </channel>
</rss>

