Announcement

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

  • Getting end date of the month

    Dear all,

    Suppose I have the following dataset
    Code:
    clear
    input str8 yearmonth 
    "1960m1"
    "1960m2"
    "1960m3"
    "1960m4"
    "1960m5"
    "1960m6"
    end
    I'd like to obtain the end date of the month (e.g 31jan1960 for 1960m1).

    Could anyone please help me with this?

    Many thanks,

    Vinh

  • #2
    The end of each month is (surprise) one day before the first day of the next month. So, noting that a string date is not much use until converted,


    Code:
    clear
    input str8 yearmonth 
    "1960m1"
    "1960m2"
    "1960m3"
    "1960m4"
    "1960m5"
    "1960m6"
    end
    
    gen wanted = dofm(monthly(yearmonth, "YM") + 1) - 1 
    format wanted %td 
    
    list , sep(0) 
    
    
         +----------------------+
         | yearmo~h      wanted |
         |----------------------|
      1. |   1960m1   31jan1960 |
      2. |   1960m2   29feb1960 |
      3. |   1960m3   31mar1960 |
      4. |   1960m4   30apr1960 |
      5. |   1960m5   31may1960 |
      6. |   1960m6   30jun1960 |
         +----------------------+
    Evidently this identity copes with months of unequal length, including in leap years, as Stata knows the (Western) calendar so you don't have to.

    Comment


    • #3
      Dear Nick,

      Thank you for your help. That is very helpful.

      Cheers,

      Vinh

      Comment

      Working...
      X