<?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 implement a macro function inside a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834980#M330055</link>
    <description>&lt;P&gt;It's hard to know without seeing your macro, but it sounds like you are trying to use a dataset to "drive" macro calls.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;One approach to this is using DOSUBL or CALL EXECUTE.&amp;nbsp; This thread has some examples:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Iteratively-pass-each-row-of-a-data-file-to-a-macro/m-p/814988" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Iteratively-pass-each-row-of-a-data-file-to-a-macro/m-p/814988&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Sep 2022 21:56:12 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-09-23T21:56:12Z</dc:date>
    <item>
      <title>How to implement a macro function inside a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834978#M330053</link>
      <description>&lt;P&gt;Hello. I'd like to run a macro function over every observation of a variable in a dataset. My code (which doesn't work) looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA _NULL_;
	SET Work.HAVE;
	%MacroFunction(Var1);
RUN;&lt;/PRE&gt;&lt;P&gt;where %MacroFunction is a macro defined earlier in my code that takes one argument. This %MacroFunction creates an external .txt file for each entry under Var1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Running just&amp;nbsp;%MacroFunction(Observation1) in open code works perfectly, but since I'd like to run this for every observation under Var1, I'd prefer not to write&amp;nbsp;%MacroFunction(Observation1),&amp;nbsp;%MacroFunction(Observation2), etc., especially when the number of observations may change with each run.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 21:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834978#M330053</guid>
      <dc:creator>smeet</dc:creator>
      <dc:date>2022-09-23T21:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro function inside a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834979#M330054</link>
      <description>&lt;P&gt;It would help if you showed us the macro function eponymously named %MacroFunction&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general, putting a macro function inside a data step requires there to be valid, legal, working SAS data step syntax when the macro function executes and generates text.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this simple example won't work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macrofunction;
proc means data=sashelp.class; run;
%mend;

data abc;
    set sashelp.class;
    new_variable=%macrofunction;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why? Because you cannot embed PROC MEANS inside a SAS data step code, this is not valid working legal SAS data step code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At this link, I provide a macro that does work inside a data step, because it generates valid working legal data step code. &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Death-Rates-in-One-Statement-in-a-DATA-step/m-p/834522#M329930" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/How-to-Calculate-Death-Rates-in-One-Statement-in-a-DATA-step/m-p/834522#M329930&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Why does it work? Because the macro resolves to legal valid working SAS data step code, when the value of &amp;amp;string is C the code becomes &lt;FONT face="courier new,courier"&gt;sum_c + (cause=:"C");&lt;/FONT&gt; and this is valid in a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 21:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834979#M330054</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-23T21:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro function inside a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834980#M330055</link>
      <description>&lt;P&gt;It's hard to know without seeing your macro, but it sounds like you are trying to use a dataset to "drive" macro calls.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;One approach to this is using DOSUBL or CALL EXECUTE.&amp;nbsp; This thread has some examples:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Iteratively-pass-each-row-of-a-data-file-to-a-macro/m-p/814988" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Iteratively-pass-each-row-of-a-data-file-to-a-macro/m-p/814988&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 21:56:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834980#M330055</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-09-23T21:56:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro function inside a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834982#M330057</link>
      <description>&lt;P&gt;You may want either CALL EXECUTE or PROC FCMP instead, depends on the code as stated by others.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 21:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834982#M330057</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-09-23T21:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to implement a macro function inside a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834983#M330058</link>
      <description>&lt;P&gt;The is not such thing as a macro "function".&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The closest thing would be a function style macro that generates only part of a SAS statement so that you could call it in the middle of a SAS statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the way you are calling the macro you couldn't use such a function style macro anyway since your code would only work if the macro generates one or more complete STATEMENTS.&amp;nbsp; But only statements that could be part of a data step.&amp;nbsp; Such as assignment statements or IF THEN statement or perhaps LABEL or FORMAT statements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 22:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-implement-a-macro-function-inside-a-data-step/m-p/834983#M330058</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-23T22:25:22Z</dc:date>
    </item>
  </channel>
</rss>

