<?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: Iterative Loop in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922307#M41384</link>
    <description>&lt;P&gt;Regarding the data, I am trying to calculate the year-over-year forecast for each monthly data point. Thus, in the full dataset, I can capture some seasonality features. That is, the value in January 2020 will be quite different than the value in August 2020, so I can't iterate growth month-over-month.&lt;/P&gt;</description>
    <pubDate>Fri, 29 Mar 2024 21:11:31 GMT</pubDate>
    <dc:creator>sas_user_1001</dc:creator>
    <dc:date>2024-03-29T21:11:31Z</dc:date>
    <item>
      <title>Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922292#M41380</link>
      <description>&lt;P&gt;I'm trying to get a sample of data I have to grab a lagged value and pull it forward and have it by 5% larger than the last value . For instance:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;input year month &amp;nbsp;x_var;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;2020 1 5&lt;/P&gt;
&lt;P&gt;2020 2 10&lt;/P&gt;
&lt;P&gt;2020 3 15&lt;/P&gt;
&lt;P&gt;2020 4 25&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to end up with is the following output:&lt;/P&gt;
&lt;P&gt;2021 1 5.25&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;*Note: 5*1.05;&lt;/P&gt;
&lt;P&gt;2021 2 10.5&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;*Note: 10*1.05;&lt;/P&gt;
&lt;P&gt;2021 3 15.75&amp;nbsp; &amp;nbsp;*Note: 15*1.05;&lt;/P&gt;
&lt;P&gt;2021 4 26.25&amp;nbsp; &amp;nbsp;*Note:25*1.05;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2022 1 5.5125&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;*Note: 5.25*1.05;&lt;/P&gt;
&lt;P&gt;2022 2 11.025&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;*Note: 10.5*1.05;&lt;/P&gt;
&lt;P&gt;2022 3 16.5375&amp;nbsp; &amp;nbsp;*Note: 15.75*1.05;&lt;/P&gt;
&lt;P&gt;2022 4 27.5625&amp;nbsp; &amp;nbsp;*Note: 26.25*1.05;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ideally, I want to be able to carry forward for more years, but I'm trying to get this to work for a small sample of data first. I tried the following, but it isn't producing the desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;data want;&lt;/DIV&gt;
&lt;DIV&gt;set have;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;do year=2021 to 2022;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; do month=1 to 4;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;x_var = lag4(x_var);&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;output;&lt;/DIV&gt;
&lt;DIV&gt;end;&lt;/DIV&gt;
&lt;DIV&gt;end;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;</description>
      <pubDate>Fri, 29 Mar 2024 18:39:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922292#M41380</guid>
      <dc:creator>sas_user_1001</dc:creator>
      <dc:date>2024-03-29T18:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922294#M41381</link>
      <description>&lt;P&gt;It does not look like you are doing anything that needs lags.&amp;nbsp; Instead if looks like you are converting one observation into 3.&amp;nbsp; This code gets the data you show.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input year month  x_var;
datalines;
2020 1 5
2020 2 10
2020 3 15
2020 4 25
;

data want;
  set have;
  new=x_var;
  do year=year to year+2 ;
    output;
    new=new*(1+x_var/100);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can add a PROC SORT to change the observation order if you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But your calculations seem strange.&amp;nbsp; Normally you would have a separate variables for the current amount and the percent increase.&amp;nbsp; &amp;nbsp;But you seem to be using X_VAR as both the percentage to increase and the starting value.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 18:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922294#M41381</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-29T18:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922298#M41382</link>
      <description>&lt;P&gt;The output from that failed attempt should have shown you that the Lag inside a loop is reusing the same value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code "works" for this example:&lt;/P&gt;
&lt;PRE&gt;data have;
input year month  x_var;
datalines;
2020 1 5
2020 2 10
2020 3 15
2020 4 25
;


data example ;
  set have;
  do year=year to (year+1);
     output;
     x_var= x_var* 1.05;
  end;
  output;
run;

proc sort data=example;
   by year month;
