<?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 partition by in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356656#M83639</link>
    <description>&lt;P&gt;Sounds like you are looking for something like:&lt;/P&gt;
&lt;PRE&gt;data have;
  input Facility $ Appointment_date mmddyy10. Patient_id;
  format Appointment_date mmddyy10.;
  cards;
a  5/4/2013  123
a  5/5/2013  123
a  5/5/2013  232
a  5/15/2013  232
;

data want;
  set have;
  format lag mmddyy10.;
  by facility Patient_id;
  lag=ifn(first.Patient_id,.,lag(Appointment_date));
  dif=ifn(first.Patient_id,.,dif(Appointment_date));
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
    <pubDate>Sat, 06 May 2017 22:26:25 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-05-06T22:26:25Z</dc:date>
    <item>
      <title>Lag function with partition by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356655#M83638</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following dataset structure&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Facility &amp;nbsp;Appointment_date &amp;nbsp;Patient_id &amp;nbsp;&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/4/2013 &amp;nbsp;123&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/5/2013 &amp;nbsp;123&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/5/2013 &amp;nbsp;232&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/15/2013 &amp;nbsp;123&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find out the difference in successive appointment_dates for patients who had multiple visits. The structure of the output should look as follows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Facility &amp;nbsp;Appointment_date &amp;nbsp;Patient_id &amp;nbsp;Lag Diff&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/4/2013 &amp;nbsp;123 &amp;nbsp;. &amp;nbsp;.&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/5/2013 &amp;nbsp;123 &amp;nbsp;&lt;SPAN&gt;5/4/2013 &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/5/2013 &amp;nbsp;232 . &amp;nbsp;.&lt;/P&gt;&lt;P&gt;a &amp;nbsp;5/15/2013 &amp;nbsp;232 &lt;SPAN&gt;5/5/2013 &amp;nbsp;10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that I can get the result by using lag function in SQL with the following query&lt;/P&gt;&lt;P&gt;select *, lag(appointment_date,1) over (partition by patient_id order by appointment_date) as lag from table1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kindly let me know if there is a way I can do the same using SAS code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2017 22:09:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356655#M83638</guid>
      <dc:creator>roh910</dc:creator>
      <dc:date>2017-05-06T22:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with partition by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356656#M83639</link>
      <description>&lt;P&gt;Sounds like you are looking for something like:&lt;/P&gt;
&lt;PRE&gt;data have;
  input Facility $ Appointment_date mmddyy10. Patient_id;
  format Appointment_date mmddyy10.;
  cards;
a  5/4/2013  123
a  5/5/2013  123
a  5/5/2013  232
a  5/15/2013  232
;

data want;
  set have;
  format lag mmddyy10.;
  by facility Patient_id;
  lag=ifn(first.Patient_id,.,lag(Appointment_date));
  dif=ifn(first.Patient_id,.,dif(Appointment_date));
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2017 22:26:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356656#M83639</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-06T22:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with partition by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356662#M83643</link>
      <description>&lt;P&gt;Thanks. It's working!!&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2017 23:46:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356662#M83643</guid>
      <dc:creator>roh910</dc:creator>
      <dc:date>2017-05-06T23:46:45Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with partition by</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356664#M83645</link>
      <description>&lt;P&gt;Then mark the question as 'solved'&lt;/P&gt;</description>
      <pubDate>Sat, 06 May 2017 23:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-partition-by/m-p/356664#M83645</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-05-06T23:49:37Z</dc:date>
    </item>
  </channel>
</rss>

