<?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: max and  min of date fields in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736016#M229291</link>
    <description>&lt;P&gt;Also, you can find the number of days difference as follows&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	begin_dttm = '05jan2020:00:00:00'dt;
	end_dttm = datetime(); 
    diff=intnx('dtday',begin_dttm,end_dttm);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Apr 2021 15:14:21 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-04-21T15:14:21Z</dc:date>
    <item>
      <title>max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736000#M229284</link>
      <description>&lt;P&gt;Hi SAS Users,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working on substracting max(end_date) - min(begin_date) on one code.&amp;nbsp; end_Date&amp;nbsp; and Begin_date fields are datetime fields. when i add max and min to those fields they are converting to numbers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking to get the total number of days from that substraction.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Total_days = max(end_Date) - min (begin_date)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Ana&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 14:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736000#M229284</guid>
      <dc:creator>SASAna</dc:creator>
      <dc:date>2021-04-21T14:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736011#M229288</link>
      <description>&lt;P&gt;The reason that the result of your formula is numeric, is that in SAS a date or datetime is in fact numeric with a SAS format applied to it. If you remove the format SAS stores a date as the number of days since 1 January 1960 and a datetime as the number of seconds since 1 January 1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are purely interested in the difference in days between two days, you can subtract them. Since you are working with datetimes, you will get the difference in seconds. I would recommend to convert the datetimes to dates first and then subtracting them. You can do this using the datepart function.&lt;/P&gt;
&lt;PRE&gt;data want;
	begin_dttm = '05jan2020:00:00:00'dt;
	end_dttm = datetime(); 

	begin_date = datepart(begin_dttm); 
	end_date = datepart(end_dttm); 

	days_diff = end_date - begin_date; 
	seconds_diff = end_dttm - begin_dttm; 

	format 
		begin_dttm end_dttm datetime20.2
		begin_date end_date date9.
	;
run;&lt;/PRE&gt;
&lt;P&gt;This will result in the following:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="NicoM_0-1619017595661.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58464i67B0AD4DC5C33ED8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="NicoM_0-1619017595661.png" alt="NicoM_0-1619017595661.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;By the way, how are you using the min and the max function? Is this within a data step or in an SQL query?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:07:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736011#M229288</guid>
      <dc:creator>NicoM</dc:creator>
      <dc:date>2021-04-21T15:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736012#M229289</link>
      <description>&lt;P&gt;It they are datetime values, then because SAS expresses internally the datetimes as the number of seconds since 01JAN1960:00:00:00 when you do a subtraction, you get the difference in seconds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since there are 24 hours a day and 60 minutes in an hour and 60 seconds in an hour, then you take the difference (which as I said is in seconds) and divide by 24*60*60 to get the number of days. You can choose to round off to integer days, if you wish.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736012#M229289</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-21T15:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736013#M229290</link>
      <description>&lt;P&gt;Max of what? What you show is the maximum of a single value of end_date so that doesn't really make much sense. Same with your minimum.&lt;/P&gt;
&lt;P&gt;Of course the result is a number , you are doing subtraction. You just likely haven't paid attention to the bit about SAS DATETIME values are measure in seconds. So the subtraction you are doing is giving you the number of seconds between the two values.&lt;/P&gt;
&lt;P&gt;If you are looking for the number of days between two values and your values are actually datetime values then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Total_days = intck('dtday',begin_date,end_date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;may be what you are looking for. INTCK is the function to return intervals between date, datetime or time values. the "DTDAY" tells SAS the expected values are datetime, the DT part and you want DAY as the interval returned. You could ask for "dtmonth" to get the months between or "dtyear" for years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise Provide some example data and what the result should look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/ta-p/424354&lt;/A&gt; has a PDF with much information about dates.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736013#M229290</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-21T15:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736016#M229291</link>
      <description>&lt;P&gt;Also, you can find the number of days difference as follows&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	begin_dttm = '05jan2020:00:00:00'dt;
	end_dttm = datetime(); 
    diff=intnx('dtday',begin_dttm,end_dttm);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736016#M229291</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-21T15:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736017#M229292</link>
      <description>&lt;P&gt;Are there multiple dates for a given person? If so, you can do it in a single PROC SQL statement. Note that it is repetitive just to show you the steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id begin_date :datetime. end_date :datetime.;
format begin_date end_date datetime.;
datalines;
1 21APR2021:11:00:00 24APR2021:11:00:00
1 31MAR2021:11:00:00 12APR2021:11:00:00
2 14FEB2021:09:00:00 15NOV2021:04:00:00
;
run;

proc sql;
	select
				distinct id,
				min(datepart(begin_date)) as min_begin_date
					format = mmddyy10.,
				max(datepart(end_date)) as max_end_date
					format = mmddyy10.,
				intck("days", min(datepart(begin_date)), max(datepart(end_date)), "c") as total_days
	from
				have
	group by
				id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id min_begin_date max_end_date total_days 
1 03/31/2021 04/24/2021 24 
2 02/14/2021 11/15/2021 274 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I also assumed you meant an actual datetime format and not just a date. If you don't have a datetime format, you don't need to use the DATEPART function.&lt;/P&gt;
&lt;P&gt;See here for more about the INTCK function: &lt;A href="https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html" target="_self"&gt;https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Apr 2021 15:15:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736017#M229292</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-04-21T15:15:25Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736027#M229294</link>
      <description>Did you mean to use INTCK?</description>
      <pubDate>Wed, 21 Apr 2021 15:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/736027#M229294</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-04-21T15:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: max and  min of date fields</title>
      <link>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/737764#M230031</link>
      <description>Thanks much. It worked very well.</description>
      <pubDate>Wed, 28 Apr 2021 23:28:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/max-and-min-of-date-fields/m-p/737764#M230031</guid>
      <dc:creator>SASAna</dc:creator>
      <dc:date>2021-04-28T23:28:53Z</dc:date>
    </item>
  </channel>
</rss>

