Announcement

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

  • Months since a var=1

    Months s

  • #2
    Hello, sorry, I hit post too soon. So, I have a variable that=1 if someone has a call in a certain month year (it is called ref_this_month). Example below--data is at month-year-person level (mci is person id). I want to create a variable that counts how many months from the FIRST time ref_this_month=1 for a person. So, for instance, for the ID below, the first observation for 526 would show a -1 (since 2018 m1 is 1 month before he gets a ref_this_month==1) and 2018 m3 observation would equal 1. However, there may be more than one ref_this_month==1 and I want it to only count from the first instance this occurs.Thanks -CJ

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long mci float(month yr ref_this_month)
    526  1 2018 0
    526  2 2018 1
    526  3 2018 0
    526  4 2018 0
    526  5 2018 0
    end

    Comment


    • #3
      Code:
      //  FIRST CREATE A STATA INTERNAL FORMAT MONTHLY DATE VARIABLE
      gen int mdate = ym(yr, month)
      format mdate %tm
      
      //  IDENTIFY MONTH OF FIRST REFERENCE
      by mci (mdate), sort: egen int month_first_ref = min(cond(ref_this_month, mdate, .))
      format month_first_ref %tm
      
      //  CALCULATE TIME TO OR SINCE MONTH FIRST REF
      gen wanted = mdate - month_first_ref

      Comment


      • #4
        Thank you CLyde!

        Comment

        Working...
        X