<?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 creating LEAD and LAG together. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577196#M163540</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=mr;
by gvkey fyear;
run;
data mr1;
set mr;
at_1=ifn(gvkey=lag(gvkey) and fyear=lag(fyear)+1,lag(at),.);
run;
&lt;BR /&gt;proc sort data=mr1;
by descending gvkey fyear;
run;

data mr2;
set mr1;
by gvkey fyear;
at1 = lag(at);
at2 = lag(at1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is a very humble code I used for getting LAG and LEAD. Because there is no such lead coding in SAS, I came up with using by descending orders. What I want to obtain is like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let 2015 is the referenced year. If I lag I may have a value of 2014(at_1, meaning total assets of 2015 minus 1, 2014).&lt;/P&gt;&lt;P&gt;by using descending order, I tried to obtain leaded values like at1(total assets of 2015+1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To me, it looks logically fine but the error code said:&amp;nbsp;&lt;STRONG&gt;ERROR: BY variables are not properly sorted on data set WORK.MR1.&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Pretty unsure why it is considered not properly sorted and more wonder how can I obtain lead and lag together..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I considered lag statement is quite easy to use and clueless now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please share your brilliant wisdom once again.&lt;/P&gt;&lt;P&gt;Thank you so much!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 28 Jul 2019 00:23:43 GMT</pubDate>
    <dc:creator>JKCho</dc:creator>
    <dc:date>2019-07-28T00:23:43Z</dc:date>
    <item>
      <title>creating LEAD and LAG together.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577196#M163540</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=mr;
by gvkey fyear;
run;
data mr1;
set mr;
at_1=ifn(gvkey=lag(gvkey) and fyear=lag(fyear)+1,lag(at),.);
run;
&lt;BR /&gt;proc sort data=mr1;
by descending gvkey fyear;
run;

data mr2;
set mr1;
by gvkey fyear;
at1 = lag(at);
at2 = lag(at1);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is a very humble code I used for getting LAG and LEAD. Because there is no such lead coding in SAS, I came up with using by descending orders. What I want to obtain is like this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let 2015 is the referenced year. If I lag I may have a value of 2014(at_1, meaning total assets of 2015 minus 1, 2014).&lt;/P&gt;&lt;P&gt;by using descending order, I tried to obtain leaded values like at1(total assets of 2015+1).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To me, it looks logically fine but the error code said:&amp;nbsp;&lt;STRONG&gt;ERROR: BY variables are not properly sorted on data set WORK.MR1.&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Pretty unsure why it is considered not properly sorted and more wonder how can I obtain lead and lag together..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I considered lag statement is quite easy to use and clueless now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please share your brilliant wisdom once again.&lt;/P&gt;&lt;P&gt;Thank you so much!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 00:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577196#M163540</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2019-07-28T00:23:43Z</dc:date>
    </item>
    <item>
      <title>Re: creating LEAD and LAG together.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577197#M163541</link>
      <description>&lt;P&gt;You need to use DESCENDING in the BY statement in the data step. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270067"&gt;@JKCho&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=mr;
by gvkey fyear;
run;
data mr1;
set mr;
at_1=ifn(gvkey=lag(gvkey) and fyear=lag(fyear)+1,lag(at),.);
run;
&lt;BR /&gt;proc sort data=mr1;
by descending gvkey fyear;
run;

data mr2;
set mr1;
by gvkey fyear;
at1 = lag(at);
at2 = lag(at1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this is a very humble code I used for getting LAG and LEAD. Because there is no such lead coding in SAS, I came up with using by descending orders. What I want to obtain is like this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let 2015 is the referenced year. If I lag I may have a value of 2014(at_1, meaning total assets of 2015 minus 1, 2014).&lt;/P&gt;
&lt;P&gt;by using descending order, I tried to obtain leaded values like at1(total assets of 2015+1).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To me, it looks logically fine but the error code said:&amp;nbsp;&lt;STRONG&gt;ERROR: BY variables are not properly sorted on data set WORK.MR1.&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Pretty unsure why it is considered not properly sorted and more wonder how can I obtain lead and lag together..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I considered lag statement is quite easy to use and clueless now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please share your brilliant wisdom once again.&lt;/P&gt;
&lt;P&gt;Thank you so much!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 00:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577197#M163541</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2019-07-28T00:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: creating LEAD and LAG together.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577198#M163542</link>
      <description>oh... no wonder. Thx for pointing out that I haven't seen it myself. What a carelessness..</description>
      <pubDate>Sun, 28 Jul 2019 02:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577198#M163542</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2019-07-28T02:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: creating LEAD and LAG together.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577273#M163577</link>
      <description>&lt;P&gt;This method would be a lot simpler&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T2;                                                 
merge T1 T1(firstobs=2 keep=VAL rename=(VAL=NEXTVAL));     
run;   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sgf/2015/06/19/can-you-lag-and-lead-at-the-same-time-if-using-the-sas-data-step-yes-you-can/" target="_blank" rel="noopener"&gt;https://blogs.sas.com/content/sgf/2015/06/19/can-you-lag-and-lead-at-the-same-time-if-using-the-sas-data-step-yes-you-can/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 23:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-LEAD-and-LAG-together/m-p/577273#M163577</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-07-28T23:52:09Z</dc:date>
    </item>
  </channel>
</rss>

