Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Converting String Time variable

    Dear All,

    Please help me to convert the following time variable from string to numeric.

    Thanks

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str15 start
    "22:00:00.000+03"
    "21:35:00.000+03"
    "12:32:00.000+03"
    "10:39:00.000+03"
    "22:02:00.000+03"
    "14:00:00.000+03"
    "11:00:00.000+03"
    "09:30:00.000+03"
    "11:10:00.000+03"
    "16:35:00.000+03"
    end

  • #2
    Starting with your sample data, here is some technique that may point the way.

    Code:
    . generate time = clock(start,"hms#")
    
    . format time %tc
    
    . clonevar time2 = time
    
    . format time2 %tcHH:MM
    
    . list, clean noobs
    
                  start                 time   time2  
        22:00:00.000+03   01jan1960 22:00:00   22:00  
        21:35:00.000+03   01jan1960 21:35:00   21:35  
        12:32:00.000+03   01jan1960 12:32:00   12:32  
        10:39:00.000+03   01jan1960 10:39:00   10:39  
        22:02:00.000+03   01jan1960 22:02:00   22:02  
        14:00:00.000+03   01jan1960 14:00:00   14:00  
        11:00:00.000+03   01jan1960 11:00:00   11:00  
        09:30:00.000+03   01jan1960 09:30:00   09:30  
        11:10:00.000+03   01jan1960 11:10:00   11:10  
        16:35:00.000+03   01jan1960 16:35:00   16:35
    Last edited by William Lisowski; 03 May 2016, 17:44.

    Comment


    • #3
      Thanks a lot William... It worked though dropped time variable and maintained time2 variable which is important to me.

      Comment


      • #4
        It is important to realise that the variables have the same values, just different display formats. Also, in general, that as the values are just multiples of 60 000 milliseconds and float is adequate, in general anything that comes out of a clock() function should be put in a double.

        As in your recent thread (just 1 May 2016) http://www.statalist.org/forums/foru...-destring-time the strong recommendation is to study

        Code:
        help dates
        where all this is documented.

        Comment


        • #5
          Thanks Nick. I will study and a lot of practice on the same

          Comment


          • #6
            Here is a rewriting of my first paragraph (too late to edit the original):

            It is important to realise that the variables have the same values, just different display formats.

            In this example the values are just multiples of 60 000 milliseconds and less than 1 day's worth, so float is adequate as a storage type. However, in general anything that comes out of a clock()function and goes into a variable should be put in a double.

            Comment

            Working...
            X