Announcement

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

  • Change minutes from decimal number to minutes:seconds

    Hello statalisters


    I´ve been struggeling with this one and hope one of you can help.

    I currently have a variable (minutes) as a decimal number (25.86667), but would like to get the minutes:seconds. In this case 25.52 min. I´ve been working with floor and round, trying to accomplish this, but so far no luck.

    Daniel

  • #2
    I don't know what "get" means here precisely. You can see (using a %tc display format) such a representation if you multiply minutes to get milliseconds as in

    Code:
    . di %tcMM:SS 25.86667 * 60e3
    25:52
    or you can produce a string as in

    Code:
    . di string(floor(25.86667)) + ":" + string(60 * mod(25.86667, 1), "%2.0f")
    25:52
    To get a new string variable, I would use the first method, as in

    Code:
    gen sminutes = string(60e3 * minutes, "%tcMM:SS")
    for simplicity so long as numbers were less than 60 minutes, and the second method for generality. (How large can your values be?)

    Comment


    • #3
      Thanks Nick for trying to "get" me what I want.

      The variable I´m working with is type numeric (double) and for future calculations this type is optimal. I will try providing an example of the current data set.


      id time (time2)
      334 25.866667 25.52
      334 0 0
      334 0 0
      334 0 0
      334 0 0
      334 0 0
      334 0 0
      334 37.983334 ...........?

      The time variable is the decimal number variable, which I´m trying to change to a numeric variable, with the actual minutes and seconds -> time2

      Thanks
      Daniel



      Comment


      • #4
        Sorry, but I don't see that you have clarified what you want at all.

        You can't or shouldn't want to hold minutes and seconds as a decimal variable. What should that mean? 23.52 means 23 min 52 sec but 24.08 - 23.52 = 0.16? The likelihood of confusing yourself or people who use your data or results is extraordinarily high.

        What do you want to do with such a variable?

        Comment


        • #5
          1.cumulate minutes used over a period of time

          2.calculate minutes/km

          Comment


          • #6
            OK. Both uses imply calculations with times in minutes and either would be messed up by converting mm:ss to mm.ss. At most you can present your final results using a display format as already explained. Keep your variable as it comes.

            Comment

            Working...
            X