<?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: Calculations with lagged values in proc sql in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658922#M22673</link>
    <description>&lt;P&gt;The LAG function does not work in PROC SQL. Better you should do this in a DATA step.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Jun 2020 10:57:33 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-06-15T10:57:33Z</dc:date>
    <item>
      <title>Calculations with lagged values in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658919#M22672</link>
      <description>&lt;P&gt;Hi!&lt;BR /&gt;I am trying to make calculations with lagged values within proc sql (I know function lag is not working) as this is only subpart of a longer proc sql program.&lt;/P&gt;
&lt;P&gt;Is it feasible?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
input ID $ L M D ;
Datalines;
B 1 10 100 
B 1 20 100
C 1 30 200
C 2 40 200
D 1 50 300
D 2 60 300
D 3 70 300
;

proc sql;
Create table want as 
Select *, 
CASE 
WHEN L=1 THEN D-M
WHEN L=2 then  (SELECT D-LAG(M)  FROM T WHERE ID=A.ID AND L=a.L) 
else (SELECT M-Lag(D)  FROM T WHERE ID=A.ID AND L=a.L) 

END AS D1
FROM T AS A
;
QUIT

Output
B 1 10 100 90
B 1 20 100 80
C 1 30 200 170
C 2 40 200 170
D 1 50 300 250
D 2 60 300 250 
D 3 70 300 -230&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jun 2020 10:55:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658919#M22672</guid>
      <dc:creator>cmemtsa</dc:creator>
      <dc:date>2020-06-15T10:55:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations with lagged values in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658922#M22673</link>
      <description>&lt;P&gt;The LAG function does not work in PROC SQL. Better you should do this in a DATA step.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 10:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658922#M22673</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-06-15T10:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations with lagged values in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658923#M22674</link>
      <description>&lt;P&gt;Please try below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
input ID $ L M D ;
Datalines;
B 1 10 100 
B 1 20 100
C 1 30 200
C 2 40 200
D 1 50 300
D 2 60 300
D 3 70 300
;

data want;
set t;
by id;
la=lag(m);
if first.id then la=.;
if l=1 then want=d-m;
else if l=2 then want=d-la;
else  want=m-d;

run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jun 2020 11:07:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658923#M22674</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-06-15T11:07:29Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations with lagged values in proc sql</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658936#M22676</link>
      <description>&lt;P&gt;Thanks. It works with Data but I wanted to know whether is feasible with Proc Sql.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 11:53:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculations-with-lagged-values-in-proc-sql/m-p/658936#M22676</guid>
      <dc:creator>cmemtsa</dc:creator>
      <dc:date>2020-06-15T11:53:57Z</dc:date>
    </item>
  </channel>
</rss>

