Announcement

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

  • converting into date formats

    Hi, how do I convert these variables into actual dates? thank you!
    Attached Files

  • #2
    year must be a numeric variable if you successfully assigned it a daily date display format. It would seem to follow that

    1. There is no need to create another daily date variable from it.

    2. Any attempt to do that using
    date() is illegal as the latter expects a string argument.

    3. The name
    year appears to be a poor choice.

    However, it is also possible that this answer misses your point. You don’t give a convincing data example (do please read and act on FAQ Advice #12) and the request for “actual dates” is not at all clear to me.

    Comment


    • #3
      Hi, sorry, the variables are currently %td and I was wondering how to just change them into YYYY, to take the 31dec away! TY

      Comment


      • #4
        if you look at the help file (-help datetime-) in the section on displaying, you will see that %ty is meant for that purpose; use the -format- command to change the display format

        Comment


        • #5
          As Rich Goldstein says, you need to read the help:

          Code:
          help datetime
          Changing the display format is not sufficient here, or even necessary. Let's take today's date:

          Code:
          . di %td  mdy(11, 26, 2019)
          26nov2019
          So far, so good. But change the display format to year and you are saying that the date is really a year: The display result will puzzle a little because Stata has no expectation of 4 digit years, but in the case of today's date if you change the format you are telling Stata that the year is really 21879.

          Code:
          . di %ty  mdy(11, 26, 2019)
          2.2e+04
          
          . di %5.0f  mdy(11, 26, 2019)
          21879
          But it's not. You need a conversion function and it can just be year():

          Code:
          . di %td  mdy(11, 26, 2019)
          26nov2019
          
          . di %ty  mdy(11, 26, 2019)
          2.2e+04
          
          . di %5.0f  mdy(11, 26, 2019)
          21879
          
          . di year(mdy(11, 26, 2019))
          2019
          So, push your variable through year().

          Comment


          • #6
            Let me add to Nick's response a note that if you read help datetime and the pages linked from it, you will learn that it is possible to change the format of a daily date like 31Dec2017 to only display the year portion. But that doesn't solve your analytical problems, because the underlying value is still the same, you have just changed how Stata displays the number.

            You (apparently) have yearly data measured on the last day of each year. If you have a variable, say X, in an observation with a daily date of 31Dec2017, and you specify L.X to obtain the lagged value of X, Stata will seek the observation with a date of 30December2017, because it recognizes your dates as daily dates and looks for the previous day.

            So you need a yearly date variable as Nick describes to ensure that calculations that depend on relative times, like leads and lags, will do what you want.

            Comment


            • #7
              Thank you Rich, Nick and William!
              format %ty year
              format %5.0f year
              replace year=year/365
              worked

              Comment


              • #8
                Consider the following example
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float date
                20453
                20819
                21184
                end
                format %td date
                
                generate year = yofd(date)
                generate badyear = date/365
                list, clean noobs
                Code:
                . list, clean noobs
                
                         date   year    badyear  
                    31dec2015   2015   56.03562  
                    31dec2016   2016   57.03836  
                    31dec2017   2017   58.03836
                Following the techniques in the documentation cited in post #6 is advisable if you want to create a year that is meaningful to Stata. Note that the year() function Nick used in post #5 is another name for the yofd() function in the example above and in the documentation.

                Comment


                • #9
                  As William Lisowski implies #7 really was not recommended and is a thoroughly bad idea. Let readers beware. We decided against downvotes here when converting to a web forum, but occasionally they would be useful.

                  Comment

                  Working...
                  X