<?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: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578780#M164210</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105081"&gt;@eap&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;With the exception where one or all 3 fields have missing values, the above is a representative sample of the combinations found in the dataset.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is this a response to something? Can not tell what it relates to.&lt;/P&gt;</description>
    <pubDate>Fri, 02 Aug 2019 16:58:38 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-08-02T16:58:38Z</dc:date>
    <item>
      <title>How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578708#M164177</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was given a huge dataset containing 2 data fields (procedurestarttime &amp;amp; procedureendtime) with multiple time formats (example below). The time interval values are not always correct. Therefore, I want to calculate the time interval from start and end time and only use the inputted time_interval field only when everything else is missing.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I imagine I need a seires of IF THEN DO statements to separately strip the dates, pm,&amp;nbsp; AM and insert ‘:’s when necessary followed by informats/formats. I’m not sure how to identify the different string types in order to apply a conversion.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I'd appreciate any suggestions regarding possible approaches. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;ProcedureStarttime &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;U&gt; ProcedureEndtime&lt;/U&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;U&gt; Time_interval&lt;/U&gt;&lt;/STRONG&gt;&lt;BR /&gt;14:08 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15:31 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:23&lt;BR /&gt;8:56:00 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 10:40:00 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1:44:00&lt;BR /&gt;7:14:00 AM &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 8:14:00 AM &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:00:00&lt;BR /&gt;1/23/2018 11:14 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1/23/2018 12:03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 12:49:00 AM&lt;BR /&gt;2/8/2018 18:47 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2/8/2018 19:50 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:03&lt;BR /&gt;10:00am &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 12:00pm &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 120&lt;BR /&gt;1350 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1532 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; Not Reported&lt;BR /&gt;01-06-2008 09:35 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01-06-2008 11:10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:35&lt;/P&gt;&lt;P&gt;10:00am &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12:00pm &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 120&lt;BR /&gt;1350 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1532 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Not Reported&lt;BR /&gt;01-06-2008 09:35 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01-06-2008 11:10 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1:35&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
set have;

if procedurestarttime="DON'T KNOW" then do;
  starttime = tranwrd(procedurestarttime, '/ /', ' ');
end;
if procedurestarttime="DON'T KNOW" then do;
  starttime = tranwrd(procedurestarttime,'AM', ' ');
end;
if procedurestarttime="DON'T KNOW" then do;
starttime = timepart(procedurestarttime);
end;

/*Do same for procedurendtime*/


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 14:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578708#M164177</guid>
      <dc:creator>eap</dc:creator>
      <dc:date>2019-08-02T14:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578725#M164178</link>
      <description>Does the above indicate an example of one of each type of what you may have? If not, can you increase the sample to include at least one of each type of date/time you have.</description>
      <pubDate>Fri, 02 Aug 2019 14:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578725#M164178</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-02T14:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578737#M164182</link>
      <description>&lt;P&gt;If you are willing to work with actual time values instead of strings this does most of what you want. The "not reported" being an ambiguous time value end up as missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   infile datalines dsd dlm=',';
   informat str $20.;
   input str;
   if length(str)&amp;gt; 10 then do;
      dt = input(str,anydtdtm32.);
      time= timepart(dt);
   end;
   else time= input(str,time8.);
   format time time8.;
