<?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: Find number of days between two dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880013#M347671</link>
    <description>&lt;P&gt;Your first statements&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	startdt = 1990-01-01;
	dt = 2012-02-01;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;are NOT establishing date values.&amp;nbsp; Instead, they are calculating arithmetic expressions on the right side of the equals sign and assigning the result to the variable on the left.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably need to use a date literal, as you did in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        startdate = '01Jan90'd;
        sdt = '01Feb2012'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above is NOT a date format.&amp;nbsp; It has nothing to do with the format (i.e. how a value is displayed).&amp;nbsp; You can assign any date format to a date value as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Added comment:&amp;nbsp; And has nothing to do with how a variable is input from an external source.&amp;nbsp; It's just a way to assign, within a SAS program, a constant value to be converted to SAS date value s (zero for 01jan1960, negative before that, and positive afterwards).&amp;nbsp; &amp;nbsp; It's not unlike using the comma informat&amp;nbsp; to remove commas when reading a numeric value (or comma format to add commas for display).&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;931  data want;
932    startdate='01jan1990'd;
933    sdt='01feb2012'd;
934    put (_all_) (=yymmdd10.);
935    put (_all_) (=date9.);
936  run;

startdate=1990-01-01 sdt=2012-02-01
startdate=01JAN1990 sdt=01FEB2012
NOTE: The data set WORK.WANT has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, if you are reading in date values from a text file, you SHOULD use the appropriate informat as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input (startdate sdt)  (:yymmdd10.);
   put (_all_) (=yymmdd10.);
   put (_all_) (=date9.);
