Announcement

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

  • Month and year

    Hello, I have this data in Stata. I want to extract the month and year variables from a string variable. The first number before _ '' represents the year and the number after '' represents the month variable. Thank you.




    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 month
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    "2021_02"
    "2021_03"
    "2021_04"
    "2021_05"
    end

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 month
    "2021_02"
    end
    
    gen mdate = monthly(month, "YM")
    format mdate %tm
    
    split month, parse("_") destring
    rename (month?) (year mon)
    
    list
    
         +-------------------------------+
         |   month    mdate   year   mon |
         |-------------------------------|
      1. | 2021_02   2021m2   2021     2 |
         +-------------------------------+
    
    .

    Comment

    Working...
    X