<?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 Use Retain to Determine Number of Days Between Dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978628#M378657</link>
    <description>&lt;P&gt;Good afternoon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need a data step to determine the number of days between succeeding rows based on a group (see the example). I've tried to use RETAIN to achieve this, but I haven't been able to make any progress at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could someone point me in the correct direction to perform this? I don't have an example since I haven't gotten anything to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much to anyone willing to help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="1152"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="142"&gt;KEY&lt;/TD&gt;
&lt;TD width="142"&gt;REVIEW_DATE&lt;/TD&gt;
&lt;TD width="142"&gt;REVIEW_TYPE&lt;/TD&gt;
&lt;TD width="64"&gt;Days&lt;/TD&gt;
&lt;TD width="662"&gt;Comment&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;3/31/2023&lt;/TD&gt;
&lt;TD&gt;INIT&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;All INIT with the same KEY begin with zero&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;9/6/2023&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;159&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;3/6/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;182&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;9/18/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;196&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;4/4/2025&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;198&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;2/22/2024&lt;/TD&gt;
&lt;TD&gt;INIT&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;All INIT with the same KEY begin with zero&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;6/30/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;129&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;7/1/2025&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;366&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
    <pubDate>Tue, 11 Nov 2025 00:26:12 GMT</pubDate>
    <dc:creator>Jeff_DOC</dc:creator>
    <dc:date>2025-11-11T00:26:12Z</dc:date>
    <item>
      <title>Use Retain to Determine Number of Days Between Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978628#M378657</link>
      <description>&lt;P&gt;Good afternoon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need a data step to determine the number of days between succeeding rows based on a group (see the example). I've tried to use RETAIN to achieve this, but I haven't been able to make any progress at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could someone point me in the correct direction to perform this? I don't have an example since I haven't gotten anything to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much to anyone willing to help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="1152"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="142"&gt;KEY&lt;/TD&gt;
&lt;TD width="142"&gt;REVIEW_DATE&lt;/TD&gt;
&lt;TD width="142"&gt;REVIEW_TYPE&lt;/TD&gt;
&lt;TD width="64"&gt;Days&lt;/TD&gt;
&lt;TD width="662"&gt;Comment&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;3/31/2023&lt;/TD&gt;
&lt;TD&gt;INIT&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;All INIT with the same KEY begin with zero&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;9/6/2023&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;159&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;3/6/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;182&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;9/18/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;196&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;380&lt;/TD&gt;
&lt;TD&gt;4/4/2025&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;198&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;2/22/2024&lt;/TD&gt;
&lt;TD&gt;INIT&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;TD&gt;All INIT with the same KEY begin with zero&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;6/30/2024&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;129&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;381&lt;/TD&gt;
&lt;TD&gt;7/1/2025&lt;/TD&gt;
&lt;TD&gt;6MONTH&lt;/TD&gt;
&lt;TD&gt;366&lt;/TD&gt;
&lt;TD&gt;Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Tue, 11 Nov 2025 00:26:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978628#M378657</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2025-11-11T00:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to Determine Number of Days Between Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978629#M378658</link>
      <description>&lt;P&gt;Here's one way of doing this - using RETAIN requires use of a variable not being read in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Reviews;
  input @1 key $3. @6 Review_Date mmddyy10.;
  format Review_Date date9.;
  datalines;
380   3/31/2023
380	  9/6/2023
380	  3/6/2024
380	  9/18/2024
380	  4/4/2025
381	  2/22/2024
381	  6/30/2024
381	  7/1/2025
;
run;

data Reviews_Want;
  keep key Review_Date Days;
  set Reviews;
  by key;
  if first.key then Days = 0;
  else Days = Review_Date - Last_Review_Date;
  output;
  retain Last_Review_Date;
  Last_Review_Date = Review_Date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2025 01:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978629#M378658</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2025-11-11T01:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to Determine Number of Days Between Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978632#M378659</link>
      <description>&lt;P&gt;First let's convert your listing into an actual SAS dataset so we have something to program with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd dlm='|' truncover ;
  input KEY REVIEW_DATE :mmddyy. REVIEW_TYPE $ Days Comment :$100. ;
  format review_date yymmdd10.;
cards;
380|3/31/2023|INIT|0|All INIT with the same KEY begin with zero
380|9/6/2023|6MONTH|159|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
380|3/6/2024|6MONTH|182|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
380|9/18/2024|6MONTH|196|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
380|4/4/2025|6MONTH|198|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
381|2/22/2024|INIT|0|All INIT with the same KEY begin with zero
381|6/30/2024|6MONTH|129|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
381|7/1/2025|6MONTH|366|Subsequent rows with the same key determine the number of days from the preceding row REVIEW_DATE.
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So it looks like from they values you show of DAYS that you don't actually want to RETAIN anything.&amp;nbsp; Instead you just want to use the PREVIOUS value, which you can so with the LAG() function.&amp;nbsp; Make not to execute LAG() conditionally, it can only return values that you previously passed it.&amp;nbsp; Also make sure to remember to ignore/replace the values calculated for the FIRST observation in a group since the previous date value is from the previous group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by key review_date;
  new_days = review_date - lag(review_date);
  if first.key then new_days=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1762823682247.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111227i100D32603F7BEA52/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1762823682247.png" alt="Tom_0-1762823682247.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;You would want to use the RETAIN statement if your goal was instead to calculate the difference since the INIT record's date (usually called a BASELINE date).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  by key review_date ;
  retain baseline ;
  format baseline yymmdd10.;
  if first.key then call missing(baseline);
  if review_type='INIT' then baseline=review_date ;
  new_days = review_date - baseline;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1762823804580.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111228i5746863FD4297917/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_1-1762823804580.png" alt="Tom_1-1762823804580.png" /&gt;&lt;/span&gt;&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, 11 Nov 2025 01:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978632#M378659</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-11-11T01:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to Determine Number of Days Between Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978643#M378663</link>
      <description>&lt;P&gt;Don't forget the DIF function:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt; new_days = review_date - lag(review_date);&lt;/LI-CODE&gt;
&lt;P&gt;is the same as&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt; new_days = dif(review_date);&lt;/LI-CODE&gt;
&lt;P&gt;Same concern as LAG for conditional operation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Nov 2025 06:12:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/978643#M378663</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-11-11T06:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Use Retain to Determine Number of Days Between Dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/979142#M378747</link>
      <description>&lt;P&gt;Sorry about the lateness of my solution acceptance. This works perfectly and gives me what I need to begin building in my conditional logic. It also explained the lag function to me as it's not something I've used before, so thank you for all of that.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Nov 2025 17:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-Retain-to-Determine-Number-of-Days-Between-Dates/m-p/979142#M378747</guid>
      <dc:creator>Jeff_DOC</dc:creator>
      <dc:date>2025-11-19T17:31:23Z</dc:date>
    </item>
  </channel>
</rss>

