<?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: Arrays retain values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594609#M170866</link>
    <description>&lt;P&gt;Thank you, Reeza, for the quick reply.&lt;/P&gt;&lt;P&gt;The reason I'm trying to use arrays is because I need to perform this type of calculation for about 50 variables.&lt;/P&gt;&lt;P&gt;But it seems, like you said, array function doesn't retain values across the rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2019 03:21:21 GMT</pubDate>
    <dc:creator>Naz10</dc:creator>
    <dc:date>2019-10-08T03:21:21Z</dc:date>
    <item>
      <title>Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594599#M170862</link>
      <description>&lt;P&gt;I have a code where I use an array of about 50 variables and perform lagged calculation.&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input Date mmddyy10. Test_1 Test_2 Test_3;&lt;BR /&gt;datalines;&lt;BR /&gt;09/30/2019 200 150 100&lt;BR /&gt;10/31/2019 0.004 0.0076 0.0074&lt;BR /&gt;11/30/2019 0.006 0.0102 0.0129&lt;BR /&gt;12/31/2019 0.0035 0.0102 0.0129&lt;BR /&gt;01/31/2020 0.014 0.0101 0.089&lt;BR /&gt;02/29/2020 0.056 -0.0103 0.055&lt;BR /&gt;03/30/2020 0.056 0.0155 0.078&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let out_list_index1 = Test_1 Test_2 Test_3;&lt;BR /&gt;%let begin_date="30Apr2020"d;&lt;BR /&gt;%put &amp;amp;out_list_index1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array list{*} &amp;amp;out_list_index1;&lt;BR /&gt;do i=1 to dim(list) while(date &amp;lt;= &amp;amp;begin_date);&lt;BR /&gt;list(i)=lag(list{i})*(1+list{i});&lt;BR /&gt;end;&lt;BR /&gt;drop i;&lt;BR /&gt;run&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I'm having trouble setting up the code correctly. The idea here is not just to lag original variables, but lag newly calculated variables, i.e. for new row of data grab the lag of the previous calculated row and use that to calculate the new row. Example below shows how the before and after (1 variable as an example)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;E.g.&lt;/P&gt;&lt;P&gt;have:&lt;/P&gt;&lt;P&gt;var1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;2&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;want:&lt;/P&gt;&lt;P&gt;var1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;(2+1)*1=3&lt;/P&gt;&lt;P&gt;(3+1)*3=12&lt;/P&gt;&lt;P&gt;(4+1)*12=60&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>Tue, 08 Oct 2019 20:56:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594599#M170862</guid>
      <dc:creator>Naz10</dc:creator>
      <dc:date>2019-10-08T20:56:47Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594603#M170864</link>
      <description>&lt;P&gt;I don't think arrays are the correct solution here. In SAS arrays are only variable shortcuts and refer to data within a single row, not across rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input var1;
cards;
1
2
3
4
;;;;

data want;
set have;
retain var2;

if _n_ = 1 then var2=var1;
else var1 = (var1+1)*var2;

var2=var1;

run;

