<?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: Executing macros from a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363168#M85928</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52605"&gt;@juanvg1972&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;In the macro m,acro_val &amp;nbsp;I make some calculations using var4 (field of ds_val) as parameter of the macro and it returns a value&lt;/P&gt;
&lt;P&gt;I don't know if I can use call execute&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_val(var);

/* Calculations using var as input */

&amp;amp;result;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's still not obvious what this macro is supposed to be doing, because you are still keeping us in the dark. We don't know what &amp;amp;result is, and if it is a number, maybe&amp;nbsp;that works in your original code, but if it is alphanumeric, then it won't work. You don't need the (var) part of&amp;nbsp;macro_val(var) as the macro doesn't use &amp;amp;var.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So please, tell us what the macro does, tell us what &amp;amp;result is, show us more code and relevant parts of the SASLOG.&lt;/P&gt;</description>
    <pubDate>Wed, 31 May 2017 17:38:48 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-05-31T17:38:48Z</dc:date>
    <item>
      <title>Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363152#M85919</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have created a macro, and now I want to execute it from a data step. Somethig similar to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_val1;
set ds_val;
var5 = %macro_val(var4);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The problem is that I want to execute it for each obs of dataset ds_val, var4 is a variable of the dataset.&lt;/P&gt;
&lt;P&gt;If I execute the data step , the macro is resolved before executing the data step.&lt;/P&gt;
&lt;P&gt;What changes do I have to do in order to execute the macro (macro_val) for each obs of ds_val?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 16:51:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363152#M85919</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2017-05-31T16:51:07Z</dc:date>
    </item>
    <item>
      <title>Re: Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363155#M85922</link>
      <description>&lt;P&gt;This ought to work if the macro %macro_val contains only syntactically correct fragments of data step code. It should operate on every observation of the data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Naturally, since you haven't shown us the code for %macro_val, we can't be more specific about what is wrong.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 16:56:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363155#M85922</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-05-31T16:56:36Z</dc:date>
    </item>
    <item>
      <title>Re: Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363157#M85923</link>
      <description>&lt;P&gt;Macros generate SAS code. So unless your macro generates just an expresson that can be used as part of a statement, like the assignment statement you have written, then there is no way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to generate macro calls for each value in a dataset then use CALL EXECUTE() to stack the macro calls to run after the current data step finishes. &amp;nbsp;There are also other ways to generate a series of macro calls from a list of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Give a complete discription of what you actually want to accomplish to get a more detailed responce. &amp;nbsp;Most likely you just need to change your way of thinking about the problem.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 16:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363157#M85923</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-31T16:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363163#M85925</link>
      <description>&lt;P&gt;In the macro m,acro_val &amp;nbsp;I make some calculations using var4 (field of ds_val) as parameter of the macro and it returns a value&lt;/P&gt;
&lt;P&gt;I don't know if I can use call execute&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_val(var);

/* Calculations using var as input */

&amp;amp;result;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 May 2017 17:21:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363163#M85925</guid>
      <dc:creator>juanvg1972</dc:creator>
      <dc:date>2017-05-31T17:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363167#M85927</link>
      <description>&lt;P&gt;It depends on what, if any, SAS code the macro is generating.&lt;/P&gt;
&lt;P&gt;If the macro totally consists of macro statements and just calculates and returns the result as the generated code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_val(var);
%eval(&amp;amp;var + 100)
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then it could work, but you will need to do more work to be able to pass the value of a dataset variable to the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  new_var = input(resolve(cats('%macro_val(',old_var,')'),32.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For this type of equation is might be better if the macro generate the SAS code to generate the required value. &amp;nbsp;Then you could call it with the name for variable to use in the generated code.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro1(varname);
&amp;amp;varname + 100
%mend;

data want;
  set have ;
  new_var1 = %macro1(old_var1);
  new_var2 = %macro1(old_var2);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if the equation is more complex then have it generate multiple data step statements. In that case you might want to pass in both &amp;nbsp;the input and output variable names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro2(inname,outname);
temp=&amp;amp;inname;
&amp;amp;outname=temp + 100;
%mend;

data want;
  set have ;
  %macro2(old_var1,new_var2)
  %macro2(old_var2,new_var2)
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 May 2017 17:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363167#M85927</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-31T17:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Executing macros from a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363168#M85928</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52605"&gt;@juanvg1972&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;In the macro m,acro_val &amp;nbsp;I make some calculations using var4 (field of ds_val) as parameter of the macro and it returns a value&lt;/P&gt;
&lt;P&gt;I don't know if I can use call execute&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro macro_val(var);

/* Calculations using var as input */

&amp;amp;result;

%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's still not obvious what this macro is supposed to be doing, because you are still keeping us in the dark. We don't know what &amp;amp;result is, and if it is a number, maybe&amp;nbsp;that works in your original code, but if it is alphanumeric, then it won't work. You don't need the (var) part of&amp;nbsp;macro_val(var) as the macro doesn't use &amp;amp;var.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So please, tell us what the macro does, tell us what &amp;amp;result is, show us more code and relevant parts of the SASLOG.&lt;/P&gt;</description>
      <pubDate>Wed, 31 May 2017 17:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Executing-macros-from-a-data-step/m-p/363168#M85928</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-05-31T17:38:48Z</dc:date>
    </item>
  </channel>
</rss>

