LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (January 2002, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 11 Jan 2002 14:04:30 -0500
Reply-To:     "Chakravarthy, Venky" <Venky.Chakravarthy@PFIZER.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "Chakravarthy, Venky" <Venky.Chakravarthy@PFIZER.COM>
Subject:      Re: Parsing a Date/Time Text Field
Comments: To: "Kitzmann, Daniel J." <kitzmann.daniel@MAYO.EDU>
Comments: cc: "David L. Ward" <dward@SASHELP.COM>
Content-Type: text/plain; charset="iso-8859-1"

Daniel,

In version 8.2, the DATETIME INFORMAT is capable of handling AM/PM.

113 data _null_; 114 x='May 21 2001 12:20:31:123AM' ; 115 y = input(substr(x,5,2)||substr(x,1,3)||substr(x,8,4)||"/"|| 116 substr(x,13,8)||"."||substr(x,22,3)||" "|| 117 substr(x,25,2),datetime40.) ; 118 date = datepart(y) ; 119 time = timepart(y) ; 120 put y= date= time= ; 121 run;

Y=1306023631.1 DATE=15116 TIME=1231.1229999

Kind Regards,

Venky #****************************************# # E-mail: venky.chakravarthy@pfizer.com # # swovcc@hotmail.com # # Phone: (734) 622-1963 # #****************************************#

-----Original Message----- From: Kitzmann, Daniel J. [mailto:kitzmann.daniel@MAYO.EDU] Sent: Friday, January 11, 2002 11:49 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Parsing a Date/Time Text Field

Dear Listers:

Suppose I have a dataset containing vars d1-d(n), where each var is a date/time field stored as a text expression in the somewhat peculiar form:

Obs d1 d2 1 May 21 2001 12:00:00:000AM May 18 2001 11:01:24:126AM 2 May 21 2001 12:00:00:000AM May 18 2001 11:23:57:870AM 3 May 21 2001 12:00:00:000AM May 18 2001 4:29:14:216PM 4 May 21 2001 12:00:00:000AM May 11 2001 8:01:30:010AM 5 May 21 2001 12:00:00:000AM May 11 2001 8:14:01:020AM

Suppose, too, that for each var d(i), I want to parse the text string and create two separate vars, u(i) and t(i), where u(i) is a SAS date var and t(i) is a SAS time var. The following code achieves same:

265 data a; 266 set; 267 array d{2}; 268 array e{2} $ 14; 269 array t{2}; 270 array u{2}; 271 do i = 1 to dim(d); 272 %let x = scan(d{i}, ; 273 e{i} = reverse(trim(&x 4))); 274 substr(e{i}, 3, 1) = ' '; ** sig. digit precision lost; 275 substr(e{i}, 6, 1) = '.'; ** at .000 s is trivial; 276 t{i} = input(reverse(e{i}), time14.); 277 u{i} = input((&x 2) || &x 1) || &x 3)), date9.); 278 end; 279 run; NOTE: There were 5 observations read from the data set WORK.A. NOTE: The data set WORK.A has 5 observations and 9 variables.

Obs d1 d2 e1 1 May 21 2001 12:00:00:000AM May 18 2001 11:01:24:126AM MA 00.00:00:21 2 May 21 2001 12:00:00:000AM May 18 2001 11:23:57:870AM MA 00.00:00:21 3 May 21 2001 12:00:00:000AM May 18 2001 4:29:14:216PM MA 00.00:00:21 4 May 21 2001 12:00:00:000AM May 11 2001 8:01:30:010AM MA 00.00:00:21 5 May 21 2001 12:00:00:000AM May 11 2001 8:14:01:020AM MA 00.00:00:21

Obs e2 t1 t2 u1 u2 i 1 MA 21.42:10:11 0 39684.12 15116 15113 3 2 MA 78.75:32:11 0 41037.87 15116 15113 3 3 MP 12.41:92:4 0 59354.21 15116 15113 3 4 MA 10.03:10:8 0 28890.01 15116 15106 3 5 MA 20.10:41:8 0 29641.02 15116 15106 3

My question is whether the foregoing can be done in a more efficient or transparent way. I puzzled over trying to do the entire conversion in a single pair of input functions (one each for date and time), with the concomitant parsing functions nested inside as the input argument (as is done with the u(i) var). But, as shown, I could not think of a way to do it in a single line with the t(i) var.

Thank you kindly in advance for any solutions.

Cordially, Daniel Kitzmann kitzd@bigfoot.com


Back to: Top of message | Previous page | Main SAS-L page