Announcement

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

  • Replacing dates manually (

    Hello,

    Wondering if anyone could help me. I've got an int variable "datesxonset" in format %td. The dates look like "09jan2015". However, some observations are missing that information so "datesxonset" is missing. However, a second data source has the data so I can replace the information. I've tried this:

    replace datesxonset = 18dec2014 if id=="COUNTRY-YR-####"
    replace datesxonset = 12/18/2014 if id=="COUNTRY-YR-####"
    Both of those without and without quotes around the date proposed. None work. The error message is "invalid syntax".

    I've tried this as well: gen datesxonset2 = date(datesxonset, "MDY") and that didn't work.

    How can I manually replace the variable "datesxonset" with the dates that I have from the second source?

    Thanks!



  • #2
    Try:

    Code:
    replace datesxonset = td(18dec2014) if id==...
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Your attempt with - gen datesxonset2 = date(datesxonset, "MDY")- is closest. There are a couple of ways you could go:

      Code:
      replace datesxonset = td(18dec2014) if id == "COUNTRY-YR-####"
      
      // OR
      
      replace datesxonset = daily("12/18/2014", "MDY") if ...
      
      // OR
      
      replace datesxonset = daily("18dec2014", "DMY") if ...
      Note: daily() is an alias for date().

      An altogether different approach, that might be less prone to typographical errors, is to -merge- the data sets (with the -update- option specified)

      Crossed in cyberspace with #2

      Comment


      • #4
        The existing variable datesxonset appears to be numeric so your replacement, on this information, should be of the form

        Code:
        replace datesxonset = daily("18dec2014", "DMY") if id=="COUNTRY-YR-####"
        date() is the same function as daily() but the latter is a better name.

        Note that

        1. your replacement date is DMY, not MDY.

        2. a numeric variable requires a numeric replacement value, not a string value.

        3. daily(datesxonset, ...) or date(datesxonset, ...) would be wrong regardless of what the second argument was because daily() works on strings, not numeric arguments, and your variable datesxonset is already numeric.

        This is all documented in the help. Unfortunately the details do matter and the details you need are documented together with the details others need, and you don't.

        Comment


        • #5
          Clyde's answer crossed with mine as often happens.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Clyde's answer crossed with mine as often happens.
            I have similar Issue, when i split my Date variable originally stored in the format yyyy-mm-dd in excel, it displays the date to these format yyyy_mm_dd. Please, how can I convert it back to retain the hyphen and not underscore?

            Comment


            • #7
              Gideon Joseph Sorry. but I can't follow what you did without data example or code. If you're referring to something you did in MS Excel, I have even less idea.

              Please read and act on https://www.statalist.org/forums/help#stata

              Comment


              • #8
                Originally posted by Nick Cox View Post
                Gideon Joseph Sorry. but I can't follow what you did without data example or code. If you're referring to something you did in MS Excel, I have even less idea.

                Please read and act on https://www.statalist.org/forums/help#stata
                No, not what that i did it in Ms Excel, i only imported the data into stata and when i split the date variable it split into date1, date2 and date3, when i tab date1 it displays it with underscore. The date format in excel is yyyy-mm-dd. E.g. 2007-09-12 when i split it in stata, the appears in 3 parts date1, date2 and date3. If i tab date1 it should be showing 2007 but instead it shows it as 2007_09_12 as a string which is unusual, what do i need to do so it does not display underscore but display only 2007 without the other parts _09_12.

                Comment


                • #9
                  Still no data example. What was the precise split command you used? FAQ #12 is essential reading.

                  This works for me:

                  Code:
                  . clear 
                  
                  . set obs 1 
                  number of observations (_N) was 0, now 1
                  
                  . gen date = "2007-09-12"
                  
                  . split date, parse(-) destring 
                  variables born as string: 
                  date1  date2  date3
                  date1: all characters numeric; replaced as int
                  date2: all characters numeric; replaced as byte
                  date3: all characters numeric; replaced as byte
                  
                  . list  
                  
                       +------------------------------------+
                       |       date   date1   date2   date3 |
                       |------------------------------------|
                    1. | 2007-09-12    2007       9      12 |
                       +------------------------------------+
                  Code:
                  
                  
                  Last edited by Nick Cox; 18 Jun 2019, 00:17.

                  Comment


                  • #10
                    Naturally, although I used hyphens - in #9 the same principles would apply with underscores _ : note that #6 and #8 move back and forth between the two.

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      Naturally, although I used hyphens - in #9 the same principles would apply with underscores _ : note that #6 and #8 move back and forth between the two.
                      Okey, I get it, but what i observed is that if one has thousands of dates and if one import to stata and split, when one tabulate date1 some appears with underscore, that is what am confused about.

                      Comment


                      • #12
                        All I can guess from #11 is that your dates are a mess -- in inconsistent formats -- so sometimes code works as you wish and sometimes it doesn't. No data example, no detailed diagnosis possible.

                        Comment

                        Working...
                        X