<?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 convert macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601789#M174116</link>
    <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrobound"&gt;&lt;BR /&gt;Just a quick question if you dont mind. i need all numbers to show into one table from 1 to 6. When i run &lt;BR /&gt;the macro below, the table only show the i=6 result. Is there anything you would recommend?&lt;BR /&gt;&lt;BR /&gt;%macro&lt;/SPAN&gt; run_all_nums&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%to&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;6&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macroname"&gt;%get_sc_val&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token macrobound"&gt;%mend&lt;/SPAN&gt; run_all_nums&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token macroname"&gt;%run_all_nums&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Nov 2019 20:11:46 GMT</pubDate>
    <dc:creator>niloya</dc:creator>
    <dc:date>2019-11-05T20:11:46Z</dc:date>
    <item>
      <title>How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601396#M173944</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;Would you please let me know how to convert the codes below to macro if SC value change as 1,2,3,4,5,6 ? Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data work.want;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Set work.have;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if SC=&lt;STRONG&gt;1&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and ((co1*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_1*daily)+(int_1))&amp;gt;&lt;STRONG&gt;99.7&lt;/STRONG&gt; then&lt;/P&gt;&lt;P&gt;exp_val =&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Else if SC=&lt;STRONG&gt;1&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and&amp;nbsp; ((co1*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_1*daily)+(int_1))&amp;lt;&lt;STRONG&gt;99.7&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then&lt;/P&gt;&lt;P&gt;exp_val=(( co_dem_1*((co1*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_1*daily)+(int_1))/100&lt;/P&gt;&lt;P&gt;;&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;if SC=&lt;STRONG&gt;2&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and ((co2*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_2*daily)+(int_2))&amp;gt;&lt;STRONG&gt;99.7&lt;/STRONG&gt; then&lt;/P&gt;&lt;P&gt;exp_val =&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Else if SC=&lt;STRONG&gt;2&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and&amp;nbsp; ((co2*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_2*daily)+(int_2))&amp;lt;&lt;STRONG&gt;99.7&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then&lt;/P&gt;&lt;P&gt;exp_val=(( co_dem_2 *((co2*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_2*daily)+(int_2))/100&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if SC=&lt;STRONG&gt;3&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and ((co3*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_3*daily)+(int_3))&amp;gt;&lt;STRONG&gt;99.7&lt;/STRONG&gt; then&lt;/P&gt;&lt;P&gt;exp_val =&lt;STRONG&gt;1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Else if SC=&lt;STRONG&gt;2&lt;/STRONG&gt; and u_rep =&lt;STRONG&gt;1&lt;/STRONG&gt; and&amp;nbsp; ((co3*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_3*daily)+(int_3))&amp;lt;&lt;STRONG&gt;99.7&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;then&lt;/P&gt;&lt;P&gt;exp_val=(( co_dem_3 *((co3*(Daily**&lt;STRONG&gt;2&lt;/STRONG&gt;))+(co_dem_3*daily)+(int_3))/100&lt;/P&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>Mon, 04 Nov 2019 15:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601396#M173944</guid>
      <dc:creator>niloya</dc:creator>
      <dc:date>2019-11-04T15:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601398#M173946</link>
      <description>&lt;P&gt;First you need to create working code without macros and without macro variables, for at least one case (for example when a=1). Your code as written does not work, and so any macro written using this code will not work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The structure you are using&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data.have.column1&lt;/PRE&gt;
&lt;P&gt;is not valid SAS syntax and will cause errors. Thus the first step is to fix this.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 15:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601398#M173946</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-11-04T15:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601450#M173976</link>
      <description>&lt;P&gt;You could use macro language.&amp;nbsp; But it is not clearly a good choice.&amp;nbsp; Arrays would work pretty well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.want;                            
set work.have;  

array co {6};
array co_cem_ {6};                           
array int_ {6};
 
if u_rep=1 and (1 &amp;lt;= SC &amp;lt;= 6) then do;
   exp_val =  (co{SC}*(Daily**2))+(co_dem_{SC}*daily)+(int_{SC});
   if exp_val &amp;gt;99.7 then exp_val =1;
   else exp_val = exp_val / 100;
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You'll have to check the formulas to make sure that I got them right.&amp;nbsp; They were not entirely consistent in your original post.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you will need to check what should happen when the formula generates 99.7 exactly.&amp;nbsp; Your logic didn't account for that, although I did assign values for those observations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the number of items changes, just change "6" to the proper number (in 4 places).&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2019 18:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601450#M173976</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-11-04T18:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601601#M174036</link>
      <description>&lt;P&gt;If you really want a macro program:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_sc_val(num);
