Announcement

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

  • Creating new variable which sums up the values of another variable

    Hi,

    I would like to calculate the total number of months spent in prison by summing up the time in months spent for each unique “episode” in prison. I have three variables
    • Unique person ID
    • Prison episode
    • The number of months spent in prison
    The problem is that I do not want to recount the months. For e.g. individual B had two episodes in prison, in the first episode they spent 10 months and in the second episode they spent a total of 16 months (4 months when they were observed the second time and then 16 months by the time, they were observed a third time). I am not sure how to tell Stata to take the maximum “months in prison” value for each unique episode in prison and add them up.

    I have tried the following:

    Code:
    bysort ID prison_ep: egen max=max(months)
    which creates a maximum column, but I am not sure how to then add up the total month without double counting.

    The column I am trying to produce is total_months (I have manually calculated it in the data example below to demonstrate)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 ID byte(prison_ep Months) float(max total_months)
    "a" 1  2 26 26
    "a" 1 14 26 26
    "a" 1 26 26 26
    "b" 1 10 10 26
    "b" 2  4 16 26
    "b" 2 16 16 26
    "c" 1  6  6  6
    "d" 1 10 10 25
    "d" 2 10 10 25
    "d" 3  2  5 25
    "d" 3  5  5 25
    end

  • #2
    Code:
    bys ID: egen sum_months = sum(cond(Months==max),Months,.)

    Comment


    • #3
      Thank you, George.

      Comment


      • #4
        George Ford gave good advice; note, however, that as from Stata 9 the equivalent function is documented as total(). But sum() continues to work.

        Comment

        Working...
        X