<?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 Create Informat to Derive Time Interval In Seconds in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59056#M16611</link>
    <description>Hi there.  This thread is further to the previous thread "Create Format to Display Time Interval" where I was told how to use cntlin to create a custom format to format a numeric field holding seconds as hours/minutes/seconds.&lt;BR /&gt;
&lt;BR /&gt;
Can I do a similar thing to create an informat to take in the original time interval value in text from my input dataset and convert it to a numeric field holding seconds? I'm not clear how much of the Proc Format instructions apply to creating informats. What I need to do is create an informat to take in time interval values such as 1 hrs 0 mins 46 secs from a text field (or raw text file) and store them in seconds e.g. 3646 in a numeric field should be the result in this case.&lt;BR /&gt;
&lt;BR /&gt;
The code I use is below.  It takes the 'timespent' field and creates a timespent_seconds field from it.  Probably handier ways to code it.  But my questions is: Can this be transformation be done in a data step to create a fmtname and a permanent informat, in a similar way as the format was created in the thread: "Create Format to Display Time Interval" as advised by Geniz, solution 3. It would be ideal to hold all this code in an informat for re-use.  Then I could do these kind of transformations in 1 or 2 lines for future datasets holding time intervals in this kind of format.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
*3. Convert Time Interval field;&lt;BR /&gt;
*******************************;&lt;BR /&gt;
&lt;BR /&gt;
*3.1 Set up the Reg Expressions to be used for matching;&lt;BR /&gt;
***************************************************;&lt;BR /&gt;
*Put the prx setup in an if obs = 1 statement becaues doesn't need to keep being re-created..;&lt;BR /&gt;
*Actually if don't do this prxparse runs millions of times and creates memory errors;&lt;BR /&gt;
if _n_ = 1 then do;&lt;BR /&gt;
	reNum = "/\d+/";&lt;BR /&gt;
	reCNum = PRXPARSE(reNum);&lt;BR /&gt;
	retain reCNum;&lt;BR /&gt;
&lt;BR /&gt;
	reMins = "/\d+\s*mins/"; *builds the regular expression or search pattern. Will be&lt;BR /&gt;
*looking for text containing char or varcher;&lt;BR /&gt;
	reCMins = PRXPARSE(reMins ); *compiles the expression for SAS, so called reText now;&lt;BR /&gt;
	retain reCMins;&lt;BR /&gt;
&lt;BR /&gt;
	reHours = "/\d+\s*hrs/";&lt;BR /&gt;
	reCHours = prxparse(reHours);&lt;BR /&gt;
	retain reCHours;&lt;BR /&gt;
&lt;BR /&gt;
	reSeconds = "/\d+\s*secs/";&lt;BR /&gt;
	reCSeconds = prxparse(reSeconds);&lt;BR /&gt;
	retain reCSeconds;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
*3.2 Extract the hours value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCMins " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCHours, timespent) then do;	&lt;BR /&gt;
	call prxposn(reCHours, 0, pos, len);&lt;BR /&gt;
	Hours_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, Hours_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like Hours or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		Hours_num = substr(Hours_text, pos, len);		&lt;BR /&gt;
	end;	&lt;BR /&gt;
end;&lt;BR /&gt;
if Hours_num = . then Hours_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.3 Extract the minutes value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCMins " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCMins, timespent) then do;&lt;BR /&gt;
	*put "Got a match on minutes for obs no: " _n_;&lt;BR /&gt;
	call prxposn(reCMins, 0, pos, len);&lt;BR /&gt;
	mins_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, mins_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like mins or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		mins_num = substr(mins_text, pos, len);&lt;BR /&gt;
		if mins_num = . then mins_num = 0;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if mins_num = . then mins_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.4 Extract the seconds value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCSeconds " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCSeconds, timespent) then do;&lt;BR /&gt;
	*put "Got a match on minutes for obs no: " _n_;&lt;BR /&gt;
	call prxposn(reCSeconds, 0, pos, len);&lt;BR /&gt;
	Seconds_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, Seconds_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like Seconds or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		Seconds_num = substr(Seconds_text, pos, len);		&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if seconds_num = . then Seconds_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.5 Add the values to get number of seconds in the field;&lt;BR /&gt;
