Announcement

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

  • Change date format

    Hello,

    I tried to change the date format of my dataset. I want all dates to be stated in years.
    However, it didn't work. And when I try to get a -dataex- from the data, the dates are not how they appear in my dataset. They appear like this:

    Date
    31dec1984
    31dec1984
    31dec1984
    31dec1984
    31dec1984
    31dec1984
    28dec1984
    28dec1984
    26dec1984
    31dec1984

    This is how it appears in -dataex- :
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Date long(tarnatcode taracqcode) double Dealvaluemil
    9131  71 213       .
    9131  71 119       .
    9131 213 105      50
    9131 211 213       .
    9131 213  13     639
    9128 213 211       3
    9128  25 211       .
    9126 172 213   2.233
    9117  36  25      10
    9117 213 211    20.1
    9107 213  22    2.25
    9110 213  25      38
    9131 213 211      37
    9131 166 213    11.5
    9131 213 211     363
    9118 213 211     125
    9079 213  36 376.491
    9098 213 211  17.094
    9118 213 211    13.5
    9131 213 211      80
    9065 153 213       .
    9047 213 211   1.775
    9054 213  99       8
    9022 213  22     4.5
    9063 213  13      60
    9098 213 211     110
    9100 213 211      43
    9028 213  36      30
    8980 166 213       7
    8967 213 211  31.507
    8957 213  36    19.3
    8952 213 219      30
    9121 213  36     106
    9015 213 211      83
    8945 213 211      22
    8942  36 213       .
    8937 213 105     280
    8979 213  36     100
    9131 213 211     5.8
    8922 213 100  15.933
    8918  36 213       .
    8914 196 211     200
    9085 213  13   7.224
    8903 213 211      18
    8903 213 211  12.353
    9101  22 213       8
    8882  36 211       .
    9009 213 105     322
    8904 213 194    22.7
    8869  71 211       .
    8917 213 150      45
    8868 213  36   21.78
    8867 213 211       3
    8907 174 213     1.2
    8859 213  36     6.5
    8965 213 211     511
    8881 213 211    27.3
    8855 213 211      10
    8896 211 213     360
    9131 213  36      12
    8972 213  36       8
    8850 213  71      15
    8856 213 211    14.7
    8841 213 211       4
    8853  36 213   2.448
    8995 153 213  36.382
    8917 213 211      85
    9075 213 211   6.522
    8832 213 211   2.918
    8875 213 105 435.633
    8860 213  36     4.3
    8952 196 105     144
    8873 213  36      47
    8810 213  36   4.698
    8806 213  36      25
    8805 213 211      50
    8805  36 213   2.411
    8881 213 211      14
    8896 213 211      82
    8813 213 219  36.783
    8846 213 211    16.5
    8825 211 213   12.51
    8902 213 211     250
    8967 213  36   8.108
    8811 213  13      43
    8776 213  12     4.5
    8775  40 213       .
    8770 213  36  31.497
    9118 213  36     896
    8769 213 211       1
    8769 213  36       3
    8765 174 213       .
    8764 219 211       .
    8764 194 211       .
    8764  71 105       .
    8763 213 206       5
    8932 213  36      30
    8750 213 211      66
    8783 213  99      10
    8855 166 213    14.1
    end
    format %td Date
    label values tarnatcode natcodes
    label values taracqcode natcodes
    label def natcodes 22 "BH", modify
    label def natcodes 25 "BL", modify
    label def natcodes 36 "CA", modify
    label def natcodes 40 "CE", modify
    label def natcodes 71 "FR", modify
    label def natcodes 153 "NT", modify
    label def natcodes 166 "PR", modify
    label def natcodes 172 "RU", modify
    label def natcodes 174 "SA", modify
    label def natcodes 194 "SW", modify
    label def natcodes 196 "SZ", modify
    label def natcodes 211 "UK", modify
    label def natcodes 213 "US", modify
    label def natcodes 219 "WG", modify
    label def natcodes 12 "AS", modify
    label def natcodes 13 "AU", modify
    label def natcodes 99 "IS", modify
    label def natcodes 100 "IT", modify
    label def natcodes 105 "JP", modify
    label def natcodes 119 "LX", modify
    label def natcodes 150 "NO", modify
    label def natcodes 206 "TW", modify

    I tried the following code:
    Code:
    format Date %ty
    And after this code the following appeared:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Date long(tarnatcode taracqcode) double Dealvaluemil
    9131  71 213       .
    9131  71 119       .
    9131 213 105      50
    9131 211 213       .
    9131 213  13     639
    9128 213 211       3
    9128  25 211       .
    9126 172 213   2.233
    9117  36  25      10
    9117 213 211    20.1
    9107 213  22    2.25
    9110 213  25      38
    9131 213 211      37
    9131 166 213    11.5
    9131 213 211     363
    9118 213 211     125
    9079 213  36 376.491
    9098 213 211  17.094
    9118 213 211    13.5
    9131 213 211      80
    9065 153 213       .
    9047 213 211   1.775
    9054 213  99       8
    9022 213  22     4.5
    9063 213  13      60
    9098 213 211     110
    9100 213 211      43
    9028 213  36      30
    8980 166 213       7
    8967 213 211  31.507
    8957 213  36    19.3
    8952 213 219      30
    9121 213  36     106
    9015 213 211      83
    8945 213 211      22
    8942  36 213       .
    8937 213 105     280
    8979 213  36     100
    9131 213 211     5.8
    8922 213 100  15.933
    8918  36 213       .
    8914 196 211     200
    9085 213  13   7.224
    8903 213 211      18
    8903 213 211  12.353
    9101  22 213       8
    8882  36 211       .
    9009 213 105     322
    8904 213 194    22.7
    8869  71 211       .
    8917 213 150      45
    8868 213  36   21.78
    8867 213 211       3
    8907 174 213     1.2
    8859 213  36     6.5
    8965 213 211     511
    8881 213 211    27.3
    8855 213 211      10
    8896 211 213     360
    9131 213  36      12
    8972 213  36       8
    8850 213  71      15
    8856 213 211    14.7
    8841 213 211       4
    8853  36 213   2.448
    8995 153 213  36.382
    8917 213 211      85
    9075 213 211   6.522
    8832 213 211   2.918
    8875 213 105 435.633
    8860 213  36     4.3
    8952 196 105     144
    8873 213  36      47
    8810 213  36   4.698
    8806 213  36      25
    8805 213 211      50
    8805  36 213   2.411
    8881 213 211      14
    8896 213 211      82
    8813 213 219  36.783
    8846 213 211    16.5
    8825 211 213   12.51
    8902 213 211     250
    8967 213  36   8.108
    8811 213  13      43
    8776 213  12     4.5
    8775  40 213       .
    8770 213  36  31.497
    9118 213  36     896
    8769 213 211       1
    8769 213  36       3
    8765 174 213       .
    8764 219 211       .
    8764 194 211       .
    8764  71 105       .
    8763 213 206       5
    8932 213  36      30
    8750 213 211      66
    8783 213  99      10
    8855 166 213    14.1
    end
    format %td Date
    label values tarnatcode natcodes
    label values taracqcode natcodes
    label def natcodes 22 "BH", modify
    label def natcodes 25 "BL", modify
    label def natcodes 36 "CA", modify
    label def natcodes 40 "CE", modify
    label def natcodes 71 "FR", modify
    label def natcodes 153 "NT", modify
    label def natcodes 166 "PR", modify
    label def natcodes 172 "RU", modify
    label def natcodes 174 "SA", modify
    label def natcodes 194 "SW", modify
    label def natcodes 196 "SZ", modify
    label def natcodes 211 "UK", modify
    label def natcodes 213 "US", modify
    label def natcodes 219 "WG", modify
    label def natcodes 12 "AS", modify
    label def natcodes 13 "AU", modify
    label def natcodes 99 "IS", modify
    label def natcodes 100 "IT", modify
    label def natcodes 105 "JP", modify
    label def natcodes 119 "LX", modify
    label def natcodes 150 "NO", modify
    label def natcodes 206 "TW", modify

  • #2
    Is it that you want to change the format of your date - the way they are displayed on output - or is it that you want to change the value of your date - so that every day in 1984 is the same value, for example, when you use it in a regression?

    The date you have is a Stata Internal Format (SIF) daily date, and when displayed using a %td format - an appropriate format for a daily date - displays as you have seen.

    Changing the format to %ty is not appropriate, because %ty is intended for SIF yearly dates, which is not what you have. Changing the format does not change the value.
    Code:
    generate Year = yofd(Date)
    format Year %ty
    will create a new variable which will be the year corresponding to the daily Date.

    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


    • #3
      Stop working on what you're doing. You need to invest some time in learning about Stata's handling of date-time variables. Run -help datetime- and then click on the link near the top of that page called (View complete PDF manual entry). Read the entire thing. Yes, it will take a long time, and you won't remember it all. But you will learn the basics of how Stata represents dates and times internally, and the ways in which you can transform among them, and the various ways you can alter the way the results are displayed for human eyes in Stata output and in the Browser.

      In particular, -format Date %ty- is just an invalid format. There is no %ty. You could do -format Date %tdy-, in which case when you look at the Date variable in -list- or -browse-, Stata will show you only the last two digits of the year and suppress the other information. But it does not change what Stata stores internally, and if you try to, for example, compare two different values of Date that are the same year but are different dates within that year, they will not compare as equal. More likely what you want is to create a new variable that extracts the year from the date. The code for that is:

      Code:
      gen year = yofd(Date)
      And since year is created as just the ordinary numeric representation of the year, you don't have to apply any special formatting to make sense of it.

      As for the value of Date in -dataex- not looking like your dates, that is exactly what is supposed to happen. -dataex- outputs the numbers that Stata uses to represent the dates internally. If you look closely you will also see that the -dataex- output includes a -format Date %td- command that causes Stata to display them in the %td format (which is exemplified by 31dec1984). That's why we are so insistent on people using -dataex- to show example data on this Forum: -dataex- gives you the actual information that your Stata data set has, both underlying and display formatting. By contrast if a Forum member just types 31dec1984 in a post, those who try to help cannot know if, in Stata, that is a string "31dec1984" or if it is, as in your case, the number 9131 as it appears when formatted with %td. And for most purposes, the difference is critical.

      So, seriously, don't just plow ahead and hope you don't mess things up. Clearly your line of research here is going to require facility with date-time variables. So take the time to learn it before you proceed farther.

      Added: Crossed with #2 which makes essentially the same points more concisely.

      Comment


      • #4
        Thank you for your reply. I am writing my bachelor thesis and have strict deadlines and a short time frame to do everything. I am struggling with my dataset, adjusting and merging them to do the actual analysis. I think I chose a difficult subject, so I don't really have time now to read into the difficult time-date subject for now. I am really glad with your help though!!
        Last edited by Eszti Bambacht; 30 Apr 2018, 13:21.

        Comment


        • #5
          And yes, I want all the dates to be the same, so I will create a new variable.

          Comment

          Working...
          X