proc print;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294167"&gt;@Naz10&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a code where I use an array of about 50 variables and perform lagged calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array list(*) &amp;amp;out_list_index1;&lt;BR /&gt;do i=1 to dim(list) while(date &amp;lt;= &amp;amp;begin_date);&lt;BR /&gt;list(i)=lag(list(i))*(1+list(i));&amp;nbsp; /*grab new &amp;lt;list&amp;gt; */&lt;BR /&gt;end;&lt;BR /&gt;drop i;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I'm having trouble setting up the code correctly. The idea here is not just to lag original variables, but lag newly calculated variables, i.e. for new row of data grab the lag of the previous calculated row and use that to calculate the new row. Example below shows how the before and after (1 variable as an example)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;E.g.&lt;/P&gt;
&lt;P&gt;have:&lt;/P&gt;
&lt;P&gt;var1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;2&lt;/P&gt;
&lt;P&gt;3&lt;/P&gt;
&lt;P&gt;4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;want:&lt;/P&gt;
&lt;P&gt;var1&lt;/P&gt;
&lt;P&gt;1&lt;/P&gt;
&lt;P&gt;(2+1)*1=3&lt;/P&gt;
&lt;P&gt;(3+1)*3=12&lt;/P&gt;
&lt;P&gt;(4+1)*12=60&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;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 03:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594603#M170864</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-10-08T03:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594609#M170866</link>
      <description>&lt;P&gt;Thank you, Reeza, for the quick reply.&lt;/P&gt;&lt;P&gt;The reason I'm trying to use arrays is because I need to perform this type of calculation for about 50 variables.&lt;/P&gt;&lt;P&gt;But it seems, like you said, array function doesn't retain values across the rows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 03:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594609#M170866</guid>
      <dc:creator>Naz10</dc:creator>
      <dc:date>2019-10-08T03:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594610#M170867</link>
      <description>&lt;P&gt;Never use lag within a conditional code block. Use a temporary array instead. Temporary arrays are always retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array list{*} &amp;amp;out_list_index1;
array lag{999} _temporary_; /* Adjust 999 */
do i=1 to dim(list) while(date &amp;lt;= &amp;amp;begin_date);
	list{i}=lag{i}*(1+list{i});
	end;
do i=1 to dim(list);
	lag{i} = list{i};
	end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 03:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594610#M170867</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-10-08T03:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594725#M170943</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294167"&gt;@Naz10&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, Reeza, for the quick reply.&lt;/P&gt;
&lt;P&gt;The reason I'm trying to use arrays is because I need to perform this type of calculation for about 50 variables.&lt;/P&gt;
&lt;P&gt;But it seems, like you said, array function doesn't retain values across the rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The RETAIN statement is used to mark variables to have their values retained. (not cleared to missing at start of next iteration of the data step).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  array old var1-var50;
  array new new1-new50;
  retain new1-new50;
  do over old;
    if _n_=1 then new=old;
    else new=(old+1)*new;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 12:56:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594725#M170943</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-08T12:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594788#M170973</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294167"&gt;@Naz10&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you, Reeza, for the quick reply.&lt;/P&gt;
&lt;P&gt;The reason I'm trying to use arrays is because I need to perform this type of calculation for about 50 variables.&lt;/P&gt;
&lt;P&gt;But it seems, like you said, array function doesn't retain values across the rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I suggest providing a small example data set using maybe 3 of those 50 variables and just enough records to exercise your logic. Then show the result of what this process should look like for that example data set.&lt;/P&gt;
&lt;P&gt;Best would be to provide the example data in the form of a data step so we can test code against it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 15:45:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594788#M170973</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-10-08T15:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594866#M171018</link>
      <description>&lt;P&gt;Thank you. I updated the original post with a sample data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2019 19:01:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594866#M171018</guid>
      <dc:creator>Naz10</dc:creator>
      <dc:date>2019-10-08T19:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: Arrays retain values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594923#M171056</link>
      <description>&lt;P&gt;The code worked! Thank you! I modified it with a macro and here is the final code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let word_cnt=%sysfunc(countw(&amp;amp;out_list_index1));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array list{*} &amp;amp;out_list_index1;&lt;BR /&gt;array lag{&amp;amp;word_cnt} _temporary_;&lt;BR /&gt;do i=1 to dim(list) while(date &amp;lt;= &amp;amp;begin_date.);&lt;BR /&gt;list{i}=lag{i}*(1+list{i});&lt;BR /&gt;end;&lt;BR /&gt;do i=1 to dim(list);&lt;BR /&gt;lag{i} = list{i};&lt;BR /&gt;end;&lt;BR /&gt;drop i;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2019 00:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrays-retain-values/m-p/594923#M171056</guid>
      <dc:creator>Naz10</dc:creator>
      <dc:date>2019-10-09T00:36:16Z</dc:date>
    </item>
  </channel>
</rss>

