Announcement

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

  • #16
    Those are interesting suggestions, but I have doubts about both.

    I would rather let users make their own separate decisions about variable order.

    I've often found that ingredients remain useful. For example, climatological data often arrive with month and year variables, which remain useful indefinitely even after monthly dates have been calculated. More crucially, a dropold option would not be welcome if the command created the wrong thing.

    Comment


    • #17
      Dear Nick,

      Is there any update of -numdate- with replace option. As I am using -strdate- for converting string date to numeric but it is not working if I have string variable with date and time i.e.
      "2018-02-23 06:08:30"

      I want to convert this type of values to numeric into same variable. Please suggest in this regard.

      Thanks
      Rasool Bux

      Comment


      • #18
        There is no version of numdate (SSC) with a replace option and I have no plans for adding one. Unless someone convinces me otherwise, it is either not really needed (just drop what you don't want first) or a bad idea (you shouldn't want to destroy the raw materials you're using).

        strdate (SSC etc) is a program by Roger Newson. I've never used it but either he or anybody else would need to see your code to say what you are doing wrong.

        Your example looks straightforward to me:

        Code:
        clear 
        input str42 mydate 
        "2018-02-23 06:08:30" 
        end  
        
         numdate clock ndate = mydate, pattern(YMD hms)
        
         l
        
             +------------------------------------------+
             |              mydate                ndate |
             |------------------------------------------|
          1. | 2018-02-23 06:08:30   23feb2018 06:08:30 |
             +------------------------------------------+

        Comment


        • #19
          In fact more can be said. strdate is a 2002 command for Stata 7 and as its help indicates it expects information on month, day and year for daily date variables. It's therefore unaware of date-time variables introduced in Stata 10.

          Comment


          • #20
            Hi Statalisters, I have a quick additional question about numdate.
            I had a string variable in the format YYYYMM (e.g.201409), which I converted into numdate (2014m9) and then did some operations (e.g. subtracting a month, yielding 2014m8). Now that I've got the numdate, is there a way to convert it back into string, so I would get 201408? Thank you!

            Comment


            • #21
              Not with numdate. It could be done directly with e.g. string() with a format argument. It’s hard for me to see why you might find that useful within Stata.

              Comment


              • #22
                Hi Nick, thank you very much for your response and sorry for taking so long with mine. The reason why I was looking to convert the variable back into string was that I need to use the year a categorical variable (e.g. 2014, 2015) in a model. I thought converting it back to string would be the easiest way to do it. But perhaps there is a better way

                Comment


                • #23
                  Originally posted by Zuzana Dancikova View Post
                  Hi Nick, thank you very much for your response and sorry for taking so long with mine. The reason why I was looking to convert the variable back into string was that I need to use the year a categorical variable (e.g. 2014, 2015) in a model. I thought converting it back to string would be the easiest way to do it. But perhaps there is a better way
                  If I undertand correctly, you have a monthly string (e.g. 2019m6). If you want the year, you can use gen year = year(dofm(<variable>)). This should get you what you want.

                  Comment


                  • #24
                    #23 If "2019m6" is literally a string value of a string variable, then that code won't work. If 2019m6 is what you see when a numeric monthly date variable is formatted %tm then that code is certainly one way to do it.

                    Comment


                    • #25
                      My variable was in the numdate format.
                      I first tried tostring, specifically the command: tostring return, replace usedisplayformat - however, I was getting back "return cannot be converted reversibly; no replace."
                      However, when I tried the other command ( gen year = year(dofm(<variable>))), it worked perfectly. Thank you both!

                      Comment


                      • #26
                        Note also that getting a year date variable out of a (numeric) monthly date variable is one of the things that convdate in the numdate package can do directly. See also #5 in this thread.

                        Code:
                        . clear
                        
                        . set obs 1
                        number of observations (_N) was 0, now 1
                        
                        . gen mdate = ym(2019, 9)
                        
                        . format mdate %tm
                        
                        . convdate ty year = mdate
                        
                        . l
                        
                             +---------------+
                             |  mdate   year |
                             |---------------|
                          1. | 2019m9   2019 |
                             +---------------+
                        Last edited by Nick Cox; 26 Sep 2019, 03:53.

                        Comment


                        • #27
                          Thanks to Luis Pecht who reported a bug and to KitBaum for uploading the files, package numdate has been updated on SSC with new files convdate.ado and convdate.sthlp.

                          Comment

                          Working...
                          X