data want;
set have;

initial_val=(co&amp;amp;num*(Daily**2)) + (co_dem_&amp;amp;num*Daily) + (int_&amp;amp;num);
exp_val=(co_dem_&amp;amp;num*(co&amp;amp;num*(Daily**2))) + (co_dem_&amp;amp;num*Daily) + (int_&amp;amp;num);

if u_rep=1 and sc=&amp;amp;num then do;
	if initial_val &amp;gt; 99.7 then exp_val=1;		/*1 if greater than 99.7*/
	if initial_val &amp;lt; 99.7 exp_val=exp_val/100;	/*computed value if less than 99.7*/
	else exp_val=.;					/*null if equal to 99.7*/
end;

run;
%mend get_sc_val;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Keep in mind that the macro variable "num" resolves to a string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the exact value of 99.7 is not handled. In the code above it will be assigned a null value indicated by a period. Just change this to whatever value you like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can initiate a run either for all values, using another macro program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro run_all_nums;
	%do i=1 %to 6;
	%get_sc_val(&amp;amp;i);
	%end;
%mend run_all_nums;

%run_all_nums; /*execute for all nums*/
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or one at a time, just change the value inside the parenthesis:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%get_sc_val(1); /*just execute for 1*/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Nov 2019 09:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601601#M174036</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2019-11-05T09:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601673#M174061</link>
      <description>You are a really good programmer. It is working. Thank you so much. I really appreciate your help</description>
      <pubDate>Tue, 05 Nov 2019 14:54:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601673#M174061</guid>
      <dc:creator>niloya</dc:creator>
      <dc:date>2019-11-05T14:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601789#M174116</link>
      <description>&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token macrobound"&gt;&lt;BR /&gt;Just a quick question if you dont mind. i need all numbers to show into one table from 1 to 6. When i run &lt;BR /&gt;the macro below, the table only show the i=6 result. Is there anything you would recommend?&lt;BR /&gt;&lt;BR /&gt;%macro&lt;/SPAN&gt; run_all_nums&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macrostatement"&gt;%do&lt;/SPAN&gt; i&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token macrostatement"&gt;%to&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;6&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macroname"&gt;%get_sc_val&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
	&lt;SPAN class="token macrostatement"&gt;%end&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token macrobound"&gt;%mend&lt;/SPAN&gt; run_all_nums&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token macroname"&gt;%run_all_nums&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Nov 2019 20:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601789#M174116</guid>
      <dc:creator>niloya</dc:creator>
      <dc:date>2019-11-05T20:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601854#M174149</link>
      <description>&lt;P&gt;Sorry my bad, to get separate tables for each value of num, when running them all at once, add _&amp;amp;num to the data set name, "want".&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want_&amp;amp;num;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Adding this, you can utilise the same macro program and append all the output datasets afterwards manually.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;&lt;BR /&gt;set want_1 want_2 want_3 want_4 want_5 want6;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;/*or*/&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set want_:;&lt;BR /&gt;run;&lt;/PRE&gt;
&lt;P&gt;The original code replaces the table "want" every time the macro program is run, that is why you only see the last value (6).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My first suggestion doesn't look to be the most efficient way of doing it if what you require is to have them all in one dataset, even if the desired output can be achieved with the first code. You can simply go over it with a do loop, try this code below instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro program;
data want;
set have;

%do i=1 %to 6;
%let num=&amp;amp;i;

initial_val=(co&amp;amp;num*(Daily**2)) + (co_dem_&amp;amp;num*Daily) + (int_&amp;amp;num);
exp_val=(co_dem_&amp;amp;num*(co&amp;amp;num*(Daily**2))) + (co_dem_&amp;amp;num*Daily) + (int_&amp;amp;num);

if u_rep=1 and sc=&amp;amp;num then do;
	if initial_val &amp;gt; 99.7 then exp_val=1;			/*1 if greater than 99.7*/
	if initial_val &amp;lt; 99.7 then exp_val=exp_val/100;	/*computed value if less than 99.7*/
	else exp_val=.;									/*null if equal to 99.7*/
end;

%end;
run;
%mend;

%program;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2019 03:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601854#M174149</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2019-11-06T03:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601986#M174219</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281550"&gt;@sustagens&lt;/a&gt;&lt;BR /&gt;You are the best ever. It is working beyond great. Million times thank you. You saved my life.</description>
      <pubDate>Wed, 06 Nov 2019 14:01:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-convert-macro/m-p/601986#M174219</guid>
      <dc:creator>niloya</dc:creator>
      <dc:date>2019-11-06T14:01:10Z</dc:date>
    </item>
  </channel>
</rss>

