Announcement

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

  • generating new variable day / month / year / hour / minute / second

    Hello
    I would like to combine 4 variables to create one time and date variable.
    But when i go:

    Code:
    input byte id str9 date
    1 "01jan1977" 
    end
    
    gen newdate = daily(date, "DMY")
    format %td newdate
    gen hour=1
    gen minute=1
    gen sec=1
    drop date
    
    gen D_M_Y_H_M_S = dhms((date(newdate, "DMY")),hour,minute,sec)
    I get a error (r(109))

    what a i doing wrong?

    Lars

  • #2
    Code:
     
     gen D_M_Y_H_M_S = dhms(newdate ,hour,minute,sec)

    Comment


    • #3
      Note that daily() and date() are just one and the same function. They accept string input and produce numeric daily date output.

      newdate was created by you as a numeric date variable by pushing a string date at daily().

      It follows that you don't need to, and in fact can't legally, push it through date() again, or daily() again. It doesn't accept numeric input.

      Beyond that a date-time variable should always be created as double. (help dates, repeatedly)

      You can get to the same place with

      Code:
      . clear 
      
      . input byte id str9 date
      
                 id       date
        1. 1 "01jan1977" 
        2. end
      
      . gen double D_M_Y_H_M_S = dhms(daily(date, "DMY"),1,1,1)
      
      . format %tc D 
      
      . list 
      
           +-------------------------------------+
           | id        date          D_M_Y_H_M_S |
           |-------------------------------------|
        1. |  1   01jan1977   01jan1977 01:01:01 |
           +-------------------------------------+

      Comment


      • #4
        Thank you both! i see what i did wrong.

        Someone from a stata newcomers emailing-list sent me this:

        Code:
        clear
        input byte id str9 date
        1 "01jan1977"
        end
        gen h=1
        gen m=1
        gen s=1
        gen d_m_y_h_m_s = dhms((date(date, "DMY")), h, m, s)

        Comment


        • #5
          It's the same solution. It shows how to use dhms() whenever the components are variables. For your toy example, you don't need to put single values in variables, but clearly your full problem (or someone else's full problem) might be of that kind.

          I didn't know that there was a email list for Stata newcomers. I imagine that many people might want to know about it.

          Comment


          • #6
            The emailing list is at my university.
            Don't know if its public.

            Comment


            • #7
              I see. Local support networks are something different.


              (My brother works at your university, if I've identified you correctly, not that he uses Stata.)

              Comment


              • #8
                Thats interesting, i actually think I'm the only one carrying that name in Denmark, so chances are its me. What faculty is he working at?

                Comment


                • #9
                  I will send you a private message.

                  Comment


                  • #10

                    how to generate new variable: hour: minute: second
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input int ID float SEGUNDOS_ID_
                    2 60
                    15 60
                    27 60
                    39 20040
                    51 2820
                    68 300
                    70 480
                    76 1200
                    78 60
                    102 60
                    end
                    [/CODE]
                    Last edited by Andrés Lahur Talavera Cuya; 01 Nov 2019, 11:44.

                    Comment


                    • #11
                      Your variable already is in seconds, so all that is needed is a multiplication by 1000 to convert to ms.


                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input int ID float SEGUNDOS_ID_
                      2 60
                      15 60
                      27 60
                      39 20040
                      51 2820
                      68 300
                      70 480
                      76 1200
                      78 60
                      102 60
                      end
                      
                      gen double mytime = 1000 * SEGUNDOS_ID 
                      format mytime %tcHH:MM:SS 
                      
                      list , abb(12)
                      
                      
                           +-------------------------------+
                           |  ID   SEGUNDOS_ID_     mytime |
                           |-------------------------------|
                        1. |   2             60   00:01:00 |
                        2. |  15             60   00:01:00 |
                        3. |  27             60   00:01:00 |
                        4. |  39          20040   05:34:00 |
                        5. |  51           2820   00:47:00 |
                           |-------------------------------|
                        6. |  68            300   00:05:00 |
                        7. |  70            480   00:08:00 |
                        8. |  76           1200   00:20:00 |
                        9. |  78             60   00:01:00 |
                       10. | 102             60   00:01:00 |
                           +-------------------------------+

                      Comment

                      Working...
                      X