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
SAS Super FREQ
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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