<?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: Dynamically call macro for different variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807463#M318367</link>
    <description>Hi,&lt;BR /&gt;I am trying to run the macro Na_update for 60 variables. So I stored the variable names in var_list. But I will try your solution if that works. Thanks</description>
    <pubDate>Tue, 12 Apr 2022 19:11:01 GMT</pubDate>
    <dc:creator>Deyanamika</dc:creator>
    <dc:date>2022-04-12T19:11:01Z</dc:date>
    <item>
      <title>Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807453#M318361</link>
      <description>I want to run a macro for 60 variables...&lt;BR /&gt;&lt;BR /&gt;/* step 1 : made a variable list */&lt;BR /&gt;Proc sql;&lt;BR /&gt;Select distinct var_name into : var_list separated by ' ' from have;&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;/* Step 2 : defined the Marco */&lt;BR /&gt;%macro na_update(var);&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;amp;Var = tranwrd(&amp;amp;var, "NA", '0');&lt;BR /&gt;If &amp;amp;var="." then do &amp;amp;var = '0';&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;/* Runs fine if i run step 2 separately for single variable */&lt;BR /&gt;&lt;BR /&gt;/* step 3 : call macro dynamically*/&lt;BR /&gt;&lt;BR /&gt;%local i, next_var;&lt;BR /&gt;%let i=1 ;&lt;BR /&gt;%do %while (%scan (&amp;amp;var_list, &amp;amp;i);&lt;BR /&gt;%let next_var = %scan(&amp;amp;var_list, &amp;amp;i);&lt;BR /&gt;%na_update(&amp;amp; next_val);&lt;BR /&gt;%let i = %eval(&amp;amp;i +1);&lt;BR /&gt;&lt;BR /&gt;%end;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;%macro_call();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The code is running fine if i run step 2 for single variable. On running the whole code, I get no errors but no desired output... Log looks good and values are assigned rightly... Not been able to understand the problem...&lt;BR /&gt;Thanks in advance!</description>
      <pubDate>Tue, 12 Apr 2022 18:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807453#M318361</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-12T18:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807455#M318363</link>
      <description>&lt;P&gt;You want to run the macro %&lt;SPAN&gt;na_update for each value of var_name in the have data set, correct?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 18:44:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807455#M318363</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-04-12T18:44:53Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807456#M318364</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%na_update(&amp;amp; next_val);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The error is a typographical error, you have misspelled the macro variable name.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 18:47:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807456#M318364</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-12T18:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807457#M318365</link>
      <description>Yes, right. That's what I am looking for.</description>
      <pubDate>Tue, 12 Apr 2022 18:46:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807457#M318365</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-12T18:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807461#M318366</link>
      <description>&lt;P&gt;Ok. I'm not sure what you're aiming for here. The purpose of the macro is unclear to me.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, speaking of the overall logic of you requirement, I would go about it differently. Instead of relying on the macro facility to generate the series of macro calls, I would use the Call Execute Routine and do something like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously, this is untested, since I can't see your data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Step 1 : Define the macro */
%macro na_update(var);
Data want;
set have;
&amp;amp;Var = tranwrd(&amp;amp;var, "NA", '0');
If &amp;amp;var="." then do &amp;amp;var = '0';
end;
run;
%mend;

/* Step 2 : Use Call Execute logic to call the macro for each value of var_name in have */
data _null_;
   set have;
   call execute('%nrstr(%na_update('||var_name||'))');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2022 18:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807461#M318366</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2022-04-12T18:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807463#M318367</link>
      <description>Hi,&lt;BR /&gt;I am trying to run the macro Na_update for 60 variables. So I stored the variable names in var_list. But I will try your solution if that works. Thanks</description>
      <pubDate>Tue, 12 Apr 2022 19:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807463#M318367</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-12T19:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807464#M318368</link>
      <description>Yeah that's actually a typo here... I typed on phone actually.. sorry for bad indentation.</description>
      <pubDate>Tue, 12 Apr 2022 19:11:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807464#M318368</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-12T19:11:52Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807465#M318369</link>
      <description>&lt;P&gt;The problem is that each time you call the macro you begin with the original data set HAVE, and write over the new data set WANT.&amp;nbsp; None of the changes from the previous iteration are saved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you have 60 variables that you want to process in the same way, the usual method is to create an array (not a macro).&amp;nbsp; Then have a single DATA step process every variable in the array.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807465#M318369</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-04-12T19:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807466#M318370</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/422731"&gt;@Deyanamika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Yeah that's actually a typo here... I typed on phone actually.. sorry for bad indentation.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please do not do this any more. It wastes your time, and it wastes our time. Please copy and paste the exact code you are using from now on.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2022 19:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807466#M318370</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-04-12T19:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807553#M318409</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;%macro na_update(var); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;set have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&amp;amp;var = tranwrd(&amp;amp;var,'NA','0'); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;if &amp;amp;var="." then do &amp;amp;var='0'; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;end; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;run; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;%mend;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;data _NULL_; set have;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;call execute('%nrstr(%na_update('||var_name||'))'); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;Tried this, log shows no error. But still its not giving the desired output.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 06:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807553#M318409</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-13T06:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807562#M318414</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/422731"&gt;@Deyanamika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;FONT size="2"&gt;%macro na_update(var); &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;data want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;set have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&amp;amp;var = tranwrd(&amp;amp;var,'NA','0'); &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;if &amp;amp;var="." then do &amp;amp;var='0'; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;end; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;run; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;%mend;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;data _NULL_; set have;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;call execute('%nrstr(%na_update('||var_name||'))'); &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Tried this, log shows no error. But still its not giving the desired output.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It can't. In every iteration you create want from have, so only the result of the last iteration will persist (somebody else already told you this). Your macro code itself is therefore wrong and useless.&lt;/P&gt;
&lt;P&gt;So you want to translate a bunch of variables, the names of which are saved in a macro variable.&lt;/P&gt;
&lt;P&gt;This is done in a single data step, using data step tools (namely, an ARRAY), no macro needed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array all_my_vars {*} &amp;amp;var_list.;
do i = 1 to dim(all_my_vars);
  all_my_vars{i} = tranwrd(all_my_vars{i},"NA",'0');
  if all_my_vars{i} = "." then all_my_vars{i} = '0';
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Given that you iterate over a quite long list of variables, you might consider if it makes sense to transpose your dataset to a long layout. This is especially true if your variable names contain information which is in fact data (like dates or group identifiers).&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 08:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807562#M318414</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-04-13T08:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically call macro for different variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807566#M318415</link>
      <description>&lt;P&gt;This worked. Thank you so much.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 10:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-call-macro-for-different-variables/m-p/807566#M318415</guid>
      <dc:creator>Deyanamika</dc:creator>
      <dc:date>2022-04-13T10:02:08Z</dc:date>
    </item>
  </channel>
</rss>

