Announcement

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

  • Creating grouping variable in long format

    Hello, I am using Stata 15

    I am trying to create a grouping variable that indicates whether participants were seen within days_to_dt1_pt <15 days , 15- 90 days and > 90 days. I am interested in only the days to the first treatment days_to_dt1_lpt). So I am not interested, for example, in the 16 and 30 for participant 1 as they would be categorized as < 15 . I want to only have one possible category for each participant. I can not figure out how to do this without creating a value for each row of data. For participant 2, they would be in the 15-90 day category but it would be recorded twice--again what I don't want.

    My data look like:

    id month days_to_ dt1_pt
    1 1 10
    1 2 16
    1 3 30
    1 4 .
    1 5 .
    2 1 .
    2 2 46
    2 3 50
    2 4 .

    What I tired is to create a 1 for each time the patient is in one of the categories. But as you can see below, code 2 would return a value for month 2 and 3 which i don't want. I am not sure how to handle this.

    ​​​​​​capture drop
    gen early_vs_delay = .
    replace early_vs_delay = 1 if days_to_dt1_pt < 15

    capture drop early_vs_delay_1
    gen early_vs_delay_1 = .
    replace early_vs_delay_1 = 1 if days_to_dt1_pt > 14 & days_to_dt1_pt < 91

    capture drop early_vs_delay_2
    gen early_vs_delay_2 = .
    replace early_vs_delay_2 = 1 if days_to_dt1_pt > 90 & days_to_dt1_pt < 1000


    Then I tried to combine:

    capture drop early_vs_delay_3 // this is to creatE a variable from combining the ones for above.
    gen early_vs_delay_3 = .
    replace early_vs_delay_3 = 1 if early_vs_delay ==1 // 2 week or less
    replace early_vs_delay_3 = 2 if early_vs_delay_1 == 1 // 2 weeks to 90 days
    replace early_vs_delay_3 = 0 if early_vs_delay_2 == 1

    Please let me know if this information is sufficient or more detail is needed.

    Thanks.

    Jake

  • #2
    You can do this with
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id month days_to_dt1_pt)
    1 1 10
    1 2 16
    1 3 30
    1 4  .
    1 5  .
    2 1  .
    2 2 46
    2 3 50
    2 4  .
    end
    
    by id, sort: egen min_days_to_dt1_pt = min(days_to_dt1_pt)
    gen wanted = 1 if min_days_to_dt1_pt < 15
    replace wanted = 2 if inrange( min_days_to_dt1_pt, 15, 90)
    replace wanted = 3 if min_days_to_dt1_pt > 90 & !missing(min_days_to_dt1_pt)
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Clyde, thanks for the help and the information about dataex. I will use that from now on. The code worked perfectly!

      Comment

      Working...
      X