<?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: ARRAY referring to current row in do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305069#M65032</link>
    <description>&lt;P&gt;There can be many additional issues once this one is cleared up, but this error message has an easy solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The array names and array references use different names.&amp;nbsp; You added an "s" at the end of the array names, but left out the "s" later on when referring to array elements.&lt;/P&gt;</description>
    <pubDate>Mon, 17 Oct 2016 13:04:33 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-10-17T13:04:33Z</dc:date>
    <item>
      <title>ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305045#M65024</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;basically I'm writing a data step that for each ID writes in a new variable all the variables that are changed from the previous day&lt;/P&gt;&lt;P&gt;I'm thinking something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;PROC SORT DATA=ccr_report_power_hist OUT=hist;
	BY ID_ANAG DESCENDING valdate;
run;

DATA hist;
	SET hist;
	BY ID_ANAG;
	IF _n_ 	EQ 1 THEN;
		DO;
			current_line = -1;
			RETAIN current_line;
		END;
	IF first.id_anag AND last.id_anag EQ 0 THEN 
		DO;
			first_date = valdate;
			ARRAY current_values[*] _CHARACTER_;
			current_line = _n_;
		END;
	IF _n_ = current_line + 1 THEN
		DO;
			first_date = valdate;
			ARRAY temp_values[*] _CHARACTER_;
			DO i=1 TO DIM(temp_values);
				same_values = '';
				IF current_value[i] EQ temp_value[i] THEN
				DO;
					same_values = CATS(same_values, VNAME(current_value[i]));
				END;
			END;
		END;	
run;&lt;/PRE&gt;&lt;P&gt;I think I'm misunderstanding how arrays are used in sas. I get the "Undeclared array referenced" for both the arrays, but I'm quite sure that the first IF is entered at least once.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2016 11:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305045#M65024</guid>
      <dc:creator>Crysis85</dc:creator>
      <dc:date>2016-10-17T11:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305047#M65025</link>
      <description>&lt;P&gt;forgot to retain current_line. Wasn't the problem tho. edited the OP&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2016 11:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305047#M65025</guid>
      <dc:creator>Crysis85</dc:creator>
      <dc:date>2016-10-17T11:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305051#M65028</link>
      <description>&lt;P&gt;RETAIN and array declaration should never be written inside conditional blocks. This is misleading, as retain and array are acted upon during data step compilation, not data step execution. Therefore what you write is misleading.&lt;/P&gt;
&lt;P&gt;This is also the reason you declare two arrays containing the same variables (and values). One would suffice.&lt;/P&gt;
&lt;PRE&gt;IF current_value[i] EQ temp_value[i]&lt;/PRE&gt;
&lt;P&gt;will always be true, as you just reference the same datastep variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, your same_values would end up containing only the last different value, since you set it to empty within the do loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you probably wanted was&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data hist;
set hist;
by id_anag;
array temp {*} _character_;
length same_values $500; * put sufficient length here;
same_values = '';
do i = 1 to dim(temp);
  if temp{i} eq lag(temp{i}) then same_values = cats(trim(same_values),vname(temp{i}));
end;
if first.id_anag then same_values = '';
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2016 11:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305051#M65028</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-17T11:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305069#M65032</link>
      <description>&lt;P&gt;There can be many additional issues once this one is cleared up, but this error message has an easy solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The array names and array references use different names.&amp;nbsp; You added an "s" at the end of the array names, but left out the "s" later on when referring to array elements.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2016 13:04:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305069#M65032</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-10-17T13:04:33Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305130#M65056</link>
      <description>&lt;P&gt;Thanks your help was very instructive, I find the workings of arrays in SAS a little confusing.&lt;/P&gt;&lt;P&gt;Forgot the existence of LAG() very useful.&lt;/P&gt;&lt;P&gt;I have but one question. You put&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;id_anag &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; same_values &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;''&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;at the end so that is it's the first row on a block it is overwrited, isn't possible to encase the whole do loop in an if-then and avoid to execute it when it's the first row in a block?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Oct 2016 15:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305130#M65056</guid>
      <dc:creator>Crysis85</dc:creator>
      <dc:date>2016-10-17T15:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305289#M65103</link>
      <description>&lt;P&gt;That is done because of the lag() function. This function fills its FIFO queue only when called, so it is usually a bad idea to put it inside a conditional &lt;U&gt;block&lt;/U&gt;. Using it in the if &lt;U&gt;condition&lt;/U&gt;, OTOH, makes sure that it is called exactly once per datastep iteration and therefore always holds the value from the previous observation, and not one before that.&lt;/P&gt;
&lt;P&gt;So instead of creating same_values conditionally, I instead cleaned it up afterwards in case a new group starts.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2016 06:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305289#M65103</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-18T06:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAY referring to current row in do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305297#M65108</link>
      <description>&lt;P&gt;Very clear. Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Oct 2016 06:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAY-referring-to-current-row-in-do-loop/m-p/305297#M65108</guid>
      <dc:creator>Crysis85</dc:creator>
      <dc:date>2016-10-18T06:49:02Z</dc:date>
    </item>
  </channel>
</rss>

