Search code examples
sasdatastep

How to read in a time mm:ss.ss format into sas?


I am having trouble reading in one particular column of data. The raw text file column I am having trouble with looks like this:

55.49
1:23.57
32.22
5:38.43
12:52.17
25.13

The form is minute minute : second second ms ms. this particular data does not go into the hours. I tried formatting the variable as TIME6.2; but I am getting missing values for over 70% of the data. I also messed around with adjusting the width. I also tried using the informat mmss but I did not have any success. However I may be using it incorrectly. As of now, I am reading the data in as a character value but I need it to be a numeric value so I can do data comparisons.

Any advice would be greatly appreciated!

Thanks!

EDIT: I wanted to make a note that these are elapsed times(race results). I hope to read the data in and then be able to compare the race times as to rank 1st,2nd,3rd place etc.


Solution

  • First thing you need to understand is the difference between informats and formats.

    Informat is a pattern that SAS uses to convert text into a number. The informat you want to use is hhmmss..

    The format is something that is applied to a number; it tells SAS how you want a particular number to be displayed or printed. In order to represent your times, you need a width greater than 6 to account for the : and . characters you want printed. Try something larger like time12.2 format. Total width of 12, with two spaces for the ms digits:

    EDIT: Assuming your text file is delimited, it seems the hhmmss informat doesn't like values that are not wide enough. It does work if you read the time in as character $ then use the input() function to convert the times to numbers.

    data yourdata;
      format finaltime time12.2;
      infile cards dlm=',';
      input age team finaltimec $ score;
      finaltime = input(finaltimec,?? hhmmss.);
      drop finaltimec;
      cards;
    20,1,55.49,1
    22,1,1:23.57,2
    34,1,NA,3
    19,2,32.22,4
    55,3,5:38.43,5
    17,3,12:52.17,6
    31,3,25.13,7
    ;
    run;