<?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: Data step with Lag function in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743611#M232850</link>
    <description>Thanks a lot. Is there anyway where i can find my mistake n learn the problem</description>
    <pubDate>Tue, 25 May 2021 16:29:02 GMT</pubDate>
    <dc:creator>Aman4SAS</dc:creator>
    <dc:date>2021-05-25T16:29:02Z</dc:date>
    <item>
      <title>Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743589#M232840</link>
      <description>Hi All,&lt;BR /&gt;Data x;&lt;BR /&gt;Input A B$ ;&lt;BR /&gt;Datalines;&lt;BR /&gt;101 a&lt;BR /&gt;101 b&lt;BR /&gt;101 b&lt;BR /&gt;101 c&lt;BR /&gt;101 d&lt;BR /&gt;102 a&lt;BR /&gt;102 a&lt;BR /&gt;102 a&lt;BR /&gt;102 b&lt;BR /&gt;102 b&lt;BR /&gt;102 c&lt;BR /&gt;103 a&lt;BR /&gt;103 b&lt;BR /&gt;103 c&lt;BR /&gt;103 d&lt;BR /&gt;103 d&lt;BR /&gt;103 e&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;Required data like:&lt;BR /&gt;101 a 1&lt;BR /&gt;101 b 2&lt;BR /&gt;101 b 2&lt;BR /&gt;101 c 3&lt;BR /&gt;101 d 4&lt;BR /&gt;102 a 1&lt;BR /&gt;102 a 1&lt;BR /&gt;102 a 1&lt;BR /&gt;102 b 2&lt;BR /&gt;102 b 2&lt;BR /&gt;102 c 3&lt;BR /&gt;103 a 1&lt;BR /&gt;103 b 2&lt;BR /&gt;103 c 3&lt;BR /&gt;103 d 4&lt;BR /&gt;103 d 4&lt;BR /&gt;103 e 5&lt;BR /&gt;My code is:&lt;BR /&gt;Data new;&lt;BR /&gt;Set x;&lt;BR /&gt;If first.a then i=1;&lt;BR /&gt;Else if b ne lag(b) then i=i+1;&lt;BR /&gt;Retain i;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;I am facing problem in 102 2nd row, in 2nd row i is being 2 which should not as b = lag(b) &lt;BR /&gt;&lt;BR /&gt;Please help me&lt;BR /&gt;Thanks&lt;BR /&gt;</description>
      <pubDate>Tue, 25 May 2021 15:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743589#M232840</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2021-05-25T15:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743595#M232845</link>
      <description>&lt;P&gt;You shouldn't use LAG() for this at all, this is better done with BY group processing. &lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=x;
by a b;
run;

data new;
set x;
by A B;
retain i;
if first.a then i=0;
if first.b then i+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39317"&gt;@Aman4SAS&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi All,&lt;BR /&gt;Data x;&lt;BR /&gt;Input A B$ ;&lt;BR /&gt;Datalines;&lt;BR /&gt;101 a&lt;BR /&gt;101 b&lt;BR /&gt;101 b&lt;BR /&gt;101 c&lt;BR /&gt;101 d&lt;BR /&gt;102 a&lt;BR /&gt;102 a&lt;BR /&gt;102 a&lt;BR /&gt;102 b&lt;BR /&gt;102 b&lt;BR /&gt;102 c&lt;BR /&gt;103 a&lt;BR /&gt;103 b&lt;BR /&gt;103 c&lt;BR /&gt;103 d&lt;BR /&gt;103 d&lt;BR /&gt;103 e&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;Required data like:&lt;BR /&gt;101 a 1&lt;BR /&gt;101 b 2&lt;BR /&gt;101 b 2&lt;BR /&gt;101 c 3&lt;BR /&gt;101 d 4&lt;BR /&gt;102 a 1&lt;BR /&gt;102 a 1&lt;BR /&gt;102 a 1&lt;BR /&gt;102 b 2&lt;BR /&gt;102 b 2&lt;BR /&gt;102 c 3&lt;BR /&gt;103 a 1&lt;BR /&gt;103 b 2&lt;BR /&gt;103 c 3&lt;BR /&gt;103 d 4&lt;BR /&gt;103 d 4&lt;BR /&gt;103 e 5&lt;BR /&gt;My code is:&lt;BR /&gt;Data new;&lt;BR /&gt;Set x;&lt;BR /&gt;If first.a then i=1;&lt;BR /&gt;Else if b ne lag(b) then i=i+1;&lt;BR /&gt;Retain i;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;I am facing problem in 102 2nd row, in 2nd row i is being 2 which should not as b = lag(b) &lt;BR /&gt;&lt;BR /&gt;Please help me&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 16:01:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743595#M232845</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-25T16:01:45Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743597#M232846</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39317"&gt;@Aman4SAS&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you really want to use the LAG function, make sure that it is executed &lt;EM&gt;unconditionally&lt;/EM&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
set x;
by a;
if b ne lag(b) then i+1;
if first.a then i=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 May 2021 16:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743597#M232846</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-25T16:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743609#M232849</link>
      <description>Thanks a lot. If there anyway where i can find my mistake n learn</description>
      <pubDate>Tue, 25 May 2021 16:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743609#M232849</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2021-05-25T16:27:45Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743611#M232850</link>
      <description>Thanks a lot. Is there anyway where i can find my mistake n learn the problem</description>
      <pubDate>Tue, 25 May 2021 16:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743611#M232850</guid>
      <dc:creator>Aman4SAS</dc:creator>
      <dc:date>2021-05-25T16:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743612#M232851</link>
      <description>Googling "issue with lag" would likely have brought up many posts that illustrate this issue. It's a common misunderstanding with the lag function that once you understand how it works you're fine. Don't forget it now &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 25 May 2021 16:31:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743612#M232851</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-05-25T16:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Data step with Lag function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743649#M232868</link>
      <description>&lt;P&gt;See also the description and examples in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.htm" target="_blank" rel="noopener"&gt;documentation of the LAG function&lt;/A&gt;. Then be creative and write your own DATA steps using the LAG function (applied to small datasets like SASHELP.CLASS or datasets you create for this purpose), moving from very simple to more advanced little programs. Always examine the output dataset or printed output they may create (and of course the log) and check if the result matches your understanding. If it doesn't, adapt your understanding. Thus you will more and more develop the ability to predict the results of new examples (even tricky ones as in&amp;nbsp;&lt;A href="http://If statement Short circuiting and Lag function" target="_blank" rel="noopener"&gt;If statement Short circuiting and Lag function&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Tue, 25 May 2021 17:59:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-step-with-Lag-function/m-p/743649#M232868</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-05-25T17:59:51Z</dc:date>
    </item>
  </channel>
</rss>