run;&lt;/PRE&gt;
&lt;P&gt;Incrementing by year and then sorting is going be much more efficient than trying "lag" to anything.&lt;/P&gt;
&lt;P&gt;Note with the first output at the top of the iterated Do /end block writes the original values. Then calculates the next value which gets written. The second output is the write the last x_var.&lt;/P&gt;
&lt;P&gt;Then SORT to be the order you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The trick is if you are going to change the incrementing value at different years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your original question with all those ugly variables then you would place them into an array and then iterate over the array where the X_var single assignment is.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 19:10:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922298#M41382</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-29T19:10:33Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922305#M41383</link>
      <description>&lt;P&gt;Thanks -- yes, I plan to change the growth rate for year further down the line, so will need to figure out how to implement that. For instance, I may have the values grow at 5% for the first couple years, but then change it to 3% the next to years, then 1%, and so on...&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 21:05:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922305#M41383</guid>
      <dc:creator>sas_user_1001</dc:creator>
      <dc:date>2024-03-29T21:05:08Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922307#M41384</link>
      <description>&lt;P&gt;Regarding the data, I am trying to calculate the year-over-year forecast for each monthly data point. Thus, in the full dataset, I can capture some seasonality features. That is, the value in January 2020 will be quite different than the value in August 2020, so I can't iterate growth month-over-month.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 21:11:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922307#M41384</guid>
      <dc:creator>sas_user_1001</dc:creator>
      <dc:date>2024-03-29T21:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922321#M41385</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/463968"&gt;@sas_user_1001&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks -- yes, I plan to change the growth rate for year further down the line, so will need to figure out how to implement that. For instance, I may have the values grow at 5% for the first couple years, but then change it to 3% the next to years, then 1%, and so on...&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could place the rates into a temporary array defined to use the YEAR as the index IF every observation in that year is going to use the same rate. Example:&lt;/P&gt;
&lt;PRE&gt;data _null_;
   array rate(2010:2015) _temporary_ (1.05, 1.05, 1.1, 1.1, 1.15, 1.2);
   file print;
   do year=2010 to 2015;
      r = rate[year];
      put 'The value of rate for ' year +1 'is ' r;
   end;
run;&lt;/PRE&gt;
&lt;P&gt;The (2010:2015) in the array definition mean that the lower bound to address members of the array is 2010 and the upper is 2015. _temporary_ means that the created variables will not be written to the output data set, if any. The comma delimited list of values following _temporary_ assigns values to the elements of the array. If you use an assignment list you have to provide a value for each element which could be . for missing.&lt;/P&gt;
&lt;P&gt;So any place we used 1.05&amp;nbsp; (or 0.05) the equivalent value could be selected using rate[year] , or other numeric variable with integer values of 2010 to 2015.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 22:45:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922321#M41385</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-03-29T22:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922324#M41386</link>
      <description>&lt;P&gt;This is great thank you! -- as a follow up, if I have the growth rates in a table in SAS, is there a way to store them as variable names and utilize those variable names in the list? I only ask because a few of the rates are carried out to 12 digits for precision. Thanks again.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 23:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922324#M41386</guid>
      <dc:creator>sas_user_1001</dc:creator>
      <dc:date>2024-03-29T23:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Iterative Loop</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922325#M41387</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/463968"&gt;@sas_user_1001&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;While I see you have already accepted a solution, here is an approach that gives the ability to have variable growth rates for future years. Yet another implementation that utilizes two dimensional array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input year month x_var;
	datalines;
2020 1 5
2020 2 10
2020 3 15
2020 4 25
;
run;

data want(KEEP= year month x_var gr_pct);
	if (0) then SET have;
	array x{20:24,1:4} 8 _temporary_; /* Change these dimensions to fit your needs */
	
	array gr{5} 3 gr1-gr5 (0, 0.05, 0.05, 0.03, 0.01); /* Annual growth rate */
	
	/* Load the 2020 (have) data values */
	do _n_=1 by 1 until(eof);
		set have end=eof;
		x{20,_n_} = x_var;
	end;
	
	/* ---------------------------- */
	/* Process future forecast data */
	/* ---------------------------- */
	gi = 1; /* growth rate index */
	
	do y=21 to hbound1(X);
		gi+1;
		do m=lbound2(X) to hbound2(X);
			x{y,m} = x{(y-1),m} * (1 + gr{gi});
			put x{y,m} =;
		end;
	end;
	
	/* ------------------------ */
	/* Generate desired output */
	/* ------------------------ */
	gi = 0;
	do y=lbound1(X) to hbound1(X);
		gi+1;
		do m=lbound2(X) to hbound2(X);
			year = 2000+y;
			month = m;
			x_var = x{y,m};
			gr_pct = gr{gi};
			output;
		end;
	end;
	stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 23:05:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Iterative-Loop/m-p/922325#M41387</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2024-03-29T23:05:05Z</dc:date>
    </item>
  </channel>
</rss>

