Search code examples
statadata-analysisdata-science

How to calculate month between two dates in stata


I want to create a variable with the age of credit. The data only has the date of the start of credit.

I create date variable (eg 2017-12-31) for default. Then, i want calculate age with date of the start credit.

I tried to search for an article about that, but no luck.

Thanks.


Solution

  • It seems like your data is daily. In that case what you need is:

    gen current_date=date("20171231","YMD")
    format current_date %td //this will be the variable from which age will be calculated
    gen age=current_date-start_credit_date //again, assuming the start credit variable is daily
    

    this gives the age variable as the number of days. If you want it as the number of months, you need to add:

    gen current_month=mofd(current_date)
    format current_month %tm
    gen start_month=mofd(start_credit_date)
    format start_month %tm
    gen age_month=current_month-start_month