Announcement

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

  • Formatting time string to Stata time variable

    Hello,

    Is it possible to format a string variable of the format "HH:MM:SS" to a time variable? In the documentation it seems like one can only format a timestamp that has both date and time, not time only. But I'm not sure if I am missing something.

    Thanks!

  • #2
    The -details- options within -format- allow you to specify how the results appear, such as %tchh:mm:ss
    Code:
    . clear
    
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . gen t = "12:20:15"
    
    . gen double stata_t = clock(t, "hms")
    
    . l
    
         +---------------------+
         |        t    stata_t |
         |---------------------|
      1. | 12:20:15   44415000 |
         +---------------------+
    
    . format stata_t %tchh:mm:ss
    
    . l
    
         +---------------------+
         |        t    stata_t |
         |---------------------|
      1. | 12:20:15   12:20:15 |
         +---------------------+

    Comment


    • #3
      You have to always distinguish between the actual content of a Stata datetime variable (there are no time variables in Stata) and its display format. All Stata datetime variables have both a date and a time. If you want to only see the time part of it in the browser and in listings, etc. you can -format datetime_variable %tcHH:MM:SS-. Then you will not be aware of the date part. But it is still there, and if you perform calculations with them, the results may seem strange if viewed only at the HH:MM:SS level.

      The other thing is that you can't apply a datetime display format to a string variable. So if you are starting with a string variable, you must use the -clock()- function to create a numeric datetime variable that corresponds to the date and time mentioned in the string variable. Then you can apply the -format- command I mentioned in the preceding paragraph so that the numeric variable will look like a human-readable time when your eyes encounter it.

      Added: Crossed with #2.

      Comment


      • #4
        To expand on Clyde's point, Stata assumes that time begins on the 1st of January, 1960.

        Code:
        di %td dofc(hms(0,0,0))
        Res.

        Code:
        . di %td dofc(hms(0,0,0))
        01jan1960
        Therefore, even if you display the time part, the hidden date assumes that starting point.

        Code:
        di %tchh:mm:ss 44415000
        di %tc  44415000
        Res.:

        Code:
        . di %tchh:mm:ss 44415000
        12:20:15
        
        . 
        . di %tc  44415000
        01jan1960 12:20:15
        Therefore, as Clyde states, there is no such thing as a time variable in Stata.

        Comment


        • #5
          Hi,
          I am trying to startify NIS data based on number of procedures performed per year. Can you please guide which command should I use. I would like to strtify data inti 5 quatrtiles 1. <=10 2. >10 & <=25 3. >25 & <=45 4. >45 &<=90 5. >90

          Thanks

          Comment


          • #6
            Originally posted by Jasmeet Kaur View Post
            Hi,
            I am trying to startify NIS data based on number of procedures performed per year. Can you please guide which command should I use. I would like to strtify data inti 5 quatrtiles 1. <=10 2. >10 & <=25 3. >25 & <=45 4. >45 &<=90 5. >90

            Thanks
            You have posted in the wrong thread. If you haven’t done so already, please create a new thread to ask your question. It would be helpful to provide a minimal data example as well since you are asking for help with code. Please read our FAQ on how to ask questions effectively.

            Comment

            Working...
            X