<?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: Use lag in a loop to generate autoregressive variable. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831430#M328544</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/217385"&gt;@Kilasuelika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What if I need previous some rows, e.g. &lt;EM&gt;x(i-2), x(i-3)&lt;/EM&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Stick the value into an array an use as needed.&lt;/P&gt;
&lt;PRE&gt;data test;
  x=0.5;
  array y(100);
  do i = 1 to 100;
    y[i]=x;
    output;
    x + 0.1;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;If you need to reference the value of x from 2 iterations previous that would be use y[i-2] (Caution: only when i is 3 or greater, this doesn't define a y[0] or y[-2] as valid indexes for the array.&lt;/P&gt;
&lt;P&gt;I use Y because you cannot have an array name the same as an existing variable.&lt;/P&gt;</description>
    <pubDate>Thu, 01 Sep 2022 16:12:36 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-09-01T16:12:36Z</dc:date>
    <item>
      <title>Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831419#M328537</link>
      <description>&lt;P&gt;Consider following code:&lt;/P&gt;&lt;PRE&gt;data test;
do i = 1 to 100;
	if i=1 then do
		x=0.5;
	end;
	else do;
		x=lag(x)+0.1;
	end;
	output;
end;
run;&lt;/PRE&gt;&lt;P&gt;I expect the result to be &lt;EM&gt;0.5 0.6 0.7 0.8&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;But the result is:&lt;EM&gt; 0.5 . 0.6 . 0.7 . 0.8&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;There are missing values. What happened here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;----------------------------------------------------------------------------------------------&lt;/P&gt;&lt;P&gt;Finally I thought maybe I have to use IML with some more lines of code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc iml;
x=J(100,1,0.5);
e=J(100,1);
call randseed(1);
call randgen(e, "normal");
do i=3 to 100;
	x[i]=0.3*x[i-1]+0.4*x[i-2]+e[i];
end;

create test from x [colnames=("x")];
append from x;
close test;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:29:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831419#M328537</guid>
      <dc:creator>Kilasuelika</dc:creator>
      <dc:date>2022-09-02T02:29:50Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831423#M328540</link>
      <description>&lt;P&gt;The LAG() function cannot return values you never passed into it.&amp;nbsp; Unless you want to do something very "creative" you should never execute LAG() or DIF() conditionally.&lt;/P&gt;
&lt;P&gt;But in this case there is no need to "lag" the value of X.&amp;nbsp; Since you are running a loop in a single iteration of the data step the "old" value of X is still there.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
do i = 1 to 100;
   if i=1 then do
    x=0.5;
  end;
  else do;
    x=x+0.1;
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or more simply.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  x=0.5;
  do i = 1 to 100;
    output;
    x + 0.1;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Sep 2022 15:37:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831423#M328540</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-01T15:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831427#M328542</link>
      <description>&lt;P&gt;What if I need previous some rows, e.g. &lt;EM&gt;x(i-2), x(i-3)&lt;/EM&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 15:48:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831427#M328542</guid>
      <dc:creator>Kilasuelika</dc:creator>
      <dc:date>2022-09-01T15:48:54Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831430#M328544</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/217385"&gt;@Kilasuelika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What if I need previous some rows, e.g. &lt;EM&gt;x(i-2), x(i-3)&lt;/EM&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Stick the value into an array an use as needed.&lt;/P&gt;
&lt;PRE&gt;data test;
  x=0.5;
  array y(100);
  do i = 1 to 100;
    y[i]=x;
    output;
    x + 0.1;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;If you need to reference the value of x from 2 iterations previous that would be use y[i-2] (Caution: only when i is 3 or greater, this doesn't define a y[0] or y[-2] as valid indexes for the array.&lt;/P&gt;
&lt;P&gt;I use Y because you cannot have an array name the same as an existing variable.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 16:12:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831430#M328544</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-01T16:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831433#M328546</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/217385"&gt;@Kilasuelika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What if I need previous some rows, e.g. &lt;EM&gt;x(i-2), x(i-3)&lt;/EM&gt;?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then show an example that actually has an input dataset that has multiple observations so that there will actually be multiple "rows".&lt;/P&gt;
&lt;P&gt;Here is method to create three lagged copies of X and make sure that values from a previous group do not bleed into the current group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  lagx1 = lag1(x);
  lagx2 = lag2(x);
  lagx3 = lag3(x);
  array lagx [3];
  if first.id then row=1;
  else row+1;
  do index= row+1 to dim(lagx);
    lagx[index]=.;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Sep 2022 16:20:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831433#M328546</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-01T16:20:30Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831495#M328588</link>
      <description>&lt;PRE&gt;array y[100];&lt;/PRE&gt;&lt;P&gt;will generate 100 variables y1, y2, y3... which is not i want. I only need a single variable x.&lt;/P&gt;&lt;P&gt;Do I have to use IML to achieve my goal?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:20:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831495#M328588</guid>
      <dc:creator>Kilasuelika</dc:creator>
      <dc:date>2022-09-02T02:20:54Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831497#M328590</link>
      <description>&lt;P&gt;I didn't mean creating lagged variables from existing data. Consider an AR(1) model:&lt;/P&gt;&lt;P&gt;x[1]=0.5, x[i]=x[i-1]+x[i-2]+rand()&lt;/P&gt;&lt;P&gt;At each loop, generate a random value and add it to the previous values. There is only a single variable.&lt;/P&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831497#M328590</guid>
      <dc:creator>Kilasuelika</dc:creator>
      <dc:date>2022-09-02T02:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Use lag in a loop to generate autoregressive variable.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831499#M328592</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/217385"&gt;@Kilasuelika&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I didn't mean creating lagged variables from existing data. Consider an AR(1) model:&lt;/P&gt;
&lt;P&gt;x[1]=0.5, x[i]=x[i-1]+x[i-2]+rand()&lt;/P&gt;
&lt;P&gt;At each loop, generate a random value and add it to the previous values. There is only a single variable.&lt;/P&gt;
&lt;DIV&gt;
&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You will have to provide a more complete example to explain what is is you are trying to do.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you are showing sounds like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  x=0.5 ;
  do i=1 to 10;
    output;
    lag1=x;
    lag2=lag(x);
    rand=rand('uniform');
    x=sum(of lag1 lag2 rand);
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs       x        i      lag1       lag2       rand

  1     0.5000     1      .          .         .
  2     0.5964     2     0.5000      .        0.09637
  3     2.0343     3     0.5964     0.5000    0.93795
  4     3.2650     4     2.0343     0.5964    0.63433
  5     5.8931     5     3.2650     2.0343    0.59379
  6     9.8466     6     5.8931     3.2650    0.68843
  7    16.4166     7     9.8466     5.8931    0.67684
  8    27.1344     8    16.4166     9.8466    0.87119
  9    44.5086     9    27.1344    16.4166    0.95770
 10    72.3635    10    44.5086    27.1344    0.72051
&lt;/PRE&gt;
&lt;P&gt;Which you can do without the LAG() function by just reversing the order of the assignment statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;    lag2=lag1;
    lag1=x;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Sep 2022 02:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-lag-in-a-loop-to-generate-autoregressive-variable/m-p/831499#M328592</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-02T02:39:38Z</dc:date>
    </item>
  </channel>
</rss>

