<?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 on the newly calculated variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836885#M330904</link>
    <description>&lt;P&gt;No LAG needed, just a retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if first.id
then final = marks1;
else final + marks2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 05 Oct 2022 08:05:23 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-10-05T08:05:23Z</dc:date>
    <item>
      <title>LAG Function on the newly calculated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836883#M330903</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Following is the data set:&lt;/P&gt;
&lt;P&gt;id marks1 marks2&lt;BR /&gt;1 10 20&lt;BR /&gt;1 30 30&lt;BR /&gt;1 20 40&lt;BR /&gt;1 20 40&lt;BR /&gt;2 20 10&lt;BR /&gt;2 100 90&lt;BR /&gt;2 30 20&lt;BR /&gt;2 20 40&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Expected result : I would like to know how we could calculate the final variable using only &lt;STRONG&gt;LAG &lt;/STRONG&gt;function?&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 240pt;" border="0" width="320" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="64" height="19" style="height: 14.5pt; width: 48pt;"&gt;id&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;marks1&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;marks2&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="width: 48pt;"&gt;final&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right" class="xl66"&gt;10&lt;/TD&gt;
&lt;TD&gt;if first.id then MARKS1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;30&lt;/TD&gt;
&lt;TD align="right"&gt;30&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;TD&gt;if not first.id then lag(final)+marks2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;TD align="right"&gt;80&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;1&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;TD align="right"&gt;120&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;TD align="right" class="xl66"&gt;20&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;100&lt;/TD&gt;
&lt;TD align="right"&gt;90&lt;/TD&gt;
&lt;TD align="right"&gt;110&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;30&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right"&gt;130&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" align="right" style="height: 14.5pt;"&gt;2&lt;/TD&gt;
&lt;TD align="right"&gt;20&lt;/TD&gt;
&lt;TD align="right"&gt;40&lt;/TD&gt;
&lt;TD align="right"&gt;170&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks and Regards,&lt;/P&gt;
&lt;P&gt;Mushy&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 07:59:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836883#M330903</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2022-10-05T07:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function on the newly calculated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836885#M330904</link>
      <description>&lt;P&gt;No LAG needed, just a retained variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if first.id
then final = marks1;
else final + marks2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 05 Oct 2022 08:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836885#M330904</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-05T08:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function on the newly calculated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836907#M330913</link>
      <description>&lt;P&gt;Hello Kurt,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the response. Indeed I know this solution. But I want to know how a LAG could be applied on the calculated value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance ,&lt;/P&gt;
&lt;P&gt;Mushy&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 10:28:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836907#M330913</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2022-10-05T10:28:01Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function on the newly calculated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836908#M330914</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393424"&gt;@Mushy&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/393424"&gt;@Mushy&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I would like to know how we could calculate the final variable using only &lt;STRONG&gt;LAG &lt;/STRONG&gt;function?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Here's how:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=lf);
set have;
by id;
if first.id then final=marks1;
else do;
  link op_q; /* retrieve previous FINAL from the queue */
  final=lf+marks2;
end;
op_q: lf=lag(final); /* operate the queue */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you can see, a computed value can be saved in a LAG queue for the next iteration of the DATA step, but it's easier to use&amp;nbsp;a retained variable for this purpose (as&amp;nbsp;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank" rel="noopener"&gt;Kurt_Bremser&lt;/A&gt;&amp;nbsp;has suggested).&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 10:28:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836908#M330914</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-10-05T10:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: LAG Function on the newly calculated variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836909#M330915</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; Thanks for the solution &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Oct 2022 10:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/LAG-Function-on-the-newly-calculated-variable/m-p/836909#M330915</guid>
      <dc:creator>Mushy</dc:creator>
      <dc:date>2022-10-05T10:44:29Z</dc:date>
    </item>
  </channel>
</rss>

