Announcement

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

  • Converting Minutes to Seconds

    Dear all,

    I would like to convert the following variable measured in minutes to seconds.
    I have gone through timedate help, but I fail to get something direct.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double time
                     1
                  1.03
                  1.25
                  1.48
                  2.08
                   2.1
                  2.13
                  2.13
                  2.15
                  2.16
                  2.16
                  2.17
                  2.18
                  2.19
                  2.19
                  2.24
                  2.25
    2.2600000000000002
                  2.29
    2.3000000000000003
    2.3000000000000003
                  2.31
                  2.32
                  2.32
                  2.32
                  2.33
                  2.34
                  2.34
                  2.34
                  2.35
                  2.35
                  2.35
                  2.38
                  2.39
                  2.39
                  2.41
                  2.42
                  2.43
                  2.43
                  2.43
                  2.43
                  2.44
                  2.45
                  2.46
                  2.47
                  2.47
                  2.47
                  2.48
    2.5100000000000002
                  2.52
    2.5500000000000003
                  2.56
                  2.58
                  2.58
                  2.59
                  2.59
                  2.59
                     3
                     3
    3.0300000000000002
    3.0300000000000002
    3.0300000000000002
    3.0500000000000003
    3.0500000000000003
                  3.06
    3.0700000000000003
                  3.08
                  3.09
                  3.09
                  3.09
                  3.11
                  3.11
                  3.11
                  3.11
                  3.12
                  3.12
                  3.12
                  3.12
                  3.12
                  3.12
                  3.12
                  3.13
                  3.13
                  3.13
                  3.13
                  3.13
                  3.14
                  3.14
                  3.14
                  3.15
                  3.15
                  3.15
                  3.15
                  3.16
                  3.16
                  3.16
                  3.16
                  3.17
                  3.17
                  3.17
    end

  • #2
    I'd just work on the minutes and seconds parts separately.

    Code:
    . clear
    
    . input double time
    
               time
      1.                  1
      2.               1.03
      3.               1.25
      4.               1.48
      5.               2.08
      6.                2.1
      7.               2.13
      8.               2.13
      9.               2.15
     10.               2.16
     11.               2.16
     12.               2.17
     13.               2.18
     14.               2.19
     15.               2.19
     16.               2.24
     17.               2.25
     18. 2.2600000000000002
     19.               2.29
     20. 2.3000000000000003
     21. 2.3000000000000003
     22.               2.31
     23.               2.32
     24.               2.32
     25.               2.32
     26.               2.33
     27.               2.34
     28.               2.34
     29.               2.34
     30.               2.35
     31.               2.35
     32.               2.35
     33.               2.38
     34.               2.39
     35.               2.39
     36.               2.41
     37.               2.42
     38.               2.43
     39.               2.43
     40.               2.43
     41.               2.43
     42.               2.44
     43.               2.45
     44.               2.46
     45.               2.47
     46.               2.47
     47.               2.47
     48.               2.48
     49. 2.5100000000000002
     50.               2.52
     51. 2.5500000000000003
     52.               2.56
     53.               2.58
     54.               2.58
     55.               2.59
     56.               2.59
     57.               2.59
     58.                  3
     59.                  3
     60. 3.0300000000000002
     61. 3.0300000000000002
     62. 3.0300000000000002
     63. 3.0500000000000003
     64. 3.0500000000000003
     65.               3.06
     66. 3.0700000000000003
     67.               3.08
     68.               3.09
     69.               3.09
     70.               3.09
     71.               3.11
     72.               3.11
     73.               3.11
     74.               3.11
     75.               3.12
     76.               3.12
     77.               3.12
     78.               3.12
     79.               3.12
     80.               3.12
     81.               3.12
     82.               3.13
     83.               3.13
     84.               3.13
     85.               3.13
     86.               3.13
     87.               3.14
     88.               3.14
     89.               3.14
     90.               3.15
     91.               3.15
     92.               3.15
     93.               3.15
     94.               3.16
     95.               3.16
     96.               3.16
     97.               3.16
     98.               3.17
     99.               3.17
    100.               3.17
    101. end
    
    . gen seconds = 60 * floor(time) + round(100 * mod(time, 1), 1) 
    
    . list 
    
         +----------------+
         | time   seconds |
         |----------------|
      1. |    1        60 |
      2. | 1.03        63 |
      3. | 1.25        85 |
      4. | 1.48       108 |
      5. | 2.08       128 |
         |----------------|
      6. |  2.1       130 |
      7. | 2.13       133 |
      8. | 2.13       133 |
      9. | 2.15       135 |
     10. | 2.16       136 |
         |----------------|
     11. | 2.16       136 |
     12. | 2.17       137 |
     13. | 2.18       138 |
     14. | 2.19       139 |
     15. | 2.19       139 |
         |----------------|
     16. | 2.24       144 |
     17. | 2.25       145 |
     18. | 2.26       146 |
     19. | 2.29       149 |
     20. |  2.3       150 |
         |----------------|
     21. |  2.3       150 |
     22. | 2.31       151 |
     23. | 2.32       152 |
     24. | 2.32       152 |
     25. | 2.32       152 |
         |----------------|
     26. | 2.33       153 |
     27. | 2.34       154 |
     28. | 2.34       154 |
     29. | 2.34       154 |
     30. | 2.35       155 |
         |----------------|
     31. | 2.35       155 |
     32. | 2.35       155 |
     33. | 2.38       158 |
     34. | 2.39       159 |
     35. | 2.39       159 |
         |----------------|
     36. | 2.41       161 |
     37. | 2.42       162 |
     38. | 2.43       163 |
     39. | 2.43       163 |
     40. | 2.43       163 |
         |----------------|
     41. | 2.43       163 |
     42. | 2.44       164 |
     43. | 2.45       165 |
     44. | 2.46       166 |
     45. | 2.47       167 |
         |----------------|
     46. | 2.47       167 |
     47. | 2.47       167 |
     48. | 2.48       168 |
     49. | 2.51       171 |
     50. | 2.52       172 |
         |----------------|
     51. | 2.55       175 |
     52. | 2.56       176 |
     53. | 2.58       178 |
     54. | 2.58       178 |
     55. | 2.59       179 |
         |----------------|
     56. | 2.59       179 |
     57. | 2.59       179 |
     58. |    3       180 |
     59. |    3       180 |
     60. | 3.03       183 |
         |----------------|
     61. | 3.03       183 |
     62. | 3.03       183 |
     63. | 3.05       185 |
     64. | 3.05       185 |
     65. | 3.06       186 |
         |----------------|
     66. | 3.07       187 |
     67. | 3.08       188 |
     68. | 3.09       189 |
     69. | 3.09       189 |
     70. | 3.09       189 |
         |----------------|
     71. | 3.11       191 |
     72. | 3.11       191 |
     73. | 3.11       191 |
     74. | 3.11       191 |
     75. | 3.12       192 |
         |----------------|
     76. | 3.12       192 |
     77. | 3.12       192 |
     78. | 3.12       192 |
     79. | 3.12       192 |
     80. | 3.12       192 |
         |----------------|
     81. | 3.12       192 |
     82. | 3.13       193 |
     83. | 3.13       193 |
     84. | 3.13       193 |
     85. | 3.13       193 |
         |----------------|
     86. | 3.13       193 |
     87. | 3.14       194 |
     88. | 3.14       194 |
     89. | 3.14       194 |
     90. | 3.15       195 |
         |----------------|
     91. | 3.15       195 |
     92. | 3.15       195 |
     93. | 3.15       195 |
     94. | 3.16       196 |
     95. | 3.16       196 |
         |----------------|
     96. | 3.16       196 |
     97. | 3.16       196 |
     98. | 3.17       197 |
     99. | 3.17       197 |
    100. | 3.17       197 |
         +----------------+
    The date-time functions can naturally be applied too. Someone may have a simpler alternative to

    Code:
    gen double seconds2 = clock(string(floor(time))  + ":" + string(round(mod(time, 1) * 100, 1)) , "m s") / 1000

    Comment


    • #3
      Nick Cox , this is what I needed. Thanks a lot

      Comment

      Working...
      X