<?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: datepart() returns zero in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630749#M18899</link>
    <description>&lt;P&gt;Raisedate contains date values, so when you run the datepart function on it you get a datetime in the morning of January 1, 1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should read some SAS Global Forum papers on date and datetime values and functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several ways to handle the problem you describe, but one way is to use the intnx function to create a new variable containing the start date value for the period you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;first_of_month = intnx('month', raisedate, 0);&lt;/P&gt;
&lt;P&gt;format first_of_month monyy7.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also get the first day of the quarter, year, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I recommend against using 2-digit years.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 09 Mar 2020 20:41:15 GMT</pubDate>
    <dc:creator>JackHamilton</dc:creator>
    <dc:date>2020-03-09T20:41:15Z</dc:date>
    <item>
      <title>datepart() returns zero</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630737#M18898</link>
      <description>&lt;P&gt;I'm working with a datetime variable, but I want to reduce it to at least a day/month/year variable, or preferably monthyear, so that I can then select only distinct cases of the reduced format.&amp;nbsp; Basically, I want to recode the dates into season/year variables for many observations.&amp;nbsp; However, the datepart() function doesn't seem to be working for me.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I'm using is this, where raisedate is the original date variable:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want; data have;&lt;/P&gt;&lt;P&gt;datetime = raisedate;&lt;BR /&gt;format datetime dateampm.;&lt;BR /&gt;datejulian = raisedate;&lt;BR /&gt;format datejulian julian.;&lt;BR /&gt;date = datepart(raisedate);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Produces this:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id raisedate datetime datejulian datepart&lt;/P&gt;&lt;P&gt;1 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;2 27APR2017 01JAN60:05:48:56 AM 17117 0&lt;BR /&gt;3 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;4 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;5 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;6 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;7 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;8 21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;9 12MAR2019 01JAN60:06:00:20 AM 19071 0&lt;BR /&gt;10 26APR2019 01JAN60:06:01:05 AM 19116 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, for some reason I can reformat the date time, but the datepart() function isn't working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Perhaps related to this issue, is that if I select distinct on the raisedate variable (i.e. without an id variable), I still get multiple indistinct entries, such as from above, even with the various formats broken out:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;BR /&gt;21AUG2014 01JAN60:05:32:36 AM 14233 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Clearly, they are not obviously distinct depending on format.&amp;nbsp; I've ran into issues with lack of rounding hidden from user view before in SAS, but I'm not sure if that's the issue here, or how I would "round" a date to resolve it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any recommendations for what to do?&amp;nbsp; Many thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 20:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630737#M18898</guid>
      <dc:creator>tellmeaboutityo</dc:creator>
      <dc:date>2020-03-09T20:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: datepart() returns zero</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630749#M18899</link>
      <description>&lt;P&gt;Raisedate contains date values, so when you run the datepart function on it you get a datetime in the morning of January 1, 1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should read some SAS Global Forum papers on date and datetime values and functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are several ways to handle the problem you describe, but one way is to use the intnx function to create a new variable containing the start date value for the period you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;first_of_month = intnx('month', raisedate, 0);&lt;/P&gt;
&lt;P&gt;format first_of_month monyy7.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also get the first day of the quarter, year, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I recommend against using 2-digit years.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 20:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630749#M18899</guid>
      <dc:creator>JackHamilton</dc:creator>
      <dc:date>2020-03-09T20:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: datepart() returns zero</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630756#M18900</link>
      <description>&lt;P&gt;I'm guessing that where your code reads 'data want; data have;' you actually ran 'data want; set have'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your raisedate variable appears to be a datetime variable that is formatted as date9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given your example results all of the date variables will have values of 0 as that is the value of the data 01jan1960.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following example, I changed the dates so that the date variable will have some variance:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  informat raisedate datetime21.;
  format raisedate date9.;
  input raisedate;
  cards;
01JAN1960:05:32:36 AM
02JAN1960:05:48:56 AM
03JAN1960:05:32:36 AM
04JAN1960:05:32:36 AM
05JAN1960:05:32:36 AM
06JAN1960:05:32:36 AM
07JAN1960:05:32:36 AM
08JAN1960:05:32:36 AM
09JAN1960:06:00:20 AM
10JAN1960:06:01:05 AM
;
run;

data want;
  set have;
  datetime = raisedate;
  format datetime dateampm.;
  datejulian = raisedate;
  format datejulian julian.;
  date = datepart(raisedate);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 21:10:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630756#M18900</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2020-03-09T21:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: datepart() returns zero</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630764#M18901</link>
      <description>&lt;P&gt;I would strongly suggest NOT creating some not-quite-a-date value to indicate the start of a month or calendar quarter but instead use an actual date value as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;suggests.&amp;nbsp; Not actual date values will have&amp;nbsp;issues with graphing as the interval between Dec and Jan is extremely uneven if you use 201912 (year month) followed by 202001.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you leave it as a date then manipulations such as intervals&amp;nbsp;can still be done with the data functions and formats can create a lot of date groupings just by changing the format. Formats with 201912 are kind of stuck (though you can have that appearance for the value with YYMMn6).&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 22:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630764#M18901</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-03-09T22:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: datepart() returns zero</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630770#M18902</link>
      <description>&lt;P&gt;Dates are stored as number of days. Datetime is stored as number of seconds.&amp;nbsp; To convert from DATETIME vales to DATE values all the DATEPART() function essentially doing is taking the remainder after integer division by (24 hours)*(60 minutes)*(60 seconds).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So when you take the datepart of values like 19,956 (which is 21st of&amp;nbsp; Auguest in 2014) then you get zero (which is the first day of 1960).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1220  data test;
1221    date='21AUG2014'd;
1222    datetime=date;
1223    put 'COMMA12.    ' (date datetime) (= comma12.);
1224    put 'YYMMDD10.   ' (date datetime) (= yymmdd10.);
1225    put 'DATETIME20. ' (date datetime) (= datetime20.);
1226  run;

COMMA12.    date=19,956 datetime=19,956
YYMMDD10.   date=2014-08-21 datetime=2014-08-21
DATETIME20. date=01JAN1960:05:32:36 datetime=01JAN1960:05:32:36
&lt;/PRE&gt;
&lt;P&gt;What are you trying to do?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Mar 2020 22:32:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/datepart-returns-zero/m-p/630770#M18902</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-09T22:32:02Z</dc:date>
    </item>
  </channel>
</rss>

