Announcement

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

  • Replace values for all rows of a study ID, based on a value in a single row.

    Hello, I am very new to stata and trying to teach myself how to use it. I wanted to determine if there is a way to generate a new variable, or replace an existing variable for all rows relevant to a specific patient ID, based on an observation from a single row.
    ID var1 visit newvar
    1 0 1 0
    1 0 2 0
    1 0 3 0
    1 1 4 0
    1 0 5 0
    2 0 1 0
    2 1 2 0
    2 0 3 0
    2 0 4 0
    So if the observation for Study ID #1 in visit 5 is 1, how can I replace the value for newvar in all rows for patient #1 if var1==1 at visit 5.

    Hopefully this makes sense.
    Thank you so much.

  • #2
    Nate:
    the following toy-example considers that -1- for -var1-, -ID1- is reported at -visit- #4 (not #5):
    Code:
    . bysort ID (visit): replace newvar=1 if var1[4]==1
    
    
    . list
    
         +----------------------------+
         | ID   var1   visit   newvar |
         |----------------------------|
      1. |  1      0       1        1 |
      2. |  1      0       2        1 |
      3. |  1      0       3        1 |
      4. |  1      1       4        1 |
      5. |  1      0       5        1 |
         |----------------------------|
      6. |  2      0       1        0 |
      7. |  2      1       2        0 |
      8. |  2      0       3        0 |
      9. |  2      0       4        0 |
         +----------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Perhaps this is what you want. Note that for clarity I changed the observation of var1 for id 2 at visit 2 to 7. This assumes that var1 is 0 except for one observation when it has a positive value.
      Code:
      . * Example generated by -dataex-. For more info, type help dataex
      . clear
      
      . input int(id var1 visit)
      
                 id      var1     visit
        1. 1 0 1
        2. 1 0 2
        3. 1 0 3
        4. 1 1 4
        5. 1 0 5
        6. 2 0 1
        7. 2 7 2
        8. 2 0 3
        9. 2 0 4
       10. end
      
      . 
      . bysort id (visit): egen newvar = max(var1)
      
      . list, sepby(id)
      
           +----------------------------+
           | id   var1   visit   newvar |
           |----------------------------|
        1. |  1      0       1        1 |
        2. |  1      0       2        1 |
        3. |  1      0       3        1 |
        4. |  1      1       4        1 |
        5. |  1      0       5        1 |
           |----------------------------|
        6. |  2      0       1        7 |
        7. |  2      7       2        7 |
        8. |  2      0       3        7 |
        9. |  2      0       4        7 |
           +----------------------------+
      
      .

      Comment

      Working...
      X