<?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: Lag by variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606016#M175923</link>
    <description>&lt;P&gt;I'm guessing that the problem is that your code does not handle by groups well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so. There are several ways to handle this. In the data step, you could do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=i);
   do ticker='a', 'b', 'c';
      do i=1 to 10;
         price=rand('integer', 1, 100);
         output;
      end;
   end;
run;

data want(drop=t5);
   set have;
   p5=lag5(price);
   t5=lag5(ticker);
   if t5 ne ticker then call missing(p5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ticker price p5 
a      1     . 
a      76    . 
a      6     . 
a      99    . 
a      77    . 
a      59    1 
a      73    76 
a      10    6 
a      91    99 
a      1     77 
b      89    . 
b      4     . 
b      21    . 
b      88    . 
b      26    . 
b      38    89 
b      58    4 
b      95    21 
b      72    88 
b      15    26 
c      46    . 
c      59    . 
c      78    . 
c      84    . 
c      26    . 
c      98    46 
c      16    59 
c      61    78 
c      54    84 
c      38    26 &lt;/PRE&gt;</description>
    <pubDate>Thu, 21 Nov 2019 06:04:15 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-11-21T06:04:15Z</dc:date>
    <item>
      <title>Lag by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606014#M175921</link>
      <description>&lt;P&gt;Hi there&lt;/P&gt;&lt;P&gt;I have a variable "Price" and I would like to creat a new variable "P1” which is lag 5 of "price" by variable "ticker" . I used the following code&lt;/P&gt;&lt;P&gt;data full;&lt;BR /&gt;set master;&lt;BR /&gt;By ticker;&lt;BR /&gt;P1=lag5(PRICE);&lt;BR /&gt;IF NOT FIRST.Ticker THEN P1= .;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;However, the code not doing well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Plase let me know how to improve.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 05:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606014#M175921</guid>
      <dc:creator>Xinhui</dc:creator>
      <dc:date>2019-11-21T05:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606016#M175923</link>
      <description>&lt;P&gt;I'm guessing that the problem is that your code does not handle by groups well?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so. There are several ways to handle this. In the data step, you could do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have(drop=i);
   do ticker='a', 'b', 'c';
      do i=1 to 10;
         price=rand('integer', 1, 100);
         output;
      end;
   end;
run;

data want(drop=t5);
   set have;
   p5=lag5(price);
   t5=lag5(ticker);
   if t5 ne ticker then call missing(p5);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ticker price p5 
a      1     . 
a      76    . 
a      6     . 
a      99    . 
a      77    . 
a      59    1 
a      73    76 
a      10    6 
a      91    99 
a      1     77 
b      89    . 
b      4     . 
b      21    . 
b      88    . 
b      26    . 
b      38    89 
b      58    4 
b      95    21 
b      72    88 
b      15    26 
c      46    . 
c      59    . 
c      78    . 
c      84    . 
c      26    . 
c      98    46 
c      16    59 
c      61    78 
c      54    84 
c      38    26 &lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Nov 2019 06:04:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606016#M175923</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-21T06:04:15Z</dc:date>
    </item>
    <item>
      <title>Re: Lag by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606021#M175926</link>
      <description>&lt;P&gt;Do you have SAS/ETS? If so, try PROC EXPAND.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2019 06:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-by-variable/m-p/606021#M175926</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-11-21T06:15:55Z</dc:date>
    </item>
  </channel>
</rss>