datalines;
1990-01-01  2012-02-01
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally if you want to use expressions such as 1990-01-01 as constants to be converted to date values, you could use the INPUT function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  startdate=input('1990-01-01',yymmdd10.);
  sdt=input('2012-02-01',yymmdd10.);
  put (_all_) (=yymmdd10.);
  put (_all_) (=date9.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 10 Jun 2023 15:41:41 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-06-10T15:41:41Z</dc:date>
    <item>
      <title>Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/879999#M347659</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;I get dates in the IS8601da. format yymmdd10. and I need to find number of days, years, months into different variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, I have dates like Startdate = 1990-01-01 and Dt1 = 2012-02-01 and here I need to consider this Dt1 as end date. That means, I should get -8066.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to use INTCK, but it seems it works only with datew. format. With datew. format dates 01Jan90 and 01Feb12,&amp;nbsp; INTCK is giving -8066.&amp;nbsp; But , with yymmddw. format and it is giving 12.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tried using datdiff with yymmddw. format dates, but it is giving difference as 12. When used datdiff with datew. format dates it is giving -8066.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please, let me know how to find the difference between dates that are in yymmdd10. formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 12:15:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/879999#M347659</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-10T12:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880000#M347660</link>
      <description>&lt;P&gt;Using INTCK on any valid SAS date values should work with ANY date format, or for unformatted dates. Using DATDIF on any valid SAS date values should work with ANY date format, or unformatted dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data abc;
    firstdate=mdy(1,1,1990);
    seconddate=mdy(2,1,2012);
    format firstdate seconddate yymmdd10.;
    delta=intck('day',seconddate,firstdate);
    delta1=datdif(seconddate,firstdate,'act/act');
    delta2=firstdate-seconddate;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that for valid SAS dates, subtraction also works to find the number of days between two dates, and is the least typing of any method.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are getting wrong answers, I suspect your variables do not have valid SAS date values. Please provide (a portion of) your SAS data set (as working SAS data step code, and not in any other format) AND your code that is giving wrong answers.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 14:02:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880000#M347660</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-10T14:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880001#M347661</link>
      <description>&lt;P&gt;In SAS, a date is a date is a date, regardless of display format. So the INTCK or DATEDIFF functions will work with them.&lt;/P&gt;
&lt;P&gt;You only need to use the correct informat when reading the data into SAS in the first place.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We need more information. Where is the data coming from, and in which form (file format) do you get it?&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 13:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880001#M347661</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-06-10T13:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880006#M347665</link>
      <description>&lt;P&gt;Thank you very much for the clarification. Actually, I don't have the code to create the dataset. I am working on a dataset that is already created and it has startDate and dt columns which are in the yymmdd10. format. I am using &lt;STRONG&gt;direct subtraction&lt;/STRONG&gt; of these dates and it's giving expected values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I want to check these dates if they are calculated correctly or not. Hence, I am trying to check giving the yymmdd10. date formats like this:&lt;/P&gt;
&lt;PRE&gt;data testdt;
	startdt = 1990-01-01;
	dt = 2012-02-01;

	startdate = '01Jan90'd;
	sdt = '01Feb2012'd;

/*	informat startdt dt yymmdd10.;*/
	format startdt dt yymmdd10.;

	diff1 = startdt - dt;

	diff2 = datdif(startdt, dt, 'ACT/ACT');

	diff3 = intck('day',startdt, dt);

	diff4 = datdif(sdt, startdate, 'ACT/ACT');

	diff5 = intck('day', sdt, startdate);

run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 14:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880006#M347665</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-10T14:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880007#M347666</link>
      <description>&lt;P&gt;Thank you for the clarification. I am working on a dataset that is already created and I don't have the code that created it. It has columns which are numeric and contain date values in yymmdd10. format and I have to find the difference between them. I get these columns into a macro variable and doing direct subtraction of these variables and it is giving correct values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, for my understanding, I am trying to check whether the values are correct or not and I am tryin using the code that I have mentioned in reply to PaigeMiller.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 14:13:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880007#M347666</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-10T14:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880013#M347671</link>
      <description>&lt;P&gt;Your first statements&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	startdt = 1990-01-01;
	dt = 2012-02-01;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;are NOT establishing date values.&amp;nbsp; Instead, they are calculating arithmetic expressions on the right side of the equals sign and assigning the result to the variable on the left.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You probably need to use a date literal, as you did in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        startdate = '01Jan90'd;
        sdt = '01Feb2012'd;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above is NOT a date format.&amp;nbsp; It has nothing to do with the format (i.e. how a value is displayed).&amp;nbsp; You can assign any date format to a date value as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Added comment:&amp;nbsp; And has nothing to do with how a variable is input from an external source.&amp;nbsp; It's just a way to assign, within a SAS program, a constant value to be converted to SAS date value s (zero for 01jan1960, negative before that, and positive afterwards).&amp;nbsp; &amp;nbsp; It's not unlike using the comma informat&amp;nbsp; to remove commas when reading a numeric value (or comma format to add commas for display).&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;931  data want;
932    startdate='01jan1990'd;
933    sdt='01feb2012'd;
934    put (_all_) (=yymmdd10.);
935    put (_all_) (=date9.);
936  run;

startdate=1990-01-01 sdt=2012-02-01
startdate=01JAN1990 sdt=01FEB2012
NOTE: The data set WORK.WANT has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, if you are reading in date values from a text file, you SHOULD use the appropriate informat as in&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input (startdate sdt)  (:yymmdd10.);
   put (_all_) (=yymmdd10.);
   put (_all_) (=date9.);
datalines;
1990-01-01  2012-02-01
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally if you want to use expressions such as 1990-01-01 as constants to be converted to date values, you could use the INPUT function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
  startdate=input('1990-01-01',yymmdd10.);
  sdt=input('2012-02-01',yymmdd10.);
  put (_all_) (=yymmdd10.);
  put (_all_) (=date9.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 15:41:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880013#M347671</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-06-10T15:41:41Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880014#M347672</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you very much for the clarification. Actually, I don't have the code to create the dataset.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I am requesting that you TYPE IN yourself a small portion of the data, into working SAS data step code. Preferably, you can also follow &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;these instructions&lt;/A&gt; to produce working SAS data step code. We need a properly produced replicate of your existing SAS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;However, I want to check these dates if they are calculated correctly or not. Hence, I am trying to check giving the yymmdd10. date formats like this:&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	startdt = 1990-01-01;
	dt = 2012-02-01;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are not anything that SAS recognizes as a date. The fact that humans recognize this is a date is irrelevant. SAS consider STARTDT to be the integer 1990 minus 01 minus 01, which equals 1988. SAS considers DT to be 2012 minus 02 minus 01 which equals 2009. These are clearly not dates, and so any arithmetic or use of INTCK/INTNX/DATDIF will fail on these variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;	startdate = '01Jan90'd;
	sdt = '01Feb2012'd;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By using this syntax, then SAS recognizes these as dates and so then arithmetic and INTCK/INTNX/DATDIF will work properly. The above is is not a DATE9. format, it is a date literal. This is so important, I am going to say it again in big bold capital letters for emphasis. &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;THIS IS NOT A DATE9. FORMAT, IT IS A DATE LITERAL.&lt;/STRONG&gt; &lt;FONT color="#333333"&gt;A date literal is one way that humans can communicate what they see as a date to SAS, it translates human understanding to SAS understanding; just as if you speak English, you need Japanese to be translated in order for the English speaking person to understand it; a date literal does translation.&lt;/FONT&gt;&lt;STRONG&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 15:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880014#M347672</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-10T15:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880016#M347674</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439180"&gt;@Moksha&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you for the clarification. I am working on a dataset that is already created and I don't have the code that created it. It has columns which are numeric and contain date values in yymmdd10. format and I have to find the difference between them. I get these columns into a macro variable and doing direct subtraction of these variables and it is giving correct values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But, for my understanding, I am trying to check whether the values are correct or not and I am tryin using the code that I have mentioned in reply to PaigeMiller.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Run Proc contents on the data set and show us the result.&lt;/P&gt;
&lt;PRE&gt;Proc contents data=yourdataset;
run;&lt;/PRE&gt;
&lt;P&gt;Copy the results and paste into the forum window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With an accurate description of the variable properties and example values we can provide the approach to make the values dates.&lt;/P&gt;
&lt;P&gt;SAS has something like 28 provided ISO date, time and datetime formats and 17 Informats to read external files using ISO layouts to read into date, time or datetime values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When talking about SAS you should ONLY ever use the word "format" as it relates to the setting of the format property for a variable. External data sources don't have "formats" in this context, layout perhaps but not the assigned property that SAS calls a format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;About once a week or so we will get someone that has created or inherited a variable that is numeric and the content is something like 20210615 or 15062021 or (worse) 6152021 that masquerades as a "date". This is quite common when using Proc Import or an import task to read external data.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 16:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880016#M347674</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-06-10T16:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880027#M347680</link>
      <description>&lt;P&gt;Thank you very much. Now, I understand why my code where I am checking for the dates using datdif and intck functions are not working as expected.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 17:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880027#M347680</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-10T17:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find number of days between two dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880028#M347681</link>
      <description>&lt;P&gt;I have already provided the dataset which I am trying to check the date differences using datdif, intck and direct subtraction. With your explantion and&amp;nbsp;mkeintz's explanation, I understand the problem with my checking of date differences using datdiff and intck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 17:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-number-of-days-between-two-dates/m-p/880028#M347681</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-06-10T17:17:45Z</dc:date>
    </item>
  </channel>
</rss>

