<?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 function with multiple by group variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746168#M234040</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    length a $1 b$1;
    input a b c;
cards;
a d 1
a d 2
a e 3
b e 4
b e 5
c e 6
c f 7
c f 8
;
proc sort; by  a b c; 
run;

data want;
    set have;
    by a b c;
    c_=lag(c);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 07 Jun 2021 08:38:50 GMT</pubDate>
    <dc:creator>blueskyxyz</dc:creator>
    <dc:date>2021-06-07T08:38:50Z</dc:date>
    <item>
      <title>Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746150#M234036</link>
      <description>&lt;P&gt;I want the following output by using the &lt;STRONG&gt;lag&lt;/STRONG&gt; function using multiple by variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp;B &amp;nbsp;C &amp;nbsp;Lag(C)&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; e&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; e&amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the output I am getting is this-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp;B &amp;nbsp;C &amp;nbsp;Lag(C)&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; e&amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; e&amp;nbsp; 6 &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;7 &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 05:49:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746150#M234036</guid>
      <dc:creator>Saurabh_Rana</dc:creator>
      <dc:date>2021-06-07T05:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746152#M234037</link>
      <description>&lt;P&gt;Please post the code you have, so that we can suggest improvements.&lt;/P&gt;
&lt;P&gt;Something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.b then Lag_C = .;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should help.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 06:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746152#M234037</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-07T06:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746168#M234040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    length a $1 b$1;
    input a b c;
cards;
a d 1
a d 2
a e 3
b e 4
b e 5
c e 6
c f 7
c f 8
;
proc sort; by  a b c; 
run;

data want;
    set have;
    by a b c;
    c_=lag(c);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Jun 2021 08:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746168#M234040</guid>
      <dc:creator>blueskyxyz</dc:creator>
      <dc:date>2021-06-07T08:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746180#M234046</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/298115"&gt;@blueskyxyz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    length a $1 b$1;
    input a b c;
cards;
a d 1
a d 2
a e 3
b e 4
b e 5
c e 6
c f 7
c f 8
;
proc sort; by  a b c; 
run;

data want;
    set have;
    by a b c;
    c_=lag(c);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This will &lt;STRONG&gt;not&lt;/STRONG&gt; create the required dataset, but the one &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt; is getting now. Also: why is variable C in the the by-statement in proc sort? This seems to be wrong.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 10:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746180#M234046</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-06-07T10:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746215#M234068</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is doing the way it is written.&lt;BR /&gt;You want the Lag(C) values for selected rows,&lt;/P&gt;
&lt;P&gt;You need to define a criteria and add a filter for it in your code.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 12:05:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746215#M234068</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-07T12:05:59Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746217#M234070</link>
      <description>&lt;P&gt;Why are those other values supposed to be missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it because you want to treat A and B as grouping variables?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by a b ;
  lag_c=lag(c);
  if first.b then lag_c=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Jun 2021 12:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746217#M234070</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-07T12:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with multiple by group variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746262#M234083</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/316224"&gt;@Saurabh_Rana&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want the following output by using the &lt;STRONG&gt;lag&lt;/STRONG&gt; function using multiple by variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp;B &amp;nbsp;C &amp;nbsp;Lag(C)&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; e&amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; e&amp;nbsp; 6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the output I am getting is this-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A &amp;nbsp;B &amp;nbsp;C &amp;nbsp;Lag(C)&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/P&gt;
&lt;P&gt;a &amp;nbsp;d &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;a&amp;nbsp; e&amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;
&lt;P&gt;b&amp;nbsp; e&amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; e&amp;nbsp; 6 &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;7 &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/P&gt;
&lt;P&gt;c&amp;nbsp; f&amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So, what is the INPUT?&lt;/P&gt;
&lt;P&gt;Which are the BY variables?&lt;/P&gt;
&lt;P&gt;What are the assignment rules?&lt;/P&gt;</description>
      <pubDate>Mon, 07 Jun 2021 14:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-multiple-by-group-variables/m-p/746262#M234083</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-07T14:42:08Z</dc:date>
    </item>
  </channel>
</rss>