datalines;
"14:08"             
"8:56:00"           
"7:14:00 AM"        
"1/23/2018 11:14"   
"2/8/2018 18:47"    
"10:00am"           
"1350"              
"01-06-2008 09:35"  
"10:00am"           
"1350"              
"01-06-2008 09:35"
;&lt;/PRE&gt;
&lt;P&gt;the key part is in the if /then/else. You didn't provide actual data so I dummied up your values into something to manipulate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Time values I suspect in the long run will be easier to work with in the long run as you can use the time functions to pull out things like hour or minute or intervals with the INTCK function or increment with INTNX. The format I assigned, TIME8. will show time using a 24 hour clock. If you want to see AM/PM then you could use the TIMEAMPM format. Or even create a custom appearance using the Proc Format Picture statement.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 15:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578737#M164182</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-02T15:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578770#M164201</link>
      <description>&lt;P&gt;With the exception where one or all 3 fields have missing values, the above is a representative sample of the combinations found in the dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 16:26:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578770#M164201</guid>
      <dc:creator>eap</dc:creator>
      <dc:date>2019-08-02T16:26:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578778#M164208</link>
      <description>&lt;P&gt;You probably need to use different INFORMAT based on how the data looks. Here is method that uses / or - to indicate that date is included. Then checks whether : is included to check for whether to use HHMMSS informat or not.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards dsd truncover dlm='|';
  input ProcedureStarttime :$32. ProcedureEndtime :$32. Time_interval :$32. ;
cards;
14:08 | 15:31 | 1:23
8:56:00 | 10:40:00 | 1:44:00
7:14:00 AM | 8:14:00 AM | 1:00:00
1/23/2018 11:14 | 1/23/2018 12:03 | 12:49:00 AM
2/8/2018 18:47 | 2/8/2018 19:50 | 1:03
10:00am | 12:00pm | 120
1350 | 1532 | Not Reported
01-06-2008 09:35 | 01-06-2008 11:10 | 1:35
 | |
10:00am | 12:00pm | 120
1350 | 1532 | Not Reported
01-06-2008 09:35 | 01-06-2008 11:10 | 1:35
;
data want ;
  set have;
  length want $32 ;
  if cmiss(ProcedureStarttime,ProcedureEndtime) then want=' ';
  else if indexc(cats(ProcedureStarttime,ProcedureEndtime),'/-') then do;
    time_diff = input(ProcedureEndtime,anydtdtm32.)-input(ProcedureStarttime,anydtdtm32.);
  end;
  else if indexc(cats(ProcedureStarttime,ProcedureEndtime),':') then do;
    time_diff = input(ProcedureEndtime,anydttme32.)-input(ProcedureStarttime,anydttme32.);
  end;
  else do;
    time_diff = input(ProcedureEndtime,hhmmss.)-input(ProcedureStarttime,hhmmss.);
  end;
  if not missing(time_diff) then want=put(time_diff,hhmm10.);
  drop time_diff;
run;

proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       Procedure                               Time_
Obs    Starttime           ProcedureEndtime    interval        want

  1    14:08               15:31               1:23            1:23
  2    8:56:00             10:40:00            1:44:00         1:44
  3    7:14:00 AM          8:14:00 AM          1:00:00         1:00
  4    1/23/2018 11:14     1/23/2018 12:03     12:49:00 AM     0:49
  5    2/8/2018 18:47      2/8/2018 19:50      1:03            1:03
  6    10:00am             12:00pm             120             2:00
  7    1350                1532                Not Reported    1:42
  8    01-06-2008 09:35    01-06-2008 11:10    1:35            1:35
  9
 10    10:00am             12:00pm             120             2:00
 11    1350                1532                Not Reported    1:42
 12    01-06-2008 09:35    01-06-2008 11:10    1:35            1:35
&lt;/PRE&gt;</description>
      <pubDate>Fri, 02 Aug 2019 16:54:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578778#M164208</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-02T16:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578780#M164210</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/105081"&gt;@eap&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;With the exception where one or all 3 fields have missing values, the above is a representative sample of the combinations found in the dataset.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is this a response to something? Can not tell what it relates to.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 16:58:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578780#M164210</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-08-02T16:58:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to Clean Data Fields with Multiple Time Strings to Calculate Time Interval</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578807#M164226</link>
      <description>&lt;P&gt;This works perfectly! Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 19:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Clean-Data-Fields-with-Multiple-Time-Strings-to-Calculate/m-p/578807#M164226</guid>
      <dc:creator>eap</dc:creator>
      <dc:date>2019-08-02T19:29:04Z</dc:date>
    </item>
  </channel>
</rss>

