Search code examples
timesasinformat

Formatting HH:MM column when reading data into SAS


I am an R user and SAS beginner trying to read a csv file into SAS. The problem I am facing is with a column called "TIME" which contains time data in the format "hh:mm " for e.g. "12:23 PM". In R it is as simple as as.POSIXct(df$TIME, format = "%I:%M %p") and my hour is converted to time values instantly (with time-zone and today's date which can be removed).

This is how I tried to implement this in SAS:

/* firstly `rename` "TIME" to "DAY_HOUR" */
data mid.prac1;
set mid.prac1;
rename time = DAY_HOUR;
run;
/*runs successfully */

/* remove unwanted characters from DAY_HOUR */
data mid.prac2;
set mid.prac1;
DAY_HOUR = compress(DAY_HOUR, 'PMAM');
proc print; run;
/* runs successfully */

/* format hh:mm as time */
data mid.prac3;
set mid.prac2;  
informat DAY_HOUR time10.;
run;
**/*Error: ERROR 48-59: The informat $TIME was not found or could not be loaded.*/**

The time informat does not exist? I read this documentation on the SAS website: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201204.htm and I think I am doing it right. I am new to the whole SAS universe so apologies if I am not following conventions in asking questions and pasting sample data etc.


Solution

  • Just parse it through the input function:

    DAY_HOUR_FOR_ME = INPUT(DAY_HOUR,TIME10.);
    

    This should convert the DAY_HOUR character variable to a DAY_HOUR_FOR_ME numeric variable. If you need to format this as a time, use the FORMAT statement:

    FORMAT DAY_HOUR_FOR_ME time10.;
    

    OK...

    FORMAT DAY_HOUR_FOR_ME timeampm11.;
    

    Informats

    Informats are used by SAS to read data. So, given a string looking like this:

    09:00:00
    

    You can ask SAS to convert this to a numeric representation of 9AM, by submitting:

    time_i_want = input(text,time8.);
    

    Where 8 is simply the length of the string text. SAS will then store a variable time_i_want as the number of seconds between 00:00 and 09:00.

    Formats

    Formats are used to display data in a usable format. If I asked you the time and you told me how many seconds there had been since the 1st January 1960, I'd be displeased. However, this is how SAS stores DATETIME values:

    '01JAN1960:00:01:00'dt = 60;
    '31DEC1959:23:59:30'dt = -30;
    

    etc.

    So, the FORMAT statement can be used in any datastep to force the resulting dataset (that which is named in the DATA statement) to display values in a particular format.

    TIMEAMPM

    is one such format. However, TIMEAMPM is not an INFORMAT

    Informats and formats

    Some informats are mirrored by equivalent formats. TIME8. is an example. You can ask SAS to read in a time value using an informat:

    01:00:32

    This is then stored as a numeric value 3632. It can then be formatted using the TIME8. format, which displays it as such:

    01:00:32