<?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: Creating New Values Based on Last Observation's Number and current observation's percent in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353895#M82669</link>
    <description>&lt;P&gt;Assuming your data is in order,&amp;nbsp;a simple program would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;retain new_pop;&lt;/P&gt;
&lt;P&gt;if year = 2017 then new_pop = population;&lt;/P&gt;
&lt;P&gt;else new_pop = new_pop * growth_percent;&lt;/P&gt;
&lt;P&gt;drop population;&lt;/P&gt;
&lt;P&gt;rename new_pop = population;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Apr 2017 20:55:50 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2017-04-26T20:55:50Z</dc:date>
    <item>
      <title>Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353878#M82662</link>
      <description>&lt;P&gt;I am looking to populate a variable that has missing numbers for future years (say 2018 through 2030). What I have is a population number for 2017, and I have given growth percents for the future years. So, for 2018, I want to take the population value (say 800) from the last observation for year 2017 and multiply that by my growth percent that I have stored in the observation for 2018. I tried using a retain statement, and I was able to get this to work for the first year, but I am unable to get it to carry over for the remaining years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data structure is as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;population&lt;/TD&gt;&lt;TD&gt;growth_percent&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2017&lt;/TD&gt;&lt;TD&gt;800&lt;/TD&gt;&lt;TD&gt;1.01&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2018&lt;/TD&gt;&lt;TD&gt;x1&lt;/TD&gt;&lt;TD&gt;1.06&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2019&lt;/TD&gt;&lt;TD&gt;x2&lt;/TD&gt;&lt;TD&gt;1.08&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2020&lt;/TD&gt;&lt;TD&gt;x3&lt;/TD&gt;&lt;TD&gt;1.11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2021&lt;/TD&gt;&lt;TD&gt;x4&lt;/TD&gt;&lt;TD&gt;1.5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2022&lt;/TD&gt;&lt;TD&gt;x5&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to dynamically calculate the numbers for x1 - x5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;X1 should equal 800 * 1.06. Then X2 should eqaul X1 * 1.08.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried the following code, but I am only able to populate X1. I can't figure out how to pass the X1 value to the next observation in order to calculate X2:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; populations;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; pop; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;retain&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; lastpop;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;output&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;; &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;lastpop= (population) * (growth_percent&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;); &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Much Thanks!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:20:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353878#M82662</guid>
      <dc:creator>KellyMcAllister</dc:creator>
      <dc:date>2017-04-26T20:20:26Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353881#M82665</link>
      <description>&lt;P&gt;Kelly,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recommend that you try to get that original number out of the x1, x2 etc. column. &amp;nbsp;From there, you can multiply it against the growth percentage.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assuming that it is always the least year that gives you the original population, you can do the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;  
input year population $ growth_percent  ;
FORMAT growth_percent 8.4;
cards;  
2017	800	1.01
2018	x1	1.06
2019	x2	1.08
2020	x3	1.11
2021	x4	1.5
2022	x5	1
;
run;

PROC SQL;
CREATE TABLE MIN AS 
SELECT INPUT(POPULATION, 8.) AS MIN_POP FORMAT=8. FROM HAVE WHERE YEAR IN (SELECT MIN(YEAR) FROM HAVE);
QUIT;

PROC SQL;
CREATE TABLE HAVE AS 
SELECT *, MIN.MIN_POP * GROWTH_PERCENT AS TOTAL_GROWTH FROM HAVE, MIN;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There are many other ways to do this I am sure, but I prefer SQL because it may be used in many other statistical programs (e.g. R, SPSS, etc.)&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:32:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353881#M82665</guid>
      <dc:creator>thomp7050</dc:creator>
      <dc:date>2017-04-26T20:32:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353892#M82668</link>
      <description>&lt;P&gt;I need to be able to pass the TOTAL_GROWTH # to the next observation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2017: 800 * 1.01 = 808&lt;/P&gt;&lt;P&gt;2018: 808 * 1.06 = 856.48&amp;nbsp;&amp;nbsp; (****not: 800 *1.06 = 848)&lt;/P&gt;&lt;P&gt;2019: 856.48 * 1.08 = 924.9984&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and so on...&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353892#M82668</guid>
      <dc:creator>KellyMcAllister</dc:creator>
      <dc:date>2017-04-26T20:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353895#M82669</link>
      <description>&lt;P&gt;Assuming your data is in order,&amp;nbsp;a simple program would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;retain new_pop;&lt;/P&gt;
&lt;P&gt;if year = 2017 then new_pop = population;&lt;/P&gt;
&lt;P&gt;else new_pop = new_pop * growth_percent;&lt;/P&gt;
&lt;P&gt;drop population;&lt;/P&gt;
&lt;P&gt;rename new_pop = population;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 20:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353895#M82669</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-26T20:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353900#M82671</link>
      <description>&lt;P&gt;Thank you so much! This is exactly what I needed.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 21:05:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353900#M82671</guid>
      <dc:creator>KellyMcAllister</dc:creator>
      <dc:date>2017-04-26T21:05:13Z</dc:date>
    </item>
    <item>
      <title>Re: Creating New Values Based on Last Observation's Number and current observation's percent</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353901#M82672</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2 (drop=lastpop);
  set have;
  if pop=. then pop=lastpop*gr_pct;
  retain lastpop;
  lastpop=pop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program assumes that the first observation always has a proper value for population.&amp;nbsp;&amp;nbsp; Also, anytime a new value for population is encountered in the input data step, the process start over.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2017 21:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-New-Values-Based-on-Last-Observation-s-Number-and/m-p/353901#M82672</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-04-26T21:05:25Z</dc:date>
    </item>
  </channel>
</rss>

