Reputation: 23
I am trying to split a date time variable into date and time.
The variable VitalSignTakenDateTime
looks like:
09JUN2010:08:05:00.000
Normally I use the code
data ex2;
set ex1;
specdate=scan(VitalSignTakenDateTime, 1, "");
spectime=scan(VitalSignTakenDateTime, 2, "");
run;
But that is not going to work for this since they are separated by a colon instead of a space. Does anyone know of a solution.
Upvotes: 1
Views: 171
Reputation: 63424
While I largely agree with using INPUT to get this most efficiently, or SUBSTR since the position is likely fixed, the most comparable result to your code is to use CALL SCAN
.
CALL SCAN
is like SCAN
except it returns the position and length of the string (meaning, the position of the first character of the word plus the length of that word), meaning you can then use SUBSTR
to get the word and the parts after (or before) the word.
As an example:
data ex2;
input
@1 VitalSignTakenDateTime $18.;
call scan(VitalSignTakenDateTime,1,pos,len,':');
specdate=substr(VitalSignTakenDateTime,1,len);
spectime=substr(VitalSignTakenDateTime,len+2);
put _all_;
datalines;
09JUN2010:08:05:00.000
;
run;
Upvotes: 1
Reputation: 21264
If it's a string and fixed length you can always use SUBSTR, but using input to convert the value to a datetime and then DATEPART and TIMEPART are the best options.
Upvotes: 0
Reputation: 4554
Use datepart and timepart:
data _null_;
dt='09JUN2010:08:05:00.000'dt;
date=datepart(dt);
time=timepart(dt);
put date=date9. time=time10.;
run;
Upvotes: 2