<?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: Save Function Result as an Macro Variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458165#M116246</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207025"&gt;@JamesBlack&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, guys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your suggestions are useful in combination with my DATA step solution, which is good.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was expecting something simpler, though. When I run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = left(put(month(&amp;amp;&lt;FONT color="#333333"&gt;day_act&lt;/FONT&gt;.) + &amp;amp;&lt;FONT color="#333333"&gt;i.,best.)&lt;/FONT&gt;);&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;%put&lt;/FONT&gt; &amp;amp;want2.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the string &lt;FONT color="#000000"&gt;'left(put(month(mdy(3,13,2018)) + -1,best.))'&lt;/FONT&gt;. It's also useful in my code, but this time I would like to have '2' returned. I tried RESOLVE etc. If there were something like that, it would make the code much simpler.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to actually execute a data step function such as Month in a macro assignment every single function used has to be enclosed in a %sysfunc() call. %SYSFUNC macro function tells the SAS compiler to use the data step function. Otherwise you get text.&lt;/P&gt;
&lt;P&gt;Also the macro language doesn't "like" the put statement and wants Putn(variable, format.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = %sysfunc( left(%sysfunc (putn( %sysfunc(month(&amp;amp;&lt;FONT color="#333333"&gt;day_act&lt;/FONT&gt;.)&amp;nbsp; )&amp;nbsp;+ &amp;amp;&lt;FONT color="#333333"&gt;i.,best.)&lt;/FONT&gt;) ) );&lt;/P&gt;
&lt;P&gt;might work. Multiple function calls in statement gets very ugly quickly. Often I may be easier to use a data _null_ step and call symputx.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there are a number of data step function that have macro equivalents such as&amp;nbsp;Index/%index, Length/%Length,&amp;nbsp;Scan&amp;nbsp;/%scan&lt;/P&gt;</description>
    <pubDate>Fri, 27 Apr 2018 15:19:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-04-27T15:19:48Z</dc:date>
    <item>
      <title>Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458093#M116226</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to do following and store result into a macro variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;gt; extract month from a variable,&lt;/P&gt;&lt;P&gt;&amp;gt; add a constant&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; day_act = mdy(3,13,2018);&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; i = -1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = MONTH(&amp;amp;&lt;FONT color="#000000"&gt;day_act&lt;/FONT&gt;.) + &amp;amp;&lt;FONT color="#000000"&gt;i.&lt;/FONT&gt;; &lt;FONT color="#008000"&gt;/*expect to return numeric 2*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been googling for quite some time and tried several combinations of SYSFUNC, EVAL etc. without desired result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;___ BTW. I have come up with following solution:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#0000ff"&gt;DATA&lt;/FONT&gt;&lt;/STRONG&gt; WORK.tmp;&lt;BR /&gt;&lt;SPAN&gt;want2 &lt;/SPAN&gt;= MONTH(&amp;amp;&lt;FONT color="#008080"&gt;&lt;SPAN&gt;day_act&lt;/SPAN&gt;&lt;/FONT&gt;.) + &amp;amp;&lt;FONT color="#008080"&gt;i&lt;/FONT&gt;.;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#0000ff"&gt;RUN&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;&lt;STRONG&gt;DATA &lt;/STRONG&gt;_NULL_&lt;/FONT&gt;;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;SET&lt;/FONT&gt; WORK.tmp;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;call&lt;/FONT&gt; symput(&lt;FONT color="#ff00ff"&gt;'&lt;SPAN&gt;want2'&lt;/SPAN&gt;&lt;/FONT&gt;, &lt;SPAN&gt;want2&lt;/SPAN&gt;);&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#0000ff"&gt;RUN&lt;/FONT&gt;&lt;/STRONG&gt;;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;%put&lt;/FONT&gt; &amp;amp;&lt;SPAN&gt;want2&lt;/SPAN&gt;.; &lt;FONT color="#008000"&gt;/*It gives exactly what I want*/&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BUT! Oddly, when trying to do this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#0000ff"&gt;DATA&lt;/FONT&gt; &lt;/STRONG&gt;WORK.rep_&amp;amp;&lt;FONT color="#008080"&gt;&lt;SPAN&gt;want2&lt;/SPAN&gt;&lt;/FONT&gt;.;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;SET&lt;/FONT&gt; WORK.something;&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;&lt;STRONG&gt;RUN&lt;/STRONG&gt;&lt;/FONT&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I receive bunch of blank spaces like&amp;nbsp;&lt;/P&gt;&lt;P&gt;WORK.rep_ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2, which obviously gives me an error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would very much appreciate any advice how to reach my goal without DATA step and also I am curious what causes the later error with blank spaces.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 11:46:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458093#M116226</guid>
      <dc:creator>JamesBlack</dc:creator>
      <dc:date>2018-04-27T11:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458095#M116228</link>
      <description>&lt;P&gt;Your assignment should throgh a message about converting to char or something.&lt;/P&gt;
