<?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: Returning data step line via macro function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514466#M138740</link>
    <description>&lt;P&gt;If I understand properly, you are overcomplicating the problem.&amp;nbsp; It seems you have a separate table with 23 observations, and want to retrieve a value from that table based on a number going from 1 to 23.&amp;nbsp; For that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do k=1 to 23;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set other_table point=k;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The STOP statement is important for this example, preventing an infinite loop in the DATA step.&amp;nbsp; It may be OK to remove it in a more complex DATA step that has a guaranteed ending.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are other approaches possible as well, such as reading the other table into a temporary array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array factors {k} _temporary_;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do k=1 to 23;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set other_table;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;factors{k} = cs_factor;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;.... retrieve values from the _temporary_ array as needed ....&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 19 Nov 2018 15:15:33 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-11-19T15:15:33Z</dc:date>
    <item>
      <title>Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514335#M138692</link>
      <description>&lt;P&gt;Hey all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've been trying to write a macro function which return a value based on the input&amp;nbsp;rating value (1 to 23);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The goal is to use the output of this function to create a new variable in the table containing the rating scores.&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;It seems every method I try failed in someway&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;1 Direct call&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro(cs_factor);&lt;/P&gt;
&lt;P&gt;[...]&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&amp;amp;output.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt; do cs = 1 to 23;&lt;BR /&gt;&amp;nbsp;&lt;FONT color="#FF0000"&gt;cs_score = %cs_factor(cs);&lt;/FONT&gt;&lt;BR /&gt;&amp;nbsp;output;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This doesn't resolve because it keeps executing %cs_factor(cs) with cs as a macro variable instead of the value of the variable cs.&lt;/P&gt;
&lt;P&gt;It does return values, but they're all equal to 0 due to error handling below which is supposed to be TRUE;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SYMBOLGEN: Macro variable CS resolves to cs&lt;BR /&gt;SYMBOLGEN: Macro variable MAX_CS resolves to 23&lt;BR /&gt;MLOGIC(CS_SCORING): %IF condition %sysevalf(&amp;amp;cs. le &amp;amp;max_cs.) is FALSE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;2 Call Execute&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro(cs_factor);&lt;/P&gt;
&lt;P&gt;[...]&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;cs_factor=&amp;amp;output.&lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt; length cs cs_score 8.;&lt;BR /&gt; do cs = 1 to 23;&lt;BR /&gt; &lt;FONT color="#FF0000"&gt;call execute('%cs_scoring('||cs||')');&lt;/FONT&gt;&lt;BR /&gt; output;&lt;BR /&gt; end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;This doesn't give me the expected table of variable cs (1 to 23) and cs_factor either, but generated this error for all 23 iterations;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 + cs_factor=1;&lt;BR /&gt; &lt;FONT color="#FF0000"&gt;_________&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt; 180&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#339966"&gt;NOTE: Line generated by the CALL EXECUTE routine.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000000"&gt;2 + cs_factor=0.99999882161769;&lt;/FONT&gt;&lt;BR /&gt; _________&lt;BR /&gt; 180&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;SPAN&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT color="#000000"&gt;if I remove the semicolon from the macro definition (see blue &lt;STRONG&gt;&lt;FONT color="#3366FF"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;) I get the error only once.&amp;nbsp;&lt;/FONT&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 + cs_factor=1&lt;BR /&gt; &lt;FONT color="#FF0000"&gt;_________&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt; 180&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;2 + cs_factor=0.99999882161769&lt;BR /&gt;3 + cs_factor=0.99999080089355&lt;BR /&gt;4 + cs_factor=0.99996111096086&lt;BR /&gt;5 + cs_factor=0.99988122640202&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;So, how do I call that macro like I would a general function? I can't use call execute to the right of an equation either like "cs_factor =&amp;nbsp;&lt;SPAN&gt;call execute('%cs_scoring('||cs||')') " so I'm a bit at a loss. The value I get out of it is correct, I just don't know how to get that value imputed into a data step call.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;Leaving office now so replies come tomorrow.&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;SPAN&gt;Thanks in advance!&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 04:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514335#M138692</guid>
      <dc:creator>WimB</dc:creator>
      <dc:date>2018-11-19T04:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514355#M138708</link>
      <description>Try proc fcmp instead of a macro.</description>
      <pubDate>Mon, 19 Nov 2018 07:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514355#M138708</guid>
      <dc:creator>error_prone</dc:creator>
      <dc:date>2018-11-19T07:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514359#M138710</link>
      <description>&lt;P&gt;The macro &lt;STRONG&gt;pre&lt;/STRONG&gt;processor is just an advanced text replacement system built to aid you in writing dynamic and/or repeating code.&lt;/P&gt;
