<?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 Function in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516709#M3219</link>
    <description>&lt;P&gt;My dataset looks like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;pagenum&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;52&lt;/P&gt;&lt;P&gt;52&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;pagenum&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;sort&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; pagenum;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;data&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt; itemqc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; itemqc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; pagenum;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; lag(pagenum)=sum(pagenum,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;;pagenum=lag(pagenum);end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is not working. Can you please help me either using lag function or retain.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks very much&lt;/P&gt;</description>
    <pubDate>Wed, 28 Nov 2018 14:59:11 GMT</pubDate>
    <dc:creator>SK4</dc:creator>
    <dc:date>2018-11-28T14:59:11Z</dc:date>
    <item>
      <title>Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516709#M3219</link>
      <description>&lt;P&gt;My dataset looks like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;pagenum&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;52&lt;/P&gt;&lt;P&gt;52&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;158&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;pagenum&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;51&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;157&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code:&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;sort&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; pagenum;&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;run&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;data&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt; itemqc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; itemqc;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; pagenum;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="2"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; lag(pagenum)=sum(pagenum,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;,&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="2"&gt;0&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="2"&gt;) &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="2"&gt;then&lt;/FONT&gt; &lt;FONT color="#0000ff" face="Courier New" size="2"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;;pagenum=lag(pagenum);end;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is not working. Can you please help me either using lag function or retain.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks very much&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 14:59:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516709#M3219</guid>
      <dc:creator>SK4</dc:creator>
      <dc:date>2018-11-28T14:59:11Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516721#M3223</link>
      <description>&lt;P&gt;You should not use conditionals with a lag() function.&amp;nbsp; Clearly states it in the manual.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;proc sort data=itemqc;
  by pagenum;
run;

data itemqc;
  set itemqc (rename=(pagenum=oldpge));
  retain pagenum;
  by oldpge;
  if _n_=1 then pagenum=oldpge;
  else if first.oldpge and oldpge ne pagenum+1 then pagenum=oldpge;
run;

&lt;/PRE&gt;
&lt;P&gt;Note not tested - post test data in the form of a datastep in future, I am not here to type in test data!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 15:15:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516721#M3223</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-28T15:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516732#M3225</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input pagenum;
cards;
51
51
51
51
52
52
157
157
158
158
158
158
;


data want;
set have;
by pagenum;
retain k k1;
if first.pagenum then do;
k=dif(pagenum);
k1=ifn(k=1,lag(pagenum),k1);
end;
if k=1 then pagenum=k1;
drop k:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Nov 2018 15:39:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516732#M3225</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-28T15:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516733#M3226</link>
      <description>&lt;P&gt;thank you very much for your quick reply. It works perfectly.&amp;nbsp; I had a feeling I was on a wrong track. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for the solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 15:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516733#M3226</guid>
      <dc:creator>SK4</dc:creator>
      <dc:date>2018-11-28T15:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516742#M3229</link>
      <description>&lt;P&gt;Seems like you could do something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if pagenum=sum(lag(pagenum),1) then pagenum=pagenum-1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Well, that would break for first record if it has pagenum=1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It also doesn't handle 'chaining', i.e. it's not clear what you would want to do with data like:&lt;/P&gt;
&lt;PRE&gt;51
51
52
53
117&lt;/PRE&gt;
&lt;P&gt;It would recode 53 to 52, not 51.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Conditional lagging works, but the key point to know is that LAG() does not return a value from the previous record.&amp;nbsp; It returns a value from a queue.&amp;nbsp; So using conditional lagging can be tricky.&amp;nbsp; It's a common problem. One way to avoid this complexity is to use the LAG() function unconditionally, to create a temporary variable, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lagPagenum=lag(Pagenum) ;

if lagPagenum=sum(pagenum,-1,0) then do;
  pagenum=lagPagenum;
end;

drop lagPagenum;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;By the way, it the 0 you have as an argument in the sum function shouldn't be doing anything.&amp;nbsp; Sometimes people add a 0 argument to the sum function like y=sum(x,0); so that it will return 0 if X is missing.&amp;nbsp; But in this case you already have an -1 argument, so adding the 0 argument is unnecessary.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Nov 2018 15:41:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/516742#M3229</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-11-28T15:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/518036#M3479</link>
      <description>&lt;P&gt;Thank you everyone for the response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This solution by RW9 worked perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=itemqc;&lt;BR /&gt;&amp;nbsp; by pagenum;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data itemqc;&lt;BR /&gt;&amp;nbsp; set itemqc (rename=(pagenum=oldpge));&lt;BR /&gt;&amp;nbsp; retain pagenum;&lt;BR /&gt;&amp;nbsp; by oldpge;&lt;BR /&gt;&amp;nbsp; if _n_=1 then pagenum=oldpge;&lt;BR /&gt;&amp;nbsp; else if first.oldpge and oldpge ne pagenum+1 then pagenum=oldpge;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 12:51:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/518036#M3479</guid>
      <dc:creator>SK4</dc:creator>
      <dc:date>2018-12-03T12:51:55Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/518041#M3481</link>
      <description>&lt;P&gt;Not too blow my own trumpet, but if my solution was correct, please mark my post as correct by clicking the button.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Dec 2018 13:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Lag-Function/m-p/518041#M3481</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-03T13:03:09Z</dc:date>
    </item>
  </channel>
</rss>

