Announcement

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

  • Desired date display format

    So, I have month (only two values 06 & 12) and year variables.
    I want to create a date variable that should look like this: Jun2009, Dec2009, Jun2010, Dec2010 and so on.

    Code:
    gen strMonth = string(month, "%02.0f")
    gen strYear = string(year)
    gen strDate = strMonth + "/" + strYear
    gen date = monthly(strDate, "MY")
    format date %td
    However, it generates date values that look like:
    Code:
    2009m6
    I appreciate your help. Thanks.

  • #2
    Code:
    format date %tmMonCCYY
    You created your variable date with the -monthly()- function. As such, it is a Stata internal format monthly date variable, so nothing good can come of putting a %td format on it. It needs some kind of %tm format to even end up in the right century! -help datetime display format- gives all the details of the various ways to format any kind of Stata internal format date variable.

    As an aside, to reduce the likelihood of errors like putting %td formats on monthly date variables, I recommend against using date as a variable name when the variable is not actually a full date, i.e. with day month and year (in whatever order). I reserve the name date for variables that are truly internal format date variables that will display correctly with a %td format. I use mdate for monthly dates and qdate for quarterly dates. I suppose I would also use wdate for weekly and hdate for half-yearly dates, but these never seem to arise in my workflow. I also use clock or dttm for Stata datetime (clock) variables. By giving variables names that say what they really are, I make fewer errors of applying functions or formats appropriate for one kind of date variable to variables that are a different kind.

    Comment


    • #3
      If you have data for just June and December in various years then you have half-yearly dates, except that Stata is inclined to think that the half-years correspond to January and July.

      .
      Code:
       di %thMonCCYY yh(2009, 1)
      Jan2009
      
      . di %thMonCCYY yh(2009, 2)
      Jul2009
      So, I would stick with monthly dates except that you may need to specify to tsset or xtset at some point delta(6). Otherwise Stata commands will see your data as mostly gaps because June and December are naturally 6 months apart.

      Code:
      . di ym(2009, 6)
      593
      
      . di ym(2009, 12)
      599

      Comment


      • #4
        Hi Clyde,

        Thanks a lot. That worked. Sorry, I had actually used %tm not %td. But I had not specified MonCCYY after that, which did the trick. So, now I am getting Jun2009, Dec2009 and so on.

        Comment


        • #5
          Hi Nick,

          Thanks a lot. Just to get more clarity on your comment, it is not advisable to use the half-yearly format here since Stata will misconstrue the months Jun and Dec as Jan and Jul. So, I should just stick to monthly format and do:

          Code:
          format date %tmMonCCYY

          and then:

          Code:
          . xtset countyfips date, delta(6)
          
          Panel variable: countyfips (unbalanced)
           Time variable: date, Jun2009 to Dec2021
                   Delta: 6 months

          Comment


          • #6
            I would just say that half-yearly would be fine unless you want display of which months are concerned, in which case any date display format with months will be wrong. You could still get good displays by defining value labels, but users can't define their own display formats if not provided. Sample code here; the loop could be avoided by using labmask from the Stata Journal


            Code:
            clear 
            set obs 26
            egen year = seq(), from(2009) to(2021) block(2)
            gen half = 2 - mod(_n, 2)
            gen month = 6 * half 
            
            gen mdate = ym(year, month)
            format mdate %tm
            gen hdate = yh(year, half)
            gen label = word("Jun Dec", half) 
            
            forval i = 1/`=_N' { 
                local label = label[`i'] + " " + strofreal(year[`i'])
                label define myhdate `=hdate[`i']' "`label'", add 
            }
            
            label val hdate myhdate 
            
            list , sepby(year)
            
                +--------------------------------------------------+
                 | year   half   month     mdate      hdate   label |
                 |--------------------------------------------------|
              1. | 2009      1       6    2009m6   Jun 2009     Jun |
              2. | 2009      2      12   2009m12   Dec 2009     Dec |
                 |--------------------------------------------------|
              3. | 2010      1       6    2010m6   Jun 2010     Jun |
              4. | 2010      2      12   2010m12   Dec 2010     Dec |
                 |--------------------------------------------------|
              5. | 2011      1       6    2011m6   Jun 2011     Jun |
              6. | 2011      2      12   2011m12   Dec 2011     Dec |
                 |--------------------------------------------------|
              7. | 2012      1       6    2012m6   Jun 2012     Jun |
              8. | 2012      2      12   2012m12   Dec 2012     Dec |
                 |--------------------------------------------------|
              9. | 2013      1       6    2013m6   Jun 2013     Jun |
             10. | 2013      2      12   2013m12   Dec 2013     Dec |
                 |--------------------------------------------------|
             11. | 2014      1       6    2014m6   Jun 2014     Jun |
             12. | 2014      2      12   2014m12   Dec 2014     Dec |
                 |--------------------------------------------------|
             13. | 2015      1       6    2015m6   Jun 2015     Jun |
             14. | 2015      2      12   2015m12   Dec 2015     Dec |
                 |--------------------------------------------------|
             15. | 2016      1       6    2016m6   Jun 2016     Jun |
             16. | 2016      2      12   2016m12   Dec 2016     Dec |
                 |--------------------------------------------------|
             17. | 2017      1       6    2017m6   Jun 2017     Jun |
             18. | 2017      2      12   2017m12   Dec 2017     Dec |
                 |--------------------------------------------------|
             19. | 2018      1       6    2018m6   Jun 2018     Jun |
             20. | 2018      2      12   2018m12   Dec 2018     Dec |
                 |--------------------------------------------------|
             21. | 2019      1       6    2019m6   Jun 2019     Jun |
             22. | 2019      2      12   2019m12   Dec 2019     Dec |
                 |--------------------------------------------------|
             23. | 2020      1       6    2020m6   Jun 2020     Jun |
             24. | 2020      2      12   2020m12   Dec 2020     Dec |
                 |--------------------------------------------------|
             25. | 2021      1       6    2021m6   Jun 2021     Jun |
             26. | 2021      2      12   2021m12   Dec 2021     Dec |
                 +--------------------------------------------------+

            Comment


            • #7
              That looks great! Thanks a lot Nick!

              Comment

              Working...
              X