&lt;P&gt;you should use put(month(),best.), and use the left() to remove preceding blanks..&lt;/P&gt;
&lt;P&gt;left(put(month(&amp;amp;day_act. + &amp;amp;i.),best.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 11:54:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458095#M116228</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2018-04-27T11:54:35Z</dc:date>
    </item>
    <item>
      <title>Re: Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458102#M116231</link>
      <description>&lt;P&gt;Unless you really want to store leading and/or trailing spaces into your macro variables you should NOT be using the older CALL SYMPUT() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the newer CALL SYMPUTX() which will automatically strip the leading and trailing spaces.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 12:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458102#M116231</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-27T12:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458107#M116234</link>
      <description>&lt;P&gt;Thank you, guys.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your suggestions are useful in combination with my DATA step solution, which is good.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was expecting something simpler, though. When I run:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = left(put(month(&amp;amp;&lt;FONT color="#333333"&gt;day_act&lt;/FONT&gt;.) + &amp;amp;&lt;FONT color="#333333"&gt;i.,best.)&lt;/FONT&gt;);&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;%put&lt;/FONT&gt; &amp;amp;want2.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the string &lt;FONT color="#000000"&gt;'left(put(month(mdy(3,13,2018)) + -1,best.))'&lt;/FONT&gt;. It's also useful in my code, but this time I would like to have '2' returned. I tried RESOLVE etc. If there were something like that, it would make the code much simpler.&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 12:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458107#M116234</guid>
      <dc:creator>JamesBlack</dc:creator>
      <dc:date>2018-04-27T12:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458116#M116235</link>
      <description>&lt;P&gt;You need to use %SYSFUNC() in day_act too.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let day_act = %SYSFUNC(mdy(3,13,2018));
%put &amp;amp;day_act;
%let i = -1;
%put &amp;amp;i;
%let want2 = %EVAL(%SYSFUNC(MONTH(&amp;amp;day_act.)) + &amp;amp;i.); /*expect to return numeric 2*/
%put &amp;amp;want2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you can try something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let want2 =%SYSFUNC(MONTH(%SYSFUNC(INTNX(MONTH,&amp;amp;day_act.,-1)))); /*expect to return numeric 2*/
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Apr 2018 13:18:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458116#M116235</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-04-27T13:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Save Function Result as an Macro Variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458165#M116246</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/207025"&gt;@JamesBlack&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, guys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your suggestions are useful in combination with my DATA step solution, which is good.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was expecting something simpler, though. When I run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = left(put(month(&amp;amp;&lt;FONT color="#333333"&gt;day_act&lt;/FONT&gt;.) + &amp;amp;&lt;FONT color="#333333"&gt;i.,best.)&lt;/FONT&gt;);&lt;BR /&gt;&lt;FONT color="#0000ff"&gt;%put&lt;/FONT&gt; &amp;amp;want2.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the string &lt;FONT color="#000000"&gt;'left(put(month(mdy(3,13,2018)) + -1,best.))'&lt;/FONT&gt;. It's also useful in my code, but this time I would like to have '2' returned. I tried RESOLVE etc. If there were something like that, it would make the code much simpler.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to actually execute a data step function such as Month in a macro assignment every single function used has to be enclosed in a %sysfunc() call. %SYSFUNC macro function tells the SAS compiler to use the data step function. Otherwise you get text.&lt;/P&gt;
&lt;P&gt;Also the macro language doesn't "like" the put statement and wants Putn(variable, format.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff"&gt;%let&lt;/FONT&gt; want2 = %sysfunc( left(%sysfunc (putn( %sysfunc(month(&amp;amp;&lt;FONT color="#333333"&gt;day_act&lt;/FONT&gt;.)&amp;nbsp; )&amp;nbsp;+ &amp;amp;&lt;FONT color="#333333"&gt;i.,best.)&lt;/FONT&gt;) ) );&lt;/P&gt;
&lt;P&gt;might work. Multiple function calls in statement gets very ugly quickly. Often I may be easier to use a data _null_ step and call symputx.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that there are a number of data step function that have macro equivalents such as&amp;nbsp;Index/%index, Length/%Length,&amp;nbsp;Scan&amp;nbsp;/%scan&lt;/P&gt;</description>
      <pubDate>Fri, 27 Apr 2018 15:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Save-Function-Result-as-an-Macro-Variable/m-p/458165#M116246</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-27T15:19:48Z</dc:date>
    </item>
  </channel>
</rss>