timespent_seconds = seconds_num + (mins_num * 60) + (hours_num * 60 * 60);</description>
    <pubDate>Fri, 31 Oct 2008 16:04:55 GMT</pubDate>
    <dc:creator>IrishGuy</dc:creator>
    <dc:date>2008-10-31T16:04:55Z</dc:date>
    <item>
      <title>Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59056#M16611</link>
      <description>Hi there.  This thread is further to the previous thread "Create Format to Display Time Interval" where I was told how to use cntlin to create a custom format to format a numeric field holding seconds as hours/minutes/seconds.&lt;BR /&gt;
&lt;BR /&gt;
Can I do a similar thing to create an informat to take in the original time interval value in text from my input dataset and convert it to a numeric field holding seconds? I'm not clear how much of the Proc Format instructions apply to creating informats. What I need to do is create an informat to take in time interval values such as 1 hrs 0 mins 46 secs from a text field (or raw text file) and store them in seconds e.g. 3646 in a numeric field should be the result in this case.&lt;BR /&gt;
&lt;BR /&gt;
The code I use is below.  It takes the 'timespent' field and creates a timespent_seconds field from it.  Probably handier ways to code it.  But my questions is: Can this be transformation be done in a data step to create a fmtname and a permanent informat, in a similar way as the format was created in the thread: "Create Format to Display Time Interval" as advised by Geniz, solution 3. It would be ideal to hold all this code in an informat for re-use.  Then I could do these kind of transformations in 1 or 2 lines for future datasets holding time intervals in this kind of format.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
*3. Convert Time Interval field;&lt;BR /&gt;
*******************************;&lt;BR /&gt;
&lt;BR /&gt;
*3.1 Set up the Reg Expressions to be used for matching;&lt;BR /&gt;
***************************************************;&lt;BR /&gt;
*Put the prx setup in an if obs = 1 statement becaues doesn't need to keep being re-created..;&lt;BR /&gt;
*Actually if don't do this prxparse runs millions of times and creates memory errors;&lt;BR /&gt;
if _n_ = 1 then do;&lt;BR /&gt;
	reNum = "/\d+/";&lt;BR /&gt;
	reCNum = PRXPARSE(reNum);&lt;BR /&gt;
	retain reCNum;&lt;BR /&gt;
&lt;BR /&gt;
	reMins = "/\d+\s*mins/"; *builds the regular expression or search pattern. Will be&lt;BR /&gt;
*looking for text containing char or varcher;&lt;BR /&gt;
	reCMins = PRXPARSE(reMins ); *compiles the expression for SAS, so called reText now;&lt;BR /&gt;
	retain reCMins;&lt;BR /&gt;
&lt;BR /&gt;
	reHours = "/\d+\s*hrs/";&lt;BR /&gt;
	reCHours = prxparse(reHours);&lt;BR /&gt;
	retain reCHours;&lt;BR /&gt;
&lt;BR /&gt;
	reSeconds = "/\d+\s*secs/";&lt;BR /&gt;
	reCSeconds = prxparse(reSeconds);&lt;BR /&gt;
	retain reCSeconds;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
*3.2 Extract the hours value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCMins " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCHours, timespent) then do;	&lt;BR /&gt;
	call prxposn(reCHours, 0, pos, len);&lt;BR /&gt;
	Hours_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, Hours_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like Hours or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		Hours_num = substr(Hours_text, pos, len);		&lt;BR /&gt;
	end;	&lt;BR /&gt;
end;&lt;BR /&gt;
if Hours_num = . then Hours_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.3 Extract the minutes value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCMins " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCMins, timespent) then do;&lt;BR /&gt;
	*put "Got a match on minutes for obs no: " _n_;&lt;BR /&gt;
	call prxposn(reCMins, 0, pos, len);&lt;BR /&gt;
	mins_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, mins_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like mins or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		mins_num = substr(mins_text, pos, len);&lt;BR /&gt;
		if mins_num = . then mins_num = 0;&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if mins_num = . then mins_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.4 Extract the seconds value;&lt;BR /&gt;
******************************;&lt;BR /&gt;
*put "************Matching reg expression" reCSeconds " against field TimeSpent";&lt;BR /&gt;
If PRXMATCH(reCSeconds, timespent) then do;&lt;BR /&gt;
	*put "Got a match on minutes for obs no: " _n_;&lt;BR /&gt;
	call prxposn(reCSeconds, 0, pos, len);&lt;BR /&gt;
	Seconds_text = substr(timespent, pos, len);&lt;BR /&gt;
	if prxmatch(reCNum, Seconds_text) then do;&lt;BR /&gt;
		*Extracting just the numbers i.e. getting rid of the text characters like Seconds or space;&lt;BR /&gt;
		call prxposn(reCNum, 0, pos, len);&lt;BR /&gt;
		Seconds_num = substr(Seconds_text, pos, len);		&lt;BR /&gt;
	end;&lt;BR /&gt;