&lt;P&gt;It does NOT (that's why I emphasized "pre") have access to the values of data step variables, only to their names (because &lt;EM&gt;you&lt;/EM&gt; supplied them).&lt;/P&gt;
&lt;P&gt;%cs_factor(cs) means that the macro will only see the &lt;EM&gt;text&lt;/EM&gt; "cs" (without the quotes, of course) when it executes.&lt;/P&gt;
&lt;P&gt;If you want to create a "function style" macro, it must NOT return a &lt;EM&gt;value&lt;/EM&gt;, but &lt;EM&gt;code&lt;/EM&gt; which then runs in the data (or procedure) step and does what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;create &lt;EM&gt;working&lt;/EM&gt; SAS code&lt;/LI&gt;
&lt;LI&gt;identify parts that need to be made dynamic&lt;/LI&gt;
&lt;LI&gt;replace those with macro variables&lt;/LI&gt;
&lt;LI&gt;manually set macro variables an test&lt;/LI&gt;
&lt;LI&gt;if OK, wrap into a macro and set the macro variables from 3. as parameters&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;So you first have to show us what you want to do in the place of the future macro, in Base SAS code.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 07:55:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514359#M138710</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-11-19T07:55:34Z</dc:date>
    </item>
    <item>
      <title>Re: Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514378#M138719</link>
      <description>&lt;P&gt;Really?&amp;nbsp; Hiding the code in a compiled catalog is going to improve things?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 08:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514378#M138719</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-19T08:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514381#M138721</link>
      <description>&lt;P&gt;Others have suggested that you use PROC FCMP, which is not a bad idea if your code is that complicated. But you can also do it as a function-style macro, if you are trying something relatively simple, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CS_factor(index);
ChooseN(&amp;amp;index,
1,
0.99999882161769,
0.99999882161769,
0.99999080089355,
0.99996111096086,
0.99988122640202,
.... rest of values go here
)
%mend;

data test;
  do cs=1 to 23;
    cs_score=%cs_factor(cs);
    output;
    end;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that I did not put a final semicolon in the expression inside the macro; that way, the macro can be used just as you would use a normal function call, e.g. "CS_plustwo=%cs_factor(cs)+2;".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that the CHOOSEN function truncates the index value, and reports an error if the index is out of range. Which may be OK with you, else you may have to modify the function call, e.g.:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CS_factor(index);
ChooseN(ifn(1&amp;lt;=&amp;amp;index&amp;lt;=23,&amp;amp;index,24),
1,
0.99999882161769,
0.99999882161769,
0.99999080089355,
0.99996111096086,
0.99988122640202,
.... rest of values go here
. /* missing value for index out of range */
)&lt;BR /&gt;%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 08:58:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514381#M138721</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-11-19T08:58:21Z</dc:date>
    </item>
    <item>
      <title>Re: Returning data step line via macro function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514466#M138740</link>
      <description>&lt;P&gt;If I understand properly, you are overcomplicating the problem.&amp;nbsp; It seems you have a separate table with 23 observations, and want to retrieve a value from that table based on a number going from 1 to 23.&amp;nbsp; For that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;do k=1 to 23;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set other_table point=k;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;stop;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The STOP statement is important for this example, preventing an infinite loop in the DATA step.&amp;nbsp; It may be OK to remove it in a more complex DATA step that has a guaranteed ending.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are other approaches possible as well, such as reading the other table into a temporary array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;array factors {k} _temporary_;&lt;/P&gt;
&lt;P&gt;if _n_=1 then do k=1 to 23;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set other_table;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;factors{k} = cs_factor;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;.... retrieve values from the _temporary_ array as needed ....&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Nov 2018 15:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Returning-data-step-line-via-macro-function/m-p/514466#M138740</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-11-19T15:15:33Z</dc:date>
    </item>
  </channel>
</rss>

