Announcement

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

  • Struggling with Clock Variable

    Hello!

    I am analyzing an annual dataset on Electrical Vehicles and need to find the sum of time (hours:minutes:seconds) of a charging station used in a day (month/date/year).

    The charging station can have multiple users per day, so I would like to code the dataset so that each date has one output amount of time it was used.

    I have attempted to use the %t command (%tc and %tC) but I already have a date and simply need to convert the time value so that Stata understands it as "time" and not simply as a "string".

    I am fairly new to Stata and there may be a simple solution that I am overlooking.

    Thank you in advance for your advice and suggestions!

    Warmly,
    Ella

  • #2
    what does the string look like?

    Comment


    • #3
      Hi George!

      The string time appears as (i.e.)

      7/1/2024 | 01:44:30
      7/1/2024 | 07:25:47

      I understand that I can encode it to be read as numeric, but when I then go to collapse it the sum does not add up to a number that would translate to a time.

      Thank you so much for your help!

      Comment


      • #4
        did you create a variable for the spread of time for each use?

        Comment


        • #5
          This might be of some use.

          Code:
          clear
          input str19 var1
          "7/1/2024 | 01:44:30"
          "7/1/2024 | 07:25:47"
          end
          
          g date = clock(var1,"MDYhms")
          g spread = date[2] - date[1]
          g hours = hours(spread)

          Comment


          • #6
            insert the word "double" where George Ford write "g date" as without it your results will not be trustable; see
            Code:
            h datetime
            also, please use -dataex- in the future as the results are both clearer and easier to use; see the FAQ

            Comment


            • #7
              As implied by positive answers #5 and #6 encode is not at all a solution here. encode by default maps distinct strings to integers 1 upwards and takes precisely no account of the date and time information in strings.

              The principle is shown by a toy example. Consider strings "frog" "toad" and "newt". With nothing else said encode sorts those values to "frog" (assigned 1), "newt" (assigned 2) and "toad" (assigned 3). It's a robot that doesn't examine content other than as instructed. Even with dates "31-12-1980" would get assigned a higher value than "01-01-1981".

              More at https://journals.sagepub.com/doi/pdf...867X1801800413 which among several other details emphasises that date functions are needed for dates. There is just about one exception: if your years arrive as strings -- say "1776" and "1812" -- then destring gets you where you want to be in one fair swoop.

              Comment

              Working...
              X