Read a character value that cannot be read with an existing SAS time informat or function to create a SAS time value.
Featured Step | PROC FORMAT and DATA step |
Featured Step Options and Statements | PICTURE format, INPUT and PUT functions |
Data set INCIDENTS has power supply information for five incidents. Character variables IN_NOTED and RESP_NOTED records the times each incident was noted and responded to. The time values are not stored in a way that can be read with a single time informat.
INCIDENTS Obs location type in_noted resp_noted 1 SW009A Surge 33.49 38.01 2 N0003Q Drop 9:13.69 12:15.02 3 ESE01C Transformer 8:00:00.33 10:21:06.43 4 NW503K Linkage 9:31:09. 10:18:18. 5 ESE01D Transformer 13:00:00.33 14:48:32.08
Output 9.12 INCIDENT_TIMES Data SetExample 9.12 INCIDENT_TIMES Obs location type in_noted resp_noted time_in time_resp 1 SW009A Surge 33.49 38.01 0:00:33.49 0:00:38.01 2 N0003Q Drop 9:13.69 12:15.02 0:09:13.69 0:12:15.02 3 ESE01C Transformer 8:00:00.33 10:21:06.43 8:00:00.33 10:21:06.43 4 NW503K Linkage 9:31:09. 10:18:18. 9:31:09.00 10:18:18.00 5 ESE01D Transformer 13:00:00.33 14:48:32.08 13:00:00.33 14:48:32.08 |
This example shows how to create a SAS time value from a character value. The character values in variables IN_NOTED and RESP_NOTED in input data set INCIDENTS cannot be read with an existing SAS time informat or function such as the TIMEn. informat or the HMS function.
The program uses a picture format that is defined by PROC FORMAT to structure the value in a form that the SAS informat TIMEn. can interpret as a time value. A series of assignment statements that contain INPUT and PUT functions transform the character value into a numeric value and then into a SAS time value.
Define picture format TIMECHAR.
Create data set INCIDENT_TIMES. Read the observations in INCIDENTS.
Assign the TIMEw.d format to the two new variables that will hold the two time values. Define array NOTED to hold the input time values. Define array TIMES to hold the results of converting the input values to SAS time values. Execute an iterative DO loop the number of times equal to the number of values to convert to SAS time values. Convert the elements in NOTED to SAS time values. Remove the colons. Create a character value that is saved in a way that can be read by the TIMEn. function. Convert the value in TEMP1 to numeric by reading it with the INPUT function and the numeric informat 11.2. Next convert this numeric value that is returned by the INPUT function back to character by using the PUT function and the picture format TIMECHAR, which writes out the value in a way that can be read by the TIMEn. function. Assign a valid SAS time value to the corresponding element in TIMES by reading the value of intermediate variable TEMP2 with the TIME11. informat.
proc format; picture timechar other='99:99:99.99'; run; data incident_times; set incidents; drop temp1 temp2 i; format time_in time_resp time11.2; array noted{2} $ in_noted resp_noted; array times{2} time_in time_resp; do i=1 to dim(noted); temp1=compress(noted{i},':'), temp2=put(input(temp1,11.2),timechar.); times{i}=input(temp2,time11.); end; run;
18.188.198.94