DATA Step, Macro, Functions and more

problems with conversion time hhmm5.

Reply
Contributor
Posts: 24

problems with conversion time hhmm5.

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.

Super User
Posts: 10,500

Re: problems with conversion time hhmm5.

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.

Contributor
Posts: 24

Re: problems with conversion time hhmm5.

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

 

 

Super User
Posts: 10,500

Re: problems with conversion time hhmm5.

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.

Ask a Question
Discussion stats
  • 3 replies
  • 224 views
  • 0 likes
  • 2 in conversation