Announcement

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

  • person-time at risk

    Hi all

    I have the following dataset.
    ID Date
    1 1/2/2013
    1 4/2/2013
    1 9/2/2013
    1 10/2/2013
    2 4/2/2013
    2 9/2/2013
    2 15/2/2013
    2 19/2/2013
    3 2/3/2013
    3 9/3/2013
    I want to calculate the person-time at risk on this dataset.
    My calculation by hand is "the number of days between 1/2/2013 and 10/2/2013 =9 + the number of days between 4/2/2013 and 15/2/2013 =11 +the number of days between 2/3/2013 and 9/2/2013 =7), so in total, the amount of person time at risk is 9+11+7 = 27

    Just wondering how to calculate that by using stata?

    Many thanks

  • #2
    So first, your Date variable has to be a numeric Stata internal date variable, not a string that looks like a date to human eyes. If you have your date as a string variable, you must first convert it using the daily() function (see -help daily()-).

    With that out of the way:

    Code:
    by ID (Date), sort: gen days_at_risk_this_person = Date[_N] - Date[1]
    egen flag = tag(ID)
    summarize days_at_risk_this_person if flag, meanonly
    display "Total person-days at risk = `r(sum)'"
    Last edited by Clyde Schechter; 03 Mar 2016, 18:06. Reason: Correct error in code.

    Comment


    • #3

      I'm not sure how your date variable is stored, so I'm going to start with it as a string and then convert it to a date variable just in case.

      Here's a way to do what you wish:


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float id str16 date
      1 "1/2/2013"
      1 "4/2/2013"
      1 "9/2/2013"
      1 "10/2/2013"
      2 "4/2/2013"
      2 "9/2/2013"
      2 "15/2/2013"
      2 "19/2/2013"
      3 "2/3/2013"
      3 "9/3/2013"
      end
      
      
      gen date2=date(date, "DMY")
      format %td date2
      
      *this subtracts the last date from the first in each id group
      bysort id: gen time=date2[_N]-date2[1] if _n==_N
      
      *this gets the total across groups
      egen tot=total(time)

      By the way, I get a different result because the last date is 19/2/2013 for the 2nd ID, making the number of days 15.

      Last edited by Carole J. Wilson; 03 Mar 2016, 18:13. Reason: Edited for typo
      Stata/MP 14.1 (64-bit x86-64)
      Revision 19 May 2016
      Win 8.1

      Comment


      • #4
        Hi
        But when I use "bysort id: gen time=date2[_N]-date2[1] if _n==_N"
        I then generated all missing values.
        Did I do something wrong here?

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          So first, your Date variable has to be a numeric Stata internal date variable, not a string that looks like a date to human eyes. If you have your date as a string variable, you must first convert it using the daily() function (see -help daily()-).

          With that out of the way:

          Code:
          by ID (Date), sort: gen days_at_risk_this_person = Date[_N] - Date[1]
          egen flag = tag(ID)
          summarize days_at_risk_this_person if flag, meanonly
          display "Total person-days at risk = `r(sum)'"
          I tried this method as well.
          When I used " by id ( date2 ), sort: gen days_at_risk_this_person=date2 [_N] - date2 [1]"
          I had an error message - "weights not allowed"

          Did I do something wrong here?

          Comment


          • #6
            It is essential that you type the code EXACTLY how it is written. There should be no space between the variable name and the open braces:

            This is correct:
            by id ( date2 ), sort: gen days_at_risk_this_person=date2[_N] - date2[1]

            This is NOT:
            by id ( date2 ), sort: gen days_at_risk_this_person=date2 [_N] - date2 [1]
            Stata/MP 14.1 (64-bit x86-64)
            Revision 19 May 2016
            Win 8.1

            Comment


            • #7
              Carole is absolutely right. You must reproduce the code exactly. Variations that don't catch your eye can be errors. Actually, the safest thing is to use your computer's copy and paste functions to transfer the code from the forum directly into a do-file and run it from there without editing it.

              Comment

              Working...
              X