end;&lt;BR /&gt;
if seconds_num = . then Seconds_num = 0;&lt;BR /&gt;
&lt;BR /&gt;
*3.5 Add the values to get number of seconds in the field;&lt;BR /&gt;
timespent_seconds = seconds_num + (mins_num * 60) + (hours_num * 60 * 60);</description>
      <pubDate>Fri, 31 Oct 2008 16:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59056#M16611</guid>
      <dc:creator>IrishGuy</dc:creator>
      <dc:date>2008-10-31T16:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59057#M16612</link>
      <description>Are you trying to produce  ISO 8601 date time intervals similar to the following...&lt;BR /&gt;
&lt;BR /&gt;
P2Y3M4DT5H6M7S&lt;BR /&gt;
&lt;BR /&gt;
You did not mention the version of SAS you are using but there are new 9.2 FUNCTIONS, FORMATS and INFORMATS related to this.&lt;BR /&gt;
&lt;BR /&gt;
Start by looking at CALL IS8601_CONVERT Routine.&lt;BR /&gt;
&lt;BR /&gt;
I don't have access to 9.2 so I have not bee able to try these function.</description>
      <pubDate>Fri, 31 Oct 2008 17:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59057#M16612</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2008-10-31T17:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59058#M16613</link>
      <description>Data_Null,&lt;BR /&gt;
I am using SAS 9.1 so don't have access to that functionality and will need to create my own informat</description>
      <pubDate>Mon, 03 Nov 2008 09:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59058#M16613</guid>
      <dc:creator>IrishGuy</dc:creator>
      <dc:date>2008-11-03T09:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59059#M16614</link>
      <description>Anyone have any thoughts on how to create the informat using the cntlin option? If not I will abandon thread!</description>
      <pubDate>Mon, 10 Nov 2008 10:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59059#M16614</guid>
      <dc:creator>IrishGuy</dc:creator>
      <dc:date>2008-11-10T10:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59060#M16615</link>
      <description>Suggest you create an INFORMAT using the PROC FORMAT INVALUE option, and then use CNTLOUT= to unload the INFORMAT  to a SAS dataset for review and dissection.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
SAS DOC on PROC FORMAT INVALUE:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/proc/59565/HTML/default/a002473466.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/proc/59565/HTML/default/a002473466.htm&lt;/A&gt;</description>
      <pubDate>Mon, 10 Nov 2008 14:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59060#M16615</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-11-10T14:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59061#M16616</link>
      <description>Thanks SBB.&lt;BR /&gt;
Thats probably the right track but I can't easily see how that will get over the issue. The cntlin option allowed for very complex transformations as you can use any data step code and functions to make the transformation from one value to another.  But if I use invalue = I seem to be tied to making the transformation within proc format so it looks to be more limited.  It may well work fine but am not very experienced in formats etc.  Willl hack around with it.   If anyone has more detail, great.</description>
      <pubDate>Tue, 11 Nov 2008 12:30:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59061#M16616</guid>
      <dc:creator>IrishGuy</dc:creator>
      <dc:date>2008-11-11T12:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59062#M16617</link>
      <description>Just as the PUT function and a format can be used to create a new character variable, the INPUT function works with an INFORMAT to create a numeric variable from a character string. The issue that I see is that you may have to break your character string into chunks in order to reassemble them into something that will generate the right time value for you....which gets you back to data step.&lt;BR /&gt;
 &lt;BR /&gt;
A closer look at some of the examples in the doc and searching for prior SUGI and Global Forum papers on the topic should help to show how INPUT/INFORMAT might work.&lt;BR /&gt;
&lt;BR /&gt;
cynthia</description>
      <pubDate>Tue, 11 Nov 2008 12:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59062#M16617</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-11-11T12:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: Create Informat to Derive Time Interval In Seconds</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59063#M16618</link>
      <description>Thanks, I will research further along those lines.</description>
      <pubDate>Tue, 11 Nov 2008 12:53:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Create-Informat-to-Derive-Time-Interval-In-Seconds/m-p/59063#M16618</guid>
      <dc:creator>IrishGuy</dc:creator>
      <dc:date>2008-11-11T12:53:27Z</dc:date>
    </item>
  </channel>
</rss>

