<?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: Getting incorrect result with array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884488#M349436</link>
    <description>&lt;P&gt;A description of what that code is supposed to accomplish would be helpful. Examples of input data and expected/desired output&amp;nbsp; would help as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When given code that gets "inconsistent result" without knowing the input or expected output we have to make many guesses as to what is going on and are quite likely to miss something.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2023 14:42:25 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-07-12T14:42:25Z</dc:date>
    <item>
      <title>Getting incorrect result with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884424#M349401</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First of all I want to thank this community which has been very helpful.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am getting inconsistent result with a array program I am using. I am trying to sum different variables at the same time using array but I am getting incorrect results. Please help me pointing out the issue with the program.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;by ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;array class (*) CLASS_1-CLASS_30;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;array add (*) SUM_CLASS_1-SUM_CLASS_30;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;do i=1 to dim(class);&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.ID then add(i) =0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;add(i) + class(i);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if last.ID;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;keep ID&amp;nbsp;SUM_CLASS_1-SUM_CLASS_30;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note:&amp;nbsp;CLASS_1-CLASS_30 variables has numeric value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&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;</description>
      <pubDate>Wed, 12 Jul 2023 02:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884424#M349401</guid>
      <dc:creator>abhi309</dc:creator>
      <dc:date>2023-07-12T02:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Getting incorrect result with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884430#M349402</link>
      <description>&lt;P&gt;There are two clear logic errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First you did not RETAIN the variables defined in the ADD array.&amp;nbsp; So on each new iteration of the data step they will be set to missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second you are stopping the data step in the middle of the DO loop.&amp;nbsp; So only on the last observation in the ID group will any variable other than the first variable in the array have had a chance to have been incremented.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
     set have;
     by ID;
     array class CLASS_1-CLASS_30;
     array add SUM_CLASS_1-SUM_CLASS_30;
     retain SUM_CLASS_1-SUM_CLASS_30;
     if first.id then call missing(of add[*]);
     do i=1 to dim(class);  
           add[i] + class[i];
     end;
     if last.ID;
     keep ID SUM_CLASS_1-SUM_CLASS_30;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 03:22:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884430#M349402</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-12T03:22:07Z</dc:date>
    </item>
    <item>
      <title>Re: Getting incorrect result with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884449#M349418</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439280"&gt;@abhi309&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unless this is an exercise in DATA step programming, I would prefer PROC SUMMARY for this task:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
by id;
var class_1-class_30;
output out=want(drop=_:) sum=sum_class_1-sum_class_30;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: The RETAIN in the DATA step is redundant thanks to the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1dfiqj146yi2cn1maeju9wo7ijs.htm" target="_blank" rel="noopener"&gt;Sum statement&lt;/A&gt;. (Interestingly, even using the Sum statement with only&amp;nbsp;&lt;EM&gt;one&lt;/EM&gt; array reference, say &lt;FONT face="courier new,courier"&gt;add[17]&lt;/FONT&gt;, would imply a RETAIN for &lt;EM&gt;all&lt;/EM&gt; 30 elements of array &lt;FONT face="courier new,courier"&gt;add&lt;/FONT&gt;.)&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 08:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884449#M349418</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-07-12T08:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Getting incorrect result with array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884488#M349436</link>
      <description>&lt;P&gt;A description of what that code is supposed to accomplish would be helpful. Examples of input data and expected/desired output&amp;nbsp; would help as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When given code that gets "inconsistent result" without knowing the input or expected output we have to make many guesses as to what is going on and are quite likely to miss something.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 14:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-incorrect-result-with-array/m-p/884488#M349436</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-07-12T14:42:25Z</dc:date>
    </item>
  </channel>
</rss>

