<?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 Lag by variable amount in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594392#M15661</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate lag value that is not constant from time to time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example for first entry I want lag6() for second entry I want lag9() for third entry lag999().&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to do it? I tried to use %let function, but macro can be only constant so it was not working.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 07 Oct 2019 01:30:32 GMT</pubDate>
    <dc:creator>eduard1231</dc:creator>
    <dc:date>2019-10-07T01:30:32Z</dc:date>
    <item>
      <title>Lag by variable amount</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594392#M15661</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate lag value that is not constant from time to time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example for first entry I want lag6() for second entry I want lag9() for third entry lag999().&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to do it? I tried to use %let function, but macro can be only constant so it was not working.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 01:30:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594392#M15661</guid>
      <dc:creator>eduard1231</dc:creator>
      <dc:date>2019-10-07T01:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable amount</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594397#M15664</link>
      <description>&lt;P&gt;Since in general you will need to execute LAGn() function call on every observation just do that and store the results somewhere that you can conditionally retrieve them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have ;
  x_lag6 = lag6(x);
  x_lag9 = lag9(x);
  x_lag999 = lag999(x);
  if entry=1 then new_var = x_lag6 ;
  else if entry=2 then new_var=x_lag9;
  else new_var=x_lag999;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Provide a more complete description of what you are actually trying to do to see if there might be a better way to do what you want. Most likely one that does not use any of the LAGn() functions.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 01:46:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594397#M15664</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-07T01:46:20Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable amount</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594416#M15667</link>
      <description>&lt;P&gt;Your problem is difficult to understand, please post example have and want datasets.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 06:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594416#M15667</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-07T06:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable amount</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594419#M15668</link>
      <description>&lt;P&gt;How many lags would you need at most?&lt;/P&gt;
&lt;P&gt;Instead of the many function calls, I'd use either an array or a hash to create a sufficiently long FIFO chain.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 06:45:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594419#M15668</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-07T06:45:29Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable amount</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594425#M15669</link>
      <description>&lt;P&gt;Array code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array mylags{1000} _temporary_;

/* your processing here */

/* at the end of the step */
do mylagindex = dim(mylags) to 2 by -1;
  mylags{mylagindex} = mylags{mylagindex-1};
end;
mylags{1} = x;
drop mylagindex;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In your code, mylags{1} is equivalent to lag(x), and mylags{999} to lag999(x). The only thing you need to adapt in that code is the size of the array and which variable's value you assign to mylags{1}.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2019 07:45:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-by-variable-amount/m-p/594425#M15669</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-10-07T07:45:34Z</dc:date>
    </item>
  </channel>
</rss>

