<?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: Help with using two arrays please in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795211#M255037</link>
    <description>&lt;P&gt;I'd just use retain combined with a reversed sorting:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA=annual_cpi_01; by descending year; RUN;

data want;
   set annual_cpi_01;
   by descending year;
   retain total_cpi .;
   if total_cpi=. then total_cpi=annual_cpi;
   else if annual_cpi^=. then total_cpi=total_cpi*annual_cpi;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 09 Feb 2022 16:35:44 GMT</pubDate>
    <dc:creator>Oligolas</dc:creator>
    <dc:date>2022-02-09T16:35:44Z</dc:date>
    <item>
      <title>Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795207#M255033</link>
      <description>&lt;P&gt;Hi, I feel like I should know how to do this but am struggling to get my head around it. I have created a dataset that contains the cpi for the last six years (from 2016 - 2021). What I want to do is for each year, multiply the starting point by the figure for the subsequent years and ending up with what the total cpi for each starting year would be in 2022. So for 2016, I would want 2016 * 2017 * 2018 * 2019 * 2020 * 2021, whist for 2020, I would only want 2020 * 2021.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have been able to write code that does this but I just feel like there has to be a cleverer way of doing it, which is why I'm asking all you clever people!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my existing code: -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data annual_cpi_01;&lt;BR /&gt;input year annual_cpi;&lt;BR /&gt;datalines;&lt;BR /&gt;2016 1.007&lt;BR /&gt;2017 1.027&lt;BR /&gt;2018 1.025&lt;BR /&gt;2019 1.018&lt;BR /&gt;2020 1.009&lt;BR /&gt;2021 1.026&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data annual_cpi_02;&lt;BR /&gt;set annual_cpi_01;&lt;BR /&gt;retain _2016 _2017 _2018 _2019 _2020;&lt;BR /&gt;&lt;BR /&gt;if _n_ = 1 then _2016 = annual_cpi;&lt;BR /&gt;else _2016 = _2016 * annual_cpi;&lt;BR /&gt;&lt;BR /&gt;if _n_ = 2 then _2017 = annual_cpi;&lt;BR /&gt;else _2017 = _2017 * annual_cpi;&lt;BR /&gt;&lt;BR /&gt;if _n_ = 3 then _2018 = annual_cpi;&lt;BR /&gt;else _2018 = _2018 * annual_cpi;&lt;BR /&gt;&lt;BR /&gt;if _n_ = 4 then _2019 = annual_cpi;&lt;BR /&gt;else _2019 = _2019 * annual_cpi;&lt;BR /&gt;&lt;BR /&gt;if _n_ = 5 then _2020 = annual_cpi;&lt;BR /&gt;else _2020 = _2020 * annual_cpi;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If anyone could suggest an alternative, I'd really appreciate it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks,&lt;/P&gt;&lt;P&gt;Rob&lt;/P&gt;</description>
      <pubDate>Wed, 09 Feb 2022 16:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795207#M255033</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2022-02-09T16:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795208#M255034</link>
      <description>Sorry, the reason for the title mentioning arrays is because I thought arrays might be the way forward but wasn't sure how I could get it to cycle through the different options when one would be going from 1 to 5 and the other would be going from 2016 - 2020. The other reason I thought arrays would work better is because I thought I could future proof it a bit more and not have to add lines of code for each new years worth of data.&lt;BR /&gt;&lt;BR /&gt;Thanks again</description>
      <pubDate>Wed, 09 Feb 2022 16:23:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795208#M255034</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2022-02-09T16:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795211#M255037</link>
      <description>&lt;P&gt;I'd just use retain combined with a reversed sorting:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SORT DATA=annual_cpi_01; by descending year; RUN;

data want;
   set annual_cpi_01;
   by descending year;
   retain total_cpi .;
   if total_cpi=. then total_cpi=annual_cpi;
   else if annual_cpi^=. then total_cpi=total_cpi*annual_cpi;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Feb 2022 16:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795211#M255037</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-02-09T16:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795234#M255043</link>
      <description>&lt;P&gt;Good approach.&amp;nbsp; I would suggest a little tweak and a little simplification:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set annual_cpi_01;
   by descending year;
   retain total_cpi  1;
   if annual_cpi ne . then total_cpi = total_cpi * annual_cpi;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Feb 2022 17:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795234#M255043</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-02-09T17:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795252#M255056</link>
      <description>&lt;P&gt;You don't really need two arrays.&amp;nbsp; You can use the same array twice, once to record the annual cpi's, the second time to update those cpi's prior to output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program does require two passes through the data, but avoids a sort step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data annual_cpi_01;
input year annual_cpi;
datalines;
2016 1.007
2017 1.027
2018 1.025
2019 1.018
2020 1.009
2021 1.026
run;

data want (drop=_:);
  array cpi_array  {2016:2021} _temporary_;
  set annual_cpi_01 (in=firstpass) annual_cpi_01 (in=secondpass);

  if firstpass then cpi_array{year}=annual_cpi ;

  if secondpass;

  if year=2016 then do _y_=2020 to 2016 by -1;
    cpi_array{_Y_}=  cpi_array{_Y_}* cpi_array{_Y_+1};
  end;
  total=cpi_array{year};
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The point of the CPI_ARRAY array is that (1) it is directly indexed by YEAR, and (2) it is temporary.&amp;nbsp; The latter point means that the values in the array are retained while all observations are read (and reread), and that those values are not stored in a variable that will be automatically output.&amp;nbsp; i.e. no extra variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first pass of the data just populates the CPI_ARRAY.&amp;nbsp; Then, at the beginning of the second pass (i.e. the subsetting IF SECONDPASS statement has been satisfied, and the year=2016) the CPI_ARRAY is updated according to your specification.&amp;nbsp; Finally, as the second pass of data is processed total_cpi is retrieved from the updated array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes data are sorted by ascending YEAR.&amp;nbsp; Actually, the data don't have to be sorted by year, but it does require that the first observation is the earliest year.&amp;nbsp; Other years can be in any order.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Feb 2022 17:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795252#M255056</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-02-09T17:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795443#M255152</link>
      <description>Thank you, this is great. For some reason, I had in my head I needed to have the values through each interim year which is what my code produces but the reality is I only need to know the impact now and this does exactly what I need. Thanks again</description>
      <pubDate>Thu, 10 Feb 2022 09:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795443#M255152</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2022-02-10T09:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795444#M255153</link>
      <description>Brilliant, thank you</description>
      <pubDate>Thu, 10 Feb 2022 09:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795444#M255153</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2022-02-10T09:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Help with using two arrays please</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795445#M255154</link>
      <description>Thanks for this, a really interesting solution, and not something I've seen done before in terms of explicitly making two passes through the data - you've definitely given me something to investigate further. Thanks again</description>
      <pubDate>Thu, 10 Feb 2022 09:06:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-using-two-arrays-please/m-p/795445#M255154</guid>
      <dc:creator>robulon</dc:creator>
      <dc:date>2022-02-10T09:06:36Z</dc:date>
    </item>
  </channel>
</rss>

