<?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 equivalent in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711058#M218970</link>
    <description>Thanks for your note and your help with the coding.</description>
    <pubDate>Wed, 13 Jan 2021 12:49:32 GMT</pubDate>
    <dc:creator>Abimal_Zippi</dc:creator>
    <dc:date>2021-01-13T12:49:32Z</dc:date>
    <item>
      <title>lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710717#M218839</link>
      <description>&lt;P&gt;Since lag function does not go with proc sql, is there an equivalent one for sum function. I appreciate if any one who can help to sum the doses between each unique&amp;nbsp; category dates (N.B. for each unique id, the sum of doses (of date_exposed var) between each unique catgeory_dates)?&lt;/P&gt;&lt;P&gt;Here is the data have and want -&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID category date_category date_exposed dose ;
format date_category date_exposed date9.;
cards;
1   0 10JUL2017 10Jan2016 15 
1   0 10JUL2017 21Jan2016 7.5
1   0 10JUL2017 22Jan2016  10
1   0 10JUL2017 27Jan2016  100
1   2 10JUL2018 5AUG2017  100
1   2 10JUL2018 15AUG2017  80
1   2 10JUL2018 25AUG2017  10
1   2 10JUL2018 25JUN2018  100
1   3 10JUL2019 15AUG2018  10
1   3 10JUL2019 15AUG2018  140
1   4 10JUL2020 15AUG2020  140
1   4 10JUL2019 15AUG2020  50
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   1 10JUL2018 10AUG2017  15
2   1 10JUL2018 20AUG2017  5
2   1 10JUL2018 20Jan2018  15
2   2 10JUL2019 20Jan2019  5
2   2 10JUL2019 20Jan2019  12
2   2 10JUL2019 20FEB2019  12
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ID category date_category sum ;
format date_category date9.;
cards;
1   0 10JUL2017  . 
1   2 10JUL2018  290
1   3 10JUL2019 150
1   4 10JUL2020  0
2   0 10JUL2017  -
2   1 10JUL2018  35
2   2 10JUL2019  29
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 01:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710717#M218839</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2021-01-12T01:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710723#M218841</link>
      <description>&lt;P&gt;The LAG function is based on the concept of FIFO queues - a concept PROC SQL will not support.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, there is the undocumented MONOTONIC function in PROC SQL (see&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/MONOTONIC-function-in-PROC-SQL/ta-p/475752" target="_self"&gt;MONOTONIC function in PROC-SQL&lt;/A&gt;), roughly analogous to _N_ in a data step.&amp;nbsp; So you might be able, using intermediate views or tables, to code something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  create view a as select date, snydjcm, monotonic() as a_sequence from sashelp.citiday;
  create view b as select date, snydjcm, monotonic() as b_sequence from sashelp.citiday;

  create table want as 
    select a.date, a.snydjcm as dj_current, b.date as prior_date, b.snydjcm as dj_prior
	from a left join b on a_sequence=b_sequence+1;
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But even then you are relying on PROC SQL reading in the data in the same order that a data step would read it in.&amp;nbsp; That is NOT guaranteed, so monotonic() should not be used for production code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or, if the data has some ordering variable, with no repeated values (say DATE), you could do a join of A and B, with a group by a.date, where b.date&amp;lt;a.date, and having b.date=max(b.date).&amp;nbsp; &amp;nbsp;Extremely expensive.&amp;nbsp; It's so anti-scalable a concept I won't write the code.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 03:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710723#M218841</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-01-12T03:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710737#M218847</link>
      <description>&lt;P&gt;I think that you need describe in considerable detail why this is the desired output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;1   4 10JUL2020  0&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for Id=1 and category = 4. You also show as input:&lt;/P&gt;
&lt;PRE&gt;1   4 10JUL2019 15AUG2020  50&lt;/PRE&gt;
&lt;P&gt;and there is no output for that date. So I suspect the 10Jul2019 was intended to be 10Jul2020 but that still raises the question of why the result for 10Jul2020 would be 0.&lt;/P&gt;
&lt;P&gt;Assuming the 10Jul2019 is a typo with an incorrect 0 for sum in the Want set I might suggest:&lt;/P&gt;
&lt;P&gt;Summarizing and then setting the first value to missing for the Id.&lt;/P&gt;
&lt;PRE&gt;data have;
input ID category date_category :date9. date_exposed :date9. dose ;
format date_category date_exposed date9.;
cards;
1   0 10JUL2017 10Jan2016 15 
1   0 10JUL2017 21Jan2016 7.5
1   0 10JUL2017 22Jan2016  10
1   0 10JUL2017 27Jan2016  100
1   2 10JUL2018 5AUG2017  100
1   2 10JUL2018 15AUG2017  80
1   2 10JUL2018 25AUG2017  10
1   2 10JUL2018 25JUN2018  100
1   3 10JUL2019 15AUG2018  10
1   3 10JUL2019 15AUG2018  140
1   4 10JUL2020 15AUG2020  140
1   4 10JUL2019 15AUG2020  50
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   1 10JUL2018 10AUG2017  15
2   1 10JUL2018 20AUG2017  5
2   1 10JUL2018 20Jan2018  15
2   2 10JUL2019 20Jan2019  5
2   2 10JUL2019 20Jan2019  12
2   2 10JUL2019 20FEB2019  12
;

proc summary data=have nway;
   class ID category date_category;
   var dose;
   output out=summary (drop=_type_ _freq_)
       sum=sum;
run;
data want; 
   set summary;
   by  id;
   if first.id then call missing(sum);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jan 2021 09:20:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710737#M218847</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-01-12T09:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710743#M218850</link>
      <description>&lt;P&gt;Testing the code that you post is NOT a crime, it's a basic courtesy with regards to those that are supposed to help you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When reading dates, do not forget to use date informats:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID category date_category :date9. date_exposed :date9. dose;
format date_category date_exposed date9.;
cards;
1   0 10JUL2017 10Jan2016 15 
1   0 10JUL2017 21Jan2016 7.5
1   0 10JUL2017 22Jan2016  10
1   0 10JUL2017 27Jan2016  100
1   2 10JUL2018 5AUG2017  100
1   2 10JUL2018 15AUG2017  80
1   2 10JUL2018 25AUG2017  10
1   2 10JUL2018 25JUN2018  100
1   3 10JUL2019 15AUG2018  10
1   3 10JUL2019 15AUG2018  140
1   4 10JUL2020 15AUG2020  140
1   4 10JUL2019 15AUG2020  50
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   0 10JUL2017 10Jan2016  15
2   1 10JUL2018 10AUG2017  15
2   1 10JUL2018 20AUG2017  5
2   1 10JUL2018 20Jan2018  15
2   2 10JUL2019 20Jan2019  5
2   2 10JUL2019 20Jan2019  12
2   2 10JUL2019 20FEB2019  12
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Based on this data, this data step creates your intended result:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id category;
retain last_date sum;
if first.id
then do;
  last_date = .;
  sum = .;
end;
if last_date ne . and last_date le date_exposed le date_category then sum + dose;
if last.category;
last_date = date_category;
output;
sum = 0;
keep id category date_category sum;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;ID	category	date_category	sum
1	0	10JUL2017	.
1	2	10JUL2018	290
1	3	10JUL2019	150
1	4	10JUL2019	0
2	0	10JUL2017	.
2	1	10JUL2018	35
2	2	10JUL2019	29
&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jan 2021 07:32:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/710743#M218850</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-01-14T07:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711057#M218969</link>
      <description>Thanks for the note, it should be 10Jul2020 .</description>
      <pubDate>Wed, 13 Jan 2021 12:46:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711057#M218969</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2021-01-13T12:46:51Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711058#M218970</link>
      <description>Thanks for your note and your help with the coding.</description>
      <pubDate>Wed, 13 Jan 2021 12:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711058#M218970</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2021-01-13T12:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: lag equivalent in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711061#M218971</link>
      <description>Thanks for your help with the coding!</description>
      <pubDate>Wed, 13 Jan 2021 12:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/lag-equivalent-in-proc-sql/m-p/711061#M218971</guid>
      <dc:creator>Abimal_Zippi</dc:creator>
      <dc:date>2021-01-13T12:52:58Z</dc:date>
    </item>
  </channel>
</rss>

