<?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: Execute macro inside data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655532#M196662</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/36451"&gt;@MariaD&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An approach to avoid this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   valor_1=2; 
   valor_2 = 5;
   call symputx ("valor_final", prxchange('s/\s+/,/',-1,strip(repeat(valor_1,valor_2-1))));
run;

data want;
	set test;
	var_final = sum(&amp;amp;valor_final.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 09 Jun 2020 14:01:04 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-06-09T14:01:04Z</dc:date>
    <item>
      <title>Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655517#M196656</link>
      <description>&lt;P&gt;Hi Folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to execute a macro inside data step. I made a simple example but the following error appears:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       valor_2 
ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro VALOR will stop executing.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Our example is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global valor_final;

%macro valor (var_1, var_2);
%let valor_final=;
%do i=1 %to %eval(&amp;amp;var_2);
  %if &amp;amp;i=1 %then %let valor_final = &amp;amp;var_2. ;  
  %else %let valor_final = &amp;amp;valor_final., &amp;amp;var_1. ; 
%end;
%mend valor;


data test;
   valor_1=90; 
   valor_2 = 5;
   %valor(valor_1, valor_2);
   var_final = SUM(&amp;amp;valor_final.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In this case, our objective is parameter "VALOR_FINAL" will be replace by: "90, 90, 90, 90, 90". So the program will be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   valor_1=90; 
   valor_2 = 5;
   %valor(valor_1, valor_2);
   var_final = SUM(90, 90, 90, 90, 90);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 13:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655517#M196656</guid>
      <dc:creator>MariaD</dc:creator>
      <dc:date>2020-06-09T13:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655523#M196658</link>
      <description>&lt;P&gt;Macro resolution is compile time only and not execution time inside a datastep. %let does not work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The options you have are sum(of valor_1 -- valor_n) if you have a repeating pattern in your variable names such as valor_: as suggested previously.&lt;/P&gt;
&lt;P&gt;Use combination of arrays and sum function.&lt;/P&gt;
&lt;P&gt;array x{*} var1 dummy2 someotherthing;&lt;/P&gt;
&lt;P&gt;sum(of x:)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 13:40:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655523#M196658</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-09T13:40:02Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655525#M196659</link>
      <description>&lt;P&gt;The value of&amp;nbsp; valor_2 is not resolved at compile time and is symbolically passed as a character string valor_2&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 13:41:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655525#M196659</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-06-09T13:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655527#M196660</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main objective is not to use with SUM function, was only a sample. Our problem is we have a value 90, por example in a variable and 5 in other variable. We need to create of list of 90 value, separated by ",", 5 times. So, we expected a result of 90, 90, 90, 90, 90 to use in other process.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I know exists REPEAT function, but the if we use REPEAT(90, 5) the result is 9090909090 (without a separated value).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 13:53:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655527#M196660</guid>
      <dc:creator>MariaD</dc:creator>
      <dc:date>2020-06-09T13:53:32Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655529#M196661</link>
      <description>&lt;P&gt;Yes,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;. The macro takes valor_2 as text not the value of the variable called valor_2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 13:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655529#M196661</guid>
      <dc:creator>MariaD</dc:creator>
      <dc:date>2020-06-09T13:56:31Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655532#M196662</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/36451"&gt;@MariaD&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An approach to avoid this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   valor_1=2; 
   valor_2 = 5;
   call symputx ("valor_final", prxchange('s/\s+/,/',-1,strip(repeat(valor_1,valor_2-1))));
run;

data want;
	set test;
	var_final = sum(&amp;amp;valor_final.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Jun 2020 14:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655532#M196662</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-09T14:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655536#M196664</link>
      <description>&lt;P&gt;Hi &lt;A class="trigger-hovercard" style="color: #007dc3;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/36451" target="_blank"&gt;MariaD&lt;/A&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your objective is to replace VALOR_FINAL by: "90, 90, 90, 90, 90", why do you need SUM function and the macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   valor_1=90; 
   valor_2 = 5;
   %valor(valor_1, valor_2);
   var_final = SUM(&amp;amp;valor_final.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why not just replace your above code with:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   valor_1=90; 
   valor_2 = 5;
   var_final = valor_1 * valor_2;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 14:12:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655536#M196664</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2020-06-09T14:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Execute macro inside data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655580#M196685</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;as it has been said you do not Need a macro to perform this Kind of Operation.&lt;/P&gt;
&lt;P&gt;For Demonstration purpose there are 2 ways to run a macro inside a data step: dosubl and call execute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dosubl:&lt;/P&gt;
&lt;PRE&gt;%macro valor (var_1, var_2);
   %do i=1 %to %eval(&amp;amp;var_2);
     %if &amp;amp;i=1 %then %let valor_final = &amp;amp;var_1. ;  
     %else %let valor_final = &amp;amp;valor_final., &amp;amp;var_1. ; 
   %end;
   %put &amp;amp;=valor_final;
   %let valor_final = %sysfunc(sum(&amp;amp;valor_final.)); 
   %put &amp;amp;=valor_final;
%mend valor;

data test;
   set sashelp.class;
   valor_1=weight; 
   valor_2 = age;

   rc = dosubl('%global valor_final; %valor('||put(valor_1,best.)||','||put(valor_2,best.)||')');
   var_final=symget('valor_final');
   rc = dosubl('%symdel valor_final');
   keep va: rc:;
run;
proc print;run;&lt;/PRE&gt;
&lt;P&gt;call execute:&lt;/P&gt;
&lt;PRE&gt;%macro valor (var_1, var_2);
   %local valor_final;
   %do i=1 %to %eval(&amp;amp;var_2);
     %if &amp;amp;i=1 %then %let valor_final = &amp;amp;var_1. ;  
     %else %let valor_final = &amp;amp;valor_final., &amp;amp;var_1. ; 
   %end;
   %put &amp;amp;=valor_final;
   %let valor_final = %sysfunc(sum(&amp;amp;valor_final.)); 
   %put &amp;amp;=valor_final;
   &amp;amp;valor_final.
%mend valor;

data test1;
   set sashelp.class end=last;
   valor_1=weight; 
   valor_2 = age;

   if _N_ eq 1 then call execute('data test2;');
   call execute('var_final=%nrstr(%valor('||put(valor_1,best.)||','||put(valor_2,best.)||'));output;');
   if last then call execute('run;');
run;
proc print;run;&lt;/PRE&gt;
&lt;P&gt;Give them a try and have fun with it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 16:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execute-macro-inside-data-step/m-p/655580#M196685</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2020-06-09T16:21:56Z</dc:date>
    </item>
  </channel>
</rss>

