BookmarkSubscribeRSS Feed
stancemcgraw
Obsidian | Level 7

Hello,

 

   I am trying to convert a variable that is in the format of h:mm:ss to hours.  I am not sure how to proceed.  It was a file converted to CSV and read-into SAS.

3 REPLIES 3
ballardw
Super User

What format does SAS show the variable as having? If the format is currently hhmm5 then the value is an actual SAS time value and you can change the format for display.

But convert to what? There are a number of options. If you only want the hour displayed and no decimal portion then use HHMM2. or HOUR2.

 

In a procedure that would be :

Proc print data=have;

   var time;

    format time hour2.;

run;

 

Or you can change the format permanently in a variety of methods.

stancemcgraw
Obsidian | Level 7

So when I tried that proc print command, it says my arrtime variable is in a character format:

 

4804  Proc print ;

4805     var arrtime;

4806      format arrtime hour2.;

ERROR: You are trying to use the numeric format HOUR with the character variable arrtime in data set IAT.TIMETOIAT2.

4807  run;

 

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE PRINT used (Total process time):

      real time           0.03 seconds

      cpu time            0.01 seconds

 

 

ballardw
Super User

The you data did not have the format you stated, it had a $xx. format.

Proc Contents data= datasetname; run;

will tell the type and format of variables.

Note that FORMAT has a very specific meaning when using SAS. FORMAT refers to how a value is displayed.

Since you don't say how you read the data into SAS we can't address that but it is a very good idea to learn how to read dates and times into SAS date, time or datetime valued variables due to the number tools designed to work with them.

 

So the question could become: Are you trying to create a TIME valued variable, a character variable that has the hour from your chracter string of hh:mm:ss or create a numeric variable that has the number in the first position.

In a data step:

 

data want;

   set have;

   SAStimevar = input(arrtime,hhmmss.);

   Format SASTimeVar hhmmss.;

   CharacterHour = scan(arrtime,1,':');

   Numerichour = input(scan(arrtime,1,':'),best2.);

run;

 

Shows some ways to make one of each type of variable.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 1313 views
  • 0 likes
  • 2 in conversation