<?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 Logic error of cumulative statement and lag function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879692#M347535</link>
    <description>&lt;P&gt;This task is going to derive a time-series variable, The rule is:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;X(t) = 1.5 * X(t-1) - 0.5 * X(t-2) + e;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;X(1) = e;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;X(2) = 1.5 * X(1) + e;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;where e follows a standard normal distribution. Here is how I generate this sequence:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  call streaminit(42);
  do i=1 to 200;
    e=rand("normal",0,1);
    output;
  end;
run;

data want;
  set have;

  *Method 1;
  x+sum(0.5*x,-0.5*lag(x),e);

  *Method 2;
  y+0.5*y-0.5*lag(y)+e;

  *Method 3;
  retain z;
  if _n_=1 then z=e;
  if _n_=2 then z=1.5*z+e;

  lag2_z=lag2(z);
  if _n_&amp;gt;2 then z=1.5*z-0.5*lag2_z+e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this code, I just find only result of method 1 is right. Why Method 2 and Method 3 are wrong? How to fix them?&lt;/P&gt;
&lt;P&gt;Thank you for any hint.&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jun 2023 04:33:04 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2023-06-08T04:33:04Z</dc:date>
    <item>
      <title>Logic error of cumulative statement and lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879692#M347535</link>
      <description>&lt;P&gt;This task is going to derive a time-series variable, The rule is:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;X(t) = 1.5 * X(t-1) - 0.5 * X(t-2) + e;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;X(1) = e;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;X(2) = 1.5 * X(1) + e;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;where e follows a standard normal distribution. Here is how I generate this sequence:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  call streaminit(42);
  do i=1 to 200;
    e=rand("normal",0,1);
    output;
  end;
run;

data want;
  set have;

  *Method 1;
  x+sum(0.5*x,-0.5*lag(x),e);

  *Method 2;
  y+0.5*y-0.5*lag(y)+e;

  *Method 3;
  retain z;
  if _n_=1 then z=e;
  if _n_=2 then z=1.5*z+e;

  lag2_z=lag2(z);
  if _n_&amp;gt;2 then z=1.5*z-0.5*lag2_z+e;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run this code, I just find only result of method 1 is right. Why Method 2 and Method 3 are wrong? How to fix them?&lt;/P&gt;
&lt;P&gt;Thank you for any hint.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2023 04:33:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879692#M347535</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-06-08T04:33:04Z</dc:date>
    </item>
    <item>
      <title>Re: Logic error of cumulative statement and lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879693#M347536</link>
      <description>&lt;P&gt;With method 2, the problem is simply that the expression&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y+0.5*y-0.5*lag(y)+e;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is to be understood as&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;y+(0.5*y-0.5*lag(y)+e);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which in the first iteration will set Y to 0 (the SUM statement, "Y+..." will initialize Y to 0, retain Y, and add whatever is after the plus sign, like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain y 0;
y=sum(y,0.5*y-0.5*lag(y)+e);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the second parameter evaluates to a missing value, meaning that Y stays 0 in the first iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The method 1 calculation, on the other hand, is equivalent to&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain x 0;
x=sum(x,0.5*x,-0.5*lag(x),e);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which will set X to E on the first iteration.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In method 3, the problem is that you calculate Z&amp;nbsp;&lt;STRONG&gt;before&lt;/STRONG&gt; taking the LAG2 function the first two times, but&amp;nbsp;&lt;STRONG&gt;after&lt;/STRONG&gt; the LAG2 call the remaining times. Method 3 can be rewritten as e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  retain z;

  lag2_z=lag(z);

  if _n_=1 then z=e;
  else if _n_=2 then z=1.5*z+e;
  else z=1.5*z-0.5*lag2_z+e;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that I changed from LAG2 to LAG, as the LAG call now always comes first.&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jun 2023 06:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879693#M347536</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-06-08T06:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Logic error of cumulative statement and lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879850#M347582</link>
      <description>Thank you very much, very detailed.</description>
      <pubDate>Fri, 09 Jun 2023 06:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Logic-error-of-cumulative-statement-and-lag-function/m-p/879850#M347582</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-06-09T06:03:41Z</dc:date>
    </item>
  </channel>
</rss>

