<?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: Want to get previous date value to next record which is less than current record date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870367#M343776</link>
    <description>&lt;P&gt;Do you have more than one ID value in the data? If so, when the ID changes what should happen with date3? Your current description would mean that the date might be from a previous ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please look very closely at your "want" data and then describe what happened to about half the records. You are doing more then getting a previous date value added to a current observation as the "want" shows entire records removed.&lt;/P&gt;
&lt;P&gt;So please describe in a bit more detail what the rules involved are. I have highlighted below observations that are apparently removed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441489"&gt;@SAS_Learner2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data like below. I am strugging to get the desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date2&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 18AUG2022:07:26:00 19AUG2022:15:00:00&lt;/FONT&gt;&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 19AUG2022:02:30:00 20AUG2022:11:00:00&lt;/FONT&gt;&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 20AUG2022:03:00:00 21AUG2022:12:30:00&lt;/FONT&gt;&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 21AUG2022:01:00:00 22AUG2022:11:30:00&lt;/FONT&gt;&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 22AUG2022:02:30:00 23AUG2022:12:30:00&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want date3 variable to be created and this value should come previous Date2 records and Date3 value should be less than Date1 value.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Date2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date3&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00 .&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00 18AUG2022:16:27:00&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00 19AUG2022:15:00:00&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00 20AUG2022:11:00:00&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00 21AUG2022:12:30:00&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Apr 2023 14:33:46 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-04-18T14:33:46Z</dc:date>
    <item>
      <title>Want to get previous date value to next record which is less than current record date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870364#M343774</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data like below. I am strugging to get the desired output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date2&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00&lt;BR /&gt;1 18AUG2022:07:26:00 19AUG2022:15:00:00&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00&lt;BR /&gt;1 19AUG2022:02:30:00 20AUG2022:11:00:00&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00&lt;BR /&gt;1 20AUG2022:03:00:00 21AUG2022:12:30:00&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00&lt;BR /&gt;1 21AUG2022:01:00:00 22AUG2022:11:30:00&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00&lt;BR /&gt;1 22AUG2022:02:30:00 23AUG2022:12:30:00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want date3 variable to be created and this value should come previous Date2 records and Date3 value should be less than Date1 value.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Date2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date3&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00 .&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00 18AUG2022:16:27:00&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00 19AUG2022:15:00:00&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00 20AUG2022:11:00:00&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00 21AUG2022:12:30:00&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help.&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;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2023 13:54:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870364#M343774</guid>
      <dc:creator>SAS_Learner2</dc:creator>
      <dc:date>2023-04-18T13:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: Want to get previous date value to next record which is less than current record date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870367#M343776</link>
      <description>&lt;P&gt;Do you have more than one ID value in the data? If so, when the ID changes what should happen with date3? Your current description would mean that the date might be from a previous ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please look very closely at your "want" data and then describe what happened to about half the records. You are doing more then getting a previous date value added to a current observation as the "want" shows entire records removed.&lt;/P&gt;
&lt;P&gt;So please describe in a bit more detail what the rules involved are. I have highlighted below observations that are apparently removed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/441489"&gt;@SAS_Learner2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data like below. I am strugging to get the desired output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date2&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 18AUG2022:07:26:00 19AUG2022:15:00:00&lt;/FONT&gt;&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 19AUG2022:02:30:00 20AUG2022:11:00:00&lt;/FONT&gt;&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 20AUG2022:03:00:00 21AUG2022:12:30:00&lt;/FONT&gt;&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 21AUG2022:01:00:00 22AUG2022:11:30:00&lt;/FONT&gt;&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00&lt;BR /&gt;&lt;FONT color="#800080"&gt;1 22AUG2022:02:30:00 23AUG2022:12:30:00&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want date3 variable to be created and this value should come previous Date2 records and Date3 value should be less than Date1 value.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;ID Date1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Date2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date3&lt;BR /&gt;1 18AUG2022:07:26:00 18AUG2022:16:27:00 .&lt;BR /&gt;1 19AUG2022:02:30:00 19AUG2022:15:00:00 18AUG2022:16:27:00&lt;BR /&gt;1 20AUG2022:03:00:00 20AUG2022:11:00:00 19AUG2022:15:00:00&lt;BR /&gt;1 21AUG2022:01:00:00 21AUG2022:12:30:00 20AUG2022:11:00:00&lt;BR /&gt;1 22AUG2022:02:30:00 22AUG2022:11:30:00 21AUG2022:12:30:00&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please help.&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2023 14:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870367#M343776</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-04-18T14:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Want to get previous date value to next record which is less than current record date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870371#M343777</link>
      <description>&lt;P&gt;You need to explain the rule that is eliminating some of the observations.&lt;/P&gt;
&lt;P&gt;If DATETIME3 is just the lagged value of DATETIME2 then it is NEVER going to be less than DATETIME1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS Your dataset does not have any DATE values.&amp;nbsp; Only DATETIME values.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2023 14:39:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870371#M343777</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-18T14:39:18Z</dc:date>
    </item>
    <item>
      <title>Re: Want to get previous date value to next record which is less than current record date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870379#M343779</link>
      <description>&lt;P&gt;I don't know what is the logic behind this problem, but this code can give you the desired output as you described above:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by id date1;
	if first.date1 then date3= lag(date2);
	format date3 datetime18.;
	if first.date1;
proc print;run; 
 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Apr 2023 14:52:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870379#M343779</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-04-18T14:52:27Z</dc:date>
    </item>
    <item>
      <title>Re: Want to get previous date value to next record which is less than current record date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870386#M343781</link>
      <description>&lt;P&gt;Thank generates the output expected.&lt;/P&gt;
&lt;P&gt;I would move the subsetting IF to the first statement. It makes the coding easier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id (datetime1-datetime2) (:datetime.);
  format datetime: datetime19.;
cards;
1 18AUG2022:07:26:00 18AUG2022:16:27:00
1 18AUG2022:07:26:00 19AUG2022:15:00:00
1 19AUG2022:02:30:00 19AUG2022:15:00:00
1 19AUG2022:02:30:00 20AUG2022:11:00:00
1 20AUG2022:03:00:00 20AUG2022:11:00:00
1 20AUG2022:03:00:00 21AUG2022:12:30:00
1 21AUG2022:01:00:00 21AUG2022:12:30:00
1 21AUG2022:01:00:00 22AUG2022:11:30:00
1 22AUG2022:02:30:00 22AUG2022:11:30:00
1 22AUG2022:02:30:00 23AUG2022:12:30:00
;

data expect ;
  input id (datetime1-datetime3) (:datetime.);
  format datetime: datetime19.;
cards;
1 18AUG2022:07:26:00 18AUG2022:16:27:00 .
1 19AUG2022:02:30:00 19AUG2022:15:00:00 18AUG2022:16:27:00
1 20AUG2022:03:00:00 20AUG2022:11:00:00 19AUG2022:15:00:00
1 21AUG2022:01:00:00 21AUG2022:12:30:00 20AUG2022:11:00:00
1 22AUG2022:02:30:00 22AUG2022:11:30:00 21AUG2022:12:30:00
;

data want;
  set have;
  by id datetime1;
  if first.datetime1;
  datetime3 = lag(datetime2);
  if first.id then datetime3=.;
  format datetime3 datetime19.;
run;

proc compare data=expect compare=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Apr 2023 15:05:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Want-to-get-previous-date-value-to-next-record-which-is-less/m-p/870386#M343781</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-18T15:05:47Z</dc:date>
    </item>
  </channel>
</rss>

