Announcement

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

  • Generate Month and Year variables

    Hello everyone,

    I have an example of my data here. What I have here is an ID variable, the last four digits are a year, and the first one (or two) digits are the month. For example, ID "12000" means Jan 2000 or "112001" means Nov 2000. How can I create a new month-year variable based on the ID variable? And how can put them in ascending order? Thank you.

    Code:
    input ID 
    12000
    12002
    22000
    112001
    end

  • #2
    Code:
    clear 
    input ID 
    12000
    12002
    22000
    112001
    end
    
    gen year = mod(ID, 1e4) 
    gen month = floor(ID/1e4)
    gen mdate = ym(year, month)
    format mdate %tm 
    sort mdate 
    list 
    
         +---------------------------------+
         |     ID   year   month     mdate |
         |---------------------------------|
      1. |  12000   2000       1    2000m1 |
      2. |  22000   2000       2    2000m2 |
      3. | 112001   2001      11   2001m11 |
      4. |  12002   2002       1    2002m1 |
         +---------------------------------+

    Comment


    • #3
      Thank you Nick, stay safe.

      Comment

      Working...
      X