Formatting the HH: MM column when reading data into SAS
I am a user R
and SAS
beginner trying to read a csv file in SAS
. The problem I am facing is a column called "TIME" which contains time data in "hh: mm" format for example. "12:23 pm". It R
's as easy as as.POSIXct(df$TIME, format = "%I:%M %p")
that and my hour is instantly converted to time values ββ(with timezone and today's date can be removed).
This is how I tried to implement it 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.*/**
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'm new to the entire SAS universe, so I'm sorry if I don't follow conventions in asking questions and pasting data samples, etc.
source to share
Just parse it through the input function:
DAY_HOUR_FOR_ME = INPUT(DAY_HOUR,TIME10.);
This should convert the symbol variable DAY_HOUR to the numeric variable DAY_HOUR_FOR_ME. If you need to format this as a time, use the operator FORMAT
:
FORMAT DAY_HOUR_FOR_ME time10.;
OK ...
FORMAT DAY_HOUR_FOR_ME timeampm11.;
Informats
The information is used by SAS to read the data. So, given a line that looks like this:
09:00:00
You can ask SAS to convert this to a 9AM numeric representation by sending:
time_i_want = input(text,time8.);
Where 8 is just the length of the string text
. SAS will then store the variable time_i_want
as a number of seconds between 00:00 and 09:00.
Formats
Formats are used to display data in an easy-to-use format. If I asked you about the time and you told me how many seconds have been since January 1, 1960, I would be unhappy. However, SAS preserves the DATETIME values:
'01JAN1960:00:01:00'dt = 60;
'31DEC1959:23:59:30'dt = -30;
and etc.
So, an operator FORMAT
can be used in any datastep to force the resulting dataset (the one specified in the instruction DATA
) to display values ββin a specific format.
TIMEAMPM
is one such format. However TIMEAMPM is not INFORMAT
Information and formats
Some information data is mirrored by equivalent formats. TIME8.
- example. You can ask SAS to read the time value using informat :
1:00:32
It is then stored as a numeric value 3632
. It can then be formatted using the formatTIME8.
which displays it as such:
1:00:32
source to share