<?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: Splitting date variable into multiple column - Date and Hour in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419705#M103204</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139205"&gt;@tobyfarms&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I know this is simple, but other posts are not working for me.&amp;nbsp; Maybe it is the formatting.&lt;/P&gt;
&lt;P&gt;I need to split the 1st column (Datetime) into 2 columns.&amp;nbsp; (date) and (hour).&amp;nbsp;&amp;nbsp; Date column should remain in the same format and positions 9 and 10 are the hour.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ace_profiles_test;
set work.'new ace_profiles_test'n;
Date=substr(datetime,1,8);
hour=substr(datetime,9,2);
date=input(datetime, date9.) ;   /*format date date9.; */
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show the log from when your run that code. You should have a message like this:&lt;/P&gt;
&lt;PRE&gt;NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
&lt;/PRE&gt;
&lt;P&gt;Your first use of date with the SUBSTR function creates a character variable date. (Also indicating that your datetime is character not numeric as in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s example).&amp;nbsp;The second use of date with input 1) does the conversion of the first 9 characters to a SAS date value and 2) stores the resulting number of days into a the previously created character variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you remove the first date= statement then you will have a numeric value from the data=input(...&lt;/P&gt;
&lt;P&gt;Then add the statement&lt;/P&gt;
&lt;P&gt;Format date date9.;&lt;/P&gt;
&lt;P&gt;so that the date numeric value is displayed in the date9. format.&lt;/P&gt;</description>
    <pubDate>Fri, 08 Dec 2017 21:21:09 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-12-08T21:21:09Z</dc:date>
    <item>
      <title>Splitting date variable into multiple column - Date and Hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419568#M103183</link>
      <description>&lt;P&gt;I know this is simple, but other posts are not working for me.&amp;nbsp; Maybe it is the formatting.&lt;/P&gt;&lt;P&gt;I need to split the 1st column (Datetime) into 2 columns.&amp;nbsp; (date) and (hour).&amp;nbsp;&amp;nbsp; Date column should remain in the same format and positions 9 and 10 are the hour.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ace_profiles_test;
set work.'new ace_profiles_test'n;
Date=substr(datetime,1,8);
hour=substr(datetime,9,2);
date=input(datetime, date9.) ;   /*format date date9.; */
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;DATETIME&lt;/TD&gt;&lt;TD&gt;FORECAST&lt;/TD&gt;&lt;TD&gt;RECORDER&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 0:00:00&lt;/TD&gt;&lt;TD&gt;1.786429151&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 1:00:00&lt;/TD&gt;&lt;TD&gt;1.594529522&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 2:00:00&lt;/TD&gt;&lt;TD&gt;1.532611857&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 3:00:00&lt;/TD&gt;&lt;TD&gt;1.408968173&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 4:00:00&lt;/TD&gt;&lt;TD&gt;1.452870258&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 5:00:00&lt;/TD&gt;&lt;TD&gt;1.48028587&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 6:00:00&lt;/TD&gt;&lt;TD&gt;1.429192766&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 7:00:00&lt;/TD&gt;&lt;TD&gt;1.466590829&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 8:00:00&lt;/TD&gt;&lt;TD&gt;1.653212565&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 9:00:00&lt;/TD&gt;&lt;TD&gt;1.629932182&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 10:00:00&lt;/TD&gt;&lt;TD&gt;1.845873452&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 11:00:00&lt;/TD&gt;&lt;TD&gt;1.995616508&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 12:00:00&lt;/TD&gt;&lt;TD&gt;2.215903722&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 13:00:00&lt;/TD&gt;&lt;TD&gt;2.189244802&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 14:00:00&lt;/TD&gt;&lt;TD&gt;2.326021186&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 15:00:00&lt;/TD&gt;&lt;TD&gt;2.434289859&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 16:00:00&lt;/TD&gt;&lt;TD&gt;2.346286646&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 17:00:00&lt;/TD&gt;&lt;TD&gt;2.498229065&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 18:00:00&lt;/TD&gt;&lt;TD&gt;2.163318795&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 19:00:00&lt;/TD&gt;&lt;TD&gt;2.117043808&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 20:00:00&lt;/TD&gt;&lt;TD&gt;2.285380221&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 21:00:00&lt;/TD&gt;&lt;TD&gt;2.319593889&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 22:00:00&lt;/TD&gt;&lt;TD&gt;2.271841793&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01Oct2017 23:00:00&lt;/TD&gt;&lt;TD&gt;2.033073233&lt;/TD&gt;&lt;TD&gt;NJAMS&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 08 Dec 2017 14:37:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419568#M103183</guid>
      <dc:creator>tobyfarms</dc:creator>
      <dc:date>2017-12-08T14:37:00Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting date variable into multiple column - Date and Hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419576#M103185</link>
      <description>&lt;P&gt;Always a good idea to convert dates or datetime values stored as text to valid SAS dates/datetime values first, then take advantage of SAS date functions afterwards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input DATETIME:$50. FORECAST:best20.	RECORDER$;
infile datalines4 dlm=',';
datalines;
01Oct2017 0:00:00,1.786429151,NJAMS
01Oct2017 1:00:00,1.594529522,NJAMS
01Oct2017 2:00:00,1.532611857,NJAMS
01Oct2017 3:00:00,1.408968173,NJAMS
01Oct2017 4:00:00,1.452870258,NJAMS
01Oct2017 5:00:00,1.48028587,NJAMS
01Oct2017 6:00:00,1.429192766,NJAMS
01Oct2017 7:00:00,1.466590829,NJAMS
01Oct2017 8:00:00,1.653212565,NJAMS
01Oct2017 9:00:00,1.629932182,NJAMS
01Oct2017 10:00:00,1.845873452,NJAMS
01Oct2017 11:00:00,1.995616508,NJAMS
01Oct2017 12:00:00,2.215903722,NJAMS
01Oct2017 13:00:00,2.189244802,NJAMS
01Oct2017 14:00:00,2.326021186,NJAMS
01Oct2017 15:00:00,2.434289859,NJAMS
01Oct2017 16:00:00,2.346286646,NJAMS
01Oct2017 17:00:00,2.498229065,NJAMS
01Oct2017 18:00:00,2.163318795,NJAMS
01Oct2017 19:00:00,2.117043808,NJAMS
01Oct2017 20:00:00,2.285380221,NJAMS
01Oct2017 21:00:00,2.319593889,NJAMS
01Oct2017 22:00:00,2.271841793,NJAMS
01Oct2017 23:00:00,2.033073233,NJAMS
;;;;

data want;
	set have;

	dt=input(DATETIME, ANYDTDTM18.);

	date=datepart(dt);
	hour=hour(dt);

	format dt datetime18. date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Dec 2017 14:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419576#M103185</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-12-08T14:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting date variable into multiple column - Date and Hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419604#M103190</link>
      <description>&lt;P&gt;Example data: Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From your post your datetime (NOT a date) could be character, we have no way to know.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;was kind enough to show how example data should be presented. But he had to make choices about reading your example and his variable types may not be yours.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 15:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419604#M103190</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-08T15:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting date variable into multiple column - Date and Hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419656#M103201</link>
      <description>&lt;P&gt;Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp; - the current format of the datetime column is DATETIME18, which may be causing the issue of my code not converting the field into a date field.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 18:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419656#M103201</guid>
      <dc:creator>tobyfarms</dc:creator>
      <dc:date>2017-12-08T18:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: Splitting date variable into multiple column - Date and Hour</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419705#M103204</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/139205"&gt;@tobyfarms&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I know this is simple, but other posts are not working for me.&amp;nbsp; Maybe it is the formatting.&lt;/P&gt;
&lt;P&gt;I need to split the 1st column (Datetime) into 2 columns.&amp;nbsp; (date) and (hour).&amp;nbsp;&amp;nbsp; Date column should remain in the same format and positions 9 and 10 are the hour.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.ace_profiles_test;
set work.'new ace_profiles_test'n;
Date=substr(datetime,1,8);
hour=substr(datetime,9,2);
date=input(datetime, date9.) ;   /*format date date9.; */
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Show the log from when your run that code. You should have a message like this:&lt;/P&gt;
&lt;PRE&gt;NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
&lt;/PRE&gt;
&lt;P&gt;Your first use of date with the SUBSTR function creates a character variable date. (Also indicating that your datetime is character not numeric as in &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s example).&amp;nbsp;The second use of date with input 1) does the conversion of the first 9 characters to a SAS date value and 2) stores the resulting number of days into a the previously created character variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you remove the first date= statement then you will have a numeric value from the data=input(...&lt;/P&gt;
&lt;P&gt;Then add the statement&lt;/P&gt;
&lt;P&gt;Format date date9.;&lt;/P&gt;
&lt;P&gt;so that the date numeric value is displayed in the date9. format.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 21:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Splitting-date-variable-into-multiple-column-Date-and-Hour/m-p/419705#M103204</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-12-08T21:21:09Z</dc:date>
    </item>
  </channel>
</rss>

