BookmarkSubscribeRSS Feed
deleted_user
Not applicable
hello,

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,
Mark
1 REPLY 1
Cynthia_sas
Diamond | Level 26
Hi:
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:
[pre]
sastime = input(chartime,time8.);
[/pre]

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:
[pre]
if sastime le "15:00:00"t then newvar = 1;
else newvar = 0;
[/pre]

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.

cynthia

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1358 views
  • 0 likes
  • 2 in conversation