Announcement

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

  • Re: Converting a string variable to a date variable

    Hello,

    My date variable (moyr) could not be used when I declared my dataset to be monthly panel data. As seen below, moyr is a string variable that is composed of YYYY-MM.

    . list moyr in 1/5
    +---------+
    | moyr |
    |---------|
    1. | 2017-01 |
    2. | 2017-02 |
    3. | 2017-03 |
    4. | 2017-04 |
    5. | 2017-05 |
    +---------+

    Thus, I generate a new date variable by using the code: generate date = date(moyr,"YM"). However, values for the date variable are not readable.

    . list moyr date in 1/5
    +-----------------+
    | moyr date |
    |-----------------|
    1. | 2017-01 20820 |
    2. | 2017-02 20851 |
    3. | 2017-03 20879 |
    4. | 2017-04 20910 |
    5. | 2017-05 20940 |
    +-----------------+

    Thus, I reformatted the date variable to be YYYY-MM using the code: format %tmCCYY-NN date.

    . list moyr date in 1/5

    +-------------------+
    | moyr date |
    |-------------------|
    1. | 2017-01 3695-01 |
    2. | 2017-02 3697-08 |
    3. | 2017-03 3699-12 |
    4. | 2017-04 3702-07 |
    5. | 2017-05 3705-01 |
    +-------------------+

    However, values in the date variable are still not readable. Any advice would be greatly appreciated. Thank you.

  • #2
    Code:
    g date2 = monthly(date, "YM")
    tsset date2, monthly

    Comment


    • #3
      Hello George,

      I wrote your code but found an error message. Please see below. Any advice would be appreciated.

      . g date2 = monthly(date, "YM")
      type mismatch
      r(109);


      Comment


      • #4
        Replace date with moyr in the code from #2.

        Comment


        • #5
          Here is some example code to demonstrate what is happening.
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str7 moyr
          "2017-01"
          "2017-02"
          "2017-03"
          "2017-04"
          "2017-05"
          end
          // the date function produces a daily date
          generate date1 = date(moyr,"YM")
          format date1 %td
          generate diff1 = date1 - date1[_n-1]
          // we can tell Stata to ignore the day when it displays the date
          generate date2 = date(moyr,"YM")
          format date2 %tdCCYY-NN
          generate diff2 = date2 - date2[_n-1]
          // we would prefer to have a monthly date for analysis
          generate date3 = mofd(date(moyr,"YM"))
          format date3 %tm
          generate diff3 = date3 - date3[_n-1]
          Code:
          . list
          
               +----------------------------------------------------------------+
               |    moyr       date1   diff1     date2   diff2    date3   diff3 |
               |----------------------------------------------------------------|
            1. | 2017-01   01jan2017       .   2017-01       .   2017m1       . |
            2. | 2017-02   01feb2017      31   2017-02      31   2017m2       1 |
            3. | 2017-03   01mar2017      28   2017-03      28   2017m3       1 |
            4. | 2017-04   01apr2017      31   2017-04      31   2017m4       1 |
            5. | 2017-05   01may2017      30   2017-05      30   2017m5       1 |
               +----------------------------------------------------------------+
          Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

          All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

          Comment


          • #6
            Did changing "date" to "moyr" work? (Sorry for the confusion). I created a dataset like Lisowski did and it worked for me.

            Is there something else in the moyr string?
            Last edited by George Ford; 08 Jul 2021, 17:05.

            Comment

            Working...
            X