Help using Base SAS procedures

SAS Time format

Not applicable
Posts: 0

SAS Time format


I am working with SAS tables that currently have time values listed in character form (ex: 06:51:23, in hours minutes seconds). I need to convert this to SAS time (or any other SAS format) so as to make a new variable, say, before_3 which will be a dummy varaible taking value 1 if the time is before 4 p.m. and 0 otherwise.

any suggestions?

Kind Regards,
Posts: 9,366

Re: SAS Time format

Posted in reply to deleted_user
If you have a character variable and you want to convert it to a numeric value for time (number of seconds since midnight), then you must use the INPUT function to turn your character value into a numeric variable.

For example, let's say that you have a variable called CHARTIME whose value is "06:51:23" and you want to convert that number to a SAS time value. Then your INPUT function would look something like this:
sastime = input(chartime,time8.);

The INPUT function, converts a CHARACTER variable to a NUMERIC variable, using the INFORMAT that you specify as the second argument to the function. Now that SASTIME is internally stored as a number of seconds since midnight, you can now do a comparison. Assuming you mean you want to set a flag based on the time being before 3:00 PM (15:00:00), then your IF statement might look like this:
if sastime le "15:00:00"t then newvar = 1;
else newvar = 0;

The documentation on SAS date and date/time and time variables is quite good and there are a lot of good examples of using the INPUT function in the documentation. The specification "15:00:00"t in the IF statement is called a "time constant" and, again, looking in the documentation on the use of SAS date, time and datetime constants will help you understand more about the task you want/need to do.

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation