Announcement

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

  • How to destring date variable (year month)?

    Hello,

    I have a time series dataset with the following date variable format:

    2000M09
    2000M10
    2000M11

    And so on until 2015M09.

    This variable is a string variable, which I would like to destring such that it can act as the date variable.

    I have tried the functions date(date, "YM") and date(date, "YYYYMM") but it doesn't seem to work. My plan was then, once I have the numerical versions of my date variable, to format it to "monthly data".

    Any ideas of how to destring this type of format (month year)?

    Thank you very much in advance!

  • #2
    Don't destring dates at all. The premise of destring here could only be that non-numeric text is irrelevant, so that you really have values such as 200009. But that isn't a date; it's an integer.

    Also, as documented under help dates, date() is a function for producing daily dates, so it won't do what you want.

    Further, you cannot change values by changing their display format.

    As also documented under help dates, monthly() is a function for producing monthly dates, so it is what you want.

    Code:
    . di monthly("2000M09", "YM")
    488
    
    . set obs 1
    obs was 0, now 1
    
    . gen sdate = "2000M9"
    
    . gen mdate = monthly(sdate, "YM")
    
    . format mdate %tm
    
         +-----------------+
         |  sdate    mdate |
         |-----------------|
      1. | 2000M9   2000m9 |
         +-----------------+
    In Stata 12+ you could use numdate (SSC) to do this.

    N.B. Pretty much the same question was asked just a few days ago. http://www.statalist.org/forums/foru...a-monthly-date

    Searching the archives is a really good idea!
    Last edited by Nick Cox; 29 Oct 2015, 04:56.

    Comment


    • #3
      Thanks you very much! The numdate function worked perfectly!

      Comment


      • #4
        Good, but numdate is a command. Regardless of what is true in other software, functions and commands are disjoint in Stata.

        Comment


        • #5
          Nick Cox is it possible to make dates like 2000m1d4 from integer values? Because that is what I have now... Examples, 19234, 16983 etc.

          Comment


          • #6
            As emphasised earlier in the thread, help dates leads to documentation. In turn that points to

            Code:
            help datetime display formats

            Examples:

            Code:
            . di %tdCY!mn!dd 19234
             2012m8d29
            
            . di %td 19234
            29aug2012
            So, if you had a daily date variable and you wanted it to display as in the first example, you would assign a format:

            Code:
            format mydate %tdCY!mn!dd

            Comment


            • #7
              Hi can you please help me?

              I have a variable in state stored as string like this: " 2021-10-16" until 2022, and the days and month are not in order. I am trying to convert this variables to just 2021, 2022, by collapsing all these months/days into just year. Can you help me?

              Thank you so much.
              Dania

              Comment


              • #8
                #7 was double posted at https://www.statalist.org/forums/for...e-made-of-year Please follow there if interested.

                Comment

                Working...
                X