Announcement

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

  • Remove seconds from time

    Hello all,

    I am working with a large data set, I got readings every 10 seconds from the equipment. I am trying to remove the seconds from time and the letters PM/AM. I am using the following code but I am getting missing vales. Any suggestion will be greatly appreciated.

    generate gassample_time = clock(var_time, "hm")

  • #2
    The best approach is to provide data, I mean, an example, but you should try something like:


    Code:
    generate double gassample_time = clock(var_time,"YMDhm")
    Hopfully that helps
    Last edited by Marcos Almeida; 21 Aug 2019, 12:40.
    Best regards,

    Marcos

    Comment


    • #3
      Discarding seconds is a rounding down operation, so one route is keep thinking of floor(). Here is some technique. Modesty aside, I am fairly experienced, but the experience boils down to this one thing: only reading the fine help can tell you what to do. Throughout, remembering that Stata's units here are milliseconds keeps you on the straight and narrow.


      Code:
      . clear
      
      . set obs 1
      number of observations (_N) was 0, now 1
      
      . gen double test = clock("2019 Aug 21 20:30:40", "YMD h m s")
      
      . format test %tc
      
      . list
      
           +--------------------+
           |               test |
           |--------------------|
        1. | 21aug2019 20:30:40 |
           +--------------------+
      
      . gen double wanted = 60000 * floor(test/60000)
      
      . format wanted %tc
      
      . list
      
           +-----------------------------------------+
           |               test               wanted |
           |-----------------------------------------|
        1. | 21aug2019 20:30:40   21aug2019 20:30:00 |
           +-----------------------------------------+
      
      . help datetime display format
      
      . format wanted %tcCCYY_Mon_DD_HH:MM
      
      . list
      
           +----------------------------------------+
           |               test              wanted |
           |----------------------------------------|
        1. | 21aug2019 20:30:40   2019 Aug 21 20:30 |
           +----------------------------------------+

      Comment


      • #4
        This is part of the data set I am trying to work with. I am trying to round up the time by minute in order to get the mean per minute


        time ppb
        11:24:05 AM 25
        11:24:15 AM 44
        11:24:25 AM 51
        11:24:35 AM 45
        11:24:45 AM 41
        11:24:55 AM 37
        11:25:05 AM 40
        11:25:15 AM 51
        11:25:25 AM 50
        11:25:35 AM 44
        11:25:45 AM 45
        11:25:55 AM 41
        11:26:05 AM 42
        11:26:15 AM 39
        11:26:25 AM 57
        11:26:35 AM 53
        11:26:45 AM 49
        11:26:55 AM 49
        11:27:05 AM 42

        Comment


        • #5
          Please read the FAQ, there you'll find how to share data. In short, please use - dataex - for that matter and present the data under code delimiters.

          That said, assuming you have a string, you may start by dropping the last parcel.

          Best regards,

          Marcos

          Comment


          • #6
            In addition to Marcos Almeida 's excellent advice, I add that if you want rounding up you need to use ceil() not floor(). Rounding up is not, as I understand it, how digital clocks or watches that show hours and minutes work, but that convention is just one of two possible.

            Note that the syntax in #1 is not a way to get Stata to ignore what you don't want. Thus this fails to do what you want

            Code:
            . display clock("20:30:40", "hm")
            .
            because the time string doesn't match the specification. clock() is not a function to extract; it is a function to map string to numeric one-to-one.

            Comment


            • #7
              If it is a string variable, you could start with:

              Code:
              gen gassample_time = substr(var_time,1,8)
              I'm not with my Stata now, hence I cannot test the code.

              Hopefully that helps.
              Best regards,

              Marcos

              Comment

              Working...
              X