Announcement

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

  • YYYYMM date to "ym" Stata

    Hi,

    I have a data variable that has the YYYYMM format. Is there a 1 line function that creates a "ym" stata variable? I have tried "date()" but does not seem to suit my need. Thanks!

  • #2
    Indeed. as date() is for producing daily dates. "Format" here is ambiguous as what you cite is not a Stata display format and doesn't make clear whether you have a numeric or string variable.

    Here's some technique:

    Code:
    . clear 
    
    . set obs 1
    number of observations (_N) was 0, now 1
    
    . gen ndate = 201606
    
    . gen sdate = "201606"
    
    . gen ndate2 = ym(floor(ndate/100), mod(ndate, 100))
    
    . gen sdate2 = ym(real(substr(sdate, 1, 4)), real(substr(sdate, -2,2)))
    
    . format *2 %tm
    
    . l
    
         +-----------------------------------+
         |  ndate    sdate   ndate2   sdate2 |
         |-----------------------------------|
      1. | 201606   201606   2016m6   2016m6 |
         +-----------------------------------+

    Comment

    Working...
    X