<?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: Divide by 0 Error in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143300#M28670</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just to add, I think what you are trying to achieve is to code your own array processing.&amp;nbsp; You can do this, per example 2 below, however the use of arrays would be a better idea - example 1:&lt;/P&gt;&lt;P&gt;Example 1:&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array tot{4} 8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array perc{4} 8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{1}=6; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{2}=10;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{3}=8; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{4}=7; &lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=1 to 4;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if tot{i}=0 then perc{i}=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else perc{i}=tot{i} / 45;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example 2:&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;&amp;nbsp; array tot{4} 8.;&lt;BR /&gt;&amp;nbsp; array perc{4} 8.;&lt;BR /&gt;&amp;nbsp; tot{1}=6; &lt;BR /&gt;&amp;nbsp; tot{2}=10;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; tot{3}=8; &lt;BR /&gt;&amp;nbsp; tot{4}=7; &lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro Do_Array (Var=, Num=);&lt;BR /&gt;&amp;nbsp; %do var_num=1 %to &amp;amp;Num.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;var.&amp;amp;var_num.=0 %then %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; perc&amp;amp;var_num.=0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; perc&amp;amp;var_num.=&amp;amp;var.&amp;amp;var_num. / 45;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp; %end;&lt;BR /&gt;%mend Do_Array;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; set have;&lt;BR /&gt;&amp;nbsp; %do_Array (Var=tot,Num=4);&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 31 Jul 2014 09:04:02 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2014-07-31T09:04:02Z</dc:date>
    <item>
      <title>Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143296#M28666</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a pretty simple macro code:&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;%macro&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; do_array(var=, num=);&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %do&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; var_num = &lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-family: 'Courier New';"&gt;1&lt;/STRONG&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;%to&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &amp;amp;num;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &amp;amp;var&amp;amp;var_num = &lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-family: 'Courier New';"&gt;0&lt;/STRONG&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;%then&lt;/SPAN&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;%do&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&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; perc_&amp;amp;var&amp;amp;var_num = &lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-family: 'Courier New';"&gt;0&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else&lt;/SPAN&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;%do&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&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; perc_&amp;amp;var&amp;amp;var_num = &amp;amp;var&amp;amp;var_num / total;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;%mend&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Basically for all of the variables I'm just trying to get a percentage of the total. When I run it, I get an warning that I'm dividing by zero, but that's what the first step in the if statement is trying to avoid. Along with the warning, &lt;/SPAN&gt;&lt;SPAN style="color: black; font-family: 'Courier New'; font-size: 10pt; line-height: 1.5em; background-color: white;"&gt;instead of a 0 for the value, I'm getting a missing value.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Any suggestions?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Thanks&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jul 2014 22:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143296#M28666</guid>
      <dc:creator>BN_RN17</dc:creator>
      <dc:date>2014-07-30T22:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143297#M28667</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's the TOTAL variable you need to check for divide by 0.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jul 2014 23:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143297#M28667</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2014-07-30T23:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143298#M28668</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; I agree that the TOTAL variable should be checked to avoid the divide by 0 error message, but depending on how you are using your variables, I believe that this %IF statement possibly needs to change to a regular DATA step IF. For example, if VAR=X and Num=1, then your %IF test would seem to be asking this of the macro processor, as if the macro processor has visibility of the DATA step variable X1 (which it doesn't):&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: courier new,courier;"&gt;&lt;STRONG&gt;%if&lt;SPAN style="background: white;"&gt; xi = &lt;/SPAN&gt;&lt;SPAN style="background: white;"&gt;0&lt;/SPAN&gt; &lt;SPAN style="background: white;"&gt;%then&lt;/SPAN&gt; &lt;SPAN style="background: white;"&gt;%do&lt;/SPAN&gt;&lt;SPAN style="background: white;"&gt;;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: courier new,courier;"&gt;&lt;STRONG&gt;&lt;SPAN style="background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; perc_x1= &lt;/SPAN&gt;&lt;SPAN style="background: white;"&gt;0&lt;/SPAN&gt;&lt;SPAN style="background: white;"&gt;;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: courier new,courier;"&gt;&lt;STRONG&gt;&lt;SPAN style="background: white;"&gt;%end&lt;/SPAN&gt;&lt;SPAN style="background: white;"&gt;;&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: 'Courier New';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;which may not be what you think is going to happen. The %IF statement gets evaluated when the SAS Macro program is executed, but the SAS Macro facility is only generating code that will be executed. So, by the time the code goes to be executed against your data, there are &lt;STRONG style="text-decoration: underline;"&gt;no&lt;/STRONG&gt; macro statements left in the code. So generally, Macro %IF statements are NOT used to &lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;test&lt;/STRONG&gt;&lt;/SPAN&gt; data step variables. The value of your data step variables will change with every observation. At the point in time when the macro code is executing, and generating code, no data has actually been read yet. So if you want to test whether X1 is 0, then you need a "regular" DATA step IF.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;For example, if you have simple data like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;grp x1&amp;nbsp; x2&amp;nbsp; x3&amp;nbsp; x4&amp;nbsp; x5&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;AAA 5&amp;nbsp;&amp;nbsp; 10&amp;nbsp; 15&amp;nbsp;&amp;nbsp; .&amp;nbsp; 25&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;BBB 3&amp;nbsp;&amp;nbsp;&amp;nbsp; .&amp;nbsp; 11&amp;nbsp; 13&amp;nbsp; 17&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="font-family: courier new,courier;"&gt;CCC 4&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp; .&amp;nbsp;&amp;nbsp; 9&amp;nbsp; 11&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;and you want to calculate new variables &lt;SPAN style="font-family: courier new,courier;"&gt;pctx1, pctx2, pctx3, pctx4&lt;/SPAN&gt; and &lt;SPAN style="font-family: courier new,courier;"&gt;pctx5&lt;/SPAN&gt;, you can do it without any macro processing. See the attached screen shot.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;Of course, your program could be more complicated, but I wonder why you are mixing macro programming statements with DATA step variables.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background: white; color: black; font-family: arial,helvetica,sans-serif;"&gt;cynthia&lt;/SPAN&gt;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/11028i6479B90AD5FDE923/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="percent_in_array_no_macro.png" title="percent_in_array_no_macro.png" /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Jul 2014 23:59:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143298#M28668</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2014-07-30T23:59:04Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143299#M28669</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You're misunderstanding what the macro processor does, and when it does it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If we feed var=xy and num=3 to your macro, it will do the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;amp;var&amp;amp;var_num will evaluate to&lt;/P&gt;&lt;P&gt;xy1&lt;/P&gt;&lt;P&gt;xy2&lt;/P&gt;&lt;P&gt;xy3&lt;/P&gt;&lt;P&gt;respectively&lt;/P&gt;&lt;P&gt;All these are clearly not 0, so therefore the macro processor will supply the lines&lt;/P&gt;&lt;P&gt;perc_xy1 = xy1 / total;&lt;/P&gt;&lt;P&gt;perc_xy2 = xy2 / total;&lt;/P&gt;&lt;P&gt;perc_xy3 = xy3 / total;&lt;/P&gt;&lt;P&gt;to the SAS interpreter. Anytime total equates to zero, you get a warning.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The macro processor is invoked BEFORE any data step runs, and does its work without any knowledge about what happens during data step execution. It is only there to&lt;STRONG&gt; &lt;EM&gt;generate program text&lt;/EM&gt;&lt;/STRONG&gt;. Repeat the last sentence three times.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You most probably want this in your %do loop:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if total ne 0&lt;/P&gt;&lt;P&gt;then perc_&amp;amp;var&amp;amp;var_num = &amp;amp;var&amp;amp;var_num / total;&lt;/P&gt;&lt;P&gt;else perc_&amp;amp;var&amp;amp;var_num = 0;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This piece of code is then repeatedly supplied vor every var_num to the data step code.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jul 2014 08:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143299#M28669</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2014-07-31T08:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143300#M28670</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just to add, I think what you are trying to achieve is to code your own array processing.&amp;nbsp; You can do this, per example 2 below, however the use of arrays would be a better idea - example 1:&lt;/P&gt;&lt;P&gt;Example 1:&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array tot{4} 8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; array perc{4} 8.;&lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{1}=6; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{2}=10;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{3}=8; &lt;/P&gt;&lt;P&gt;&amp;nbsp; tot{4}=7; &lt;/P&gt;&lt;P&gt;&amp;nbsp; do i=1 to 4;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if tot{i}=0 then perc{i}=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else perc{i}=tot{i} / 45;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example 2:&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;&amp;nbsp; array tot{4} 8.;&lt;BR /&gt;&amp;nbsp; array perc{4} 8.;&lt;BR /&gt;&amp;nbsp; tot{1}=6; &lt;BR /&gt;&amp;nbsp; tot{2}=10;&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp; tot{3}=8; &lt;BR /&gt;&amp;nbsp; tot{4}=7; &lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro Do_Array (Var=, Num=);&lt;BR /&gt;&amp;nbsp; %do var_num=1 %to &amp;amp;Num.;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %if &amp;amp;var.&amp;amp;var_num.=0 %then %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; perc&amp;amp;var_num.=0;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %else %do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; perc&amp;amp;var_num.=&amp;amp;var.&amp;amp;var_num. / 45;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; %end;&lt;BR /&gt;&amp;nbsp; %end;&lt;BR /&gt;%mend Do_Array;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; set have;&lt;BR /&gt;&amp;nbsp; %do_Array (Var=tot,Num=4);&lt;BR /&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jul 2014 09:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143300#M28670</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-07-31T09:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143301#M28671</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The DIVIDE function returns missing I when you divide by 0.&amp;nbsp; No message.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;23&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data _null_;&lt;/P&gt;&lt;P&gt;24&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = divide(1,0);&lt;/P&gt;&lt;P&gt;25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put x=;&lt;/P&gt;&lt;P&gt;26&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;x=I&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 10pt; line-height: 1.5em;"&gt;27&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;28&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; %put NOTE: %sysfunc(divide(1,0));&lt;/P&gt;&lt;P&gt;NOTE: I&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jul 2014 12:47:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143301#M28671</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2014-07-31T12:47:19Z</dc:date>
    </item>
    <item>
      <title>Re: Divide by 0 Error in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143302#M28672</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry about that, I originally had total, but I changed it to the variable to see if it would make a difference. Forgot to change it back before copying it here. Anyway, thank you all so much for your responses.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 31 Jul 2014 14:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Divide-by-0-Error-in-Macro/m-p/143302#M28672</guid>
      <dc:creator>BN_RN17</dc:creator>
      <dc:date>2014-07-31T14:03:32Z</dc:date>
    </item>
  </channel>
</rss>

