Announcement

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

  • recode two continuous variables into one categorical(3 categories) variable

    I am new to Stata and I am trying to recode two continuous variables into one categorical(3 categories) variable. I pasted data from dataex below. The continuous variables are "vigmin" and "modmin"(see definition below).

    Vigmin = the number minutes someone participated in vigorous activity in a week

    Modmin = the number minutes someone participated in moderate activity in a week

    “.” = the period is treated as 0 minutes of activity, so any “.” should equal “poor(3).

    Desired categories for the new categorical variable:

    Ideal(1) = 150 minutes or greater of moderate physical activity per week, or 75 minutes or greater of vigorous activity per week

    Intermediate(2) = 1 – 149 minutes of moderate physical activity per week, or 1 - 74 minutes of vigorous activity per week

    Poor(3) = 0 minutes of activity


    This is the syntax that I tried; I did not get an error, but the numbers in my output were not accurate. Should I be using different syntax?
    gen idealPA=0
    replace idealPA=1 if (modmin >=150) | (vigmin >=75)
    replace idealPA=2 if (modmin >=1) & (modmin <=149)
    replace idealPA=2 if (vigmin >=1) & (vigmin <=74)
    replace idealPA=3 if (modmin==.) | (vigmin==.)
    label define idealPA 1 "ideal" 2 "intermediate" 3 "Poor"
    label value idealPA idealPA
    ta idealPA RCTgroup, row col chi


    (example data from Dataex)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(modmin vigmin)
    105  50
      .   .
     60   .
    180  30
     20   .
     60   .
     60   .
     30   .
     90   .
      .   .
      .   .
    180   .
     60   .
     60   .
    180   .
      .   .
     90  20
     60   .
     15  15
     30   .
     60   .
     60   .
    120   .
     40   .
     60 180
     45   .
      .   .
     10   .
     90   .
     60  20
     30  35
     40   .
     30  60
     60  30
    180 105
     25   .
    180  60
     60 120
    120  30
    120   .
      .  10
    120   .
    120 120
     40 180
     60  30
    180   .
    180  20
     60   .
     25   .
    180  30
     30   .
     60  60
     30   .
      .   .
    150   .
     10 180
      .   .
     20  15
    120   .
     30   .
     90  10
    180   .
     60   .
     30  30
      .  10
     30   .
     30 180
     30   .
     60   .
     90  90
     60  10
     20  10
     20  15
     45  60
     25   .
    180   .
     60 120
    120  90
     15   .
     15   .
     15  10
    180  60
      .   .
      .   .
     30   .
      .   .
     75   .
      .   .
      .   .
     10   .
     10   .
      .   .
      .   .
      .   .
     20  10
     10   .
      .   .
      .   .
     25   .
      .   .
    end




    Thanks in advance for any assistance.

  • #2
    So I suspect three things are causing you problems:

    1) "replace idealPA=3 if (modmin==.) | (vigmin==.)" should be "replace idealPA=3 if (modmin==.) & (vigmin==.)"
    2) I would go from poor to intermed to ideal. Under your current order, a person with 60 min of moderate exercise and 120 min of vigorous exercise would get recoded to intermediate when you really want them to get coded as ideal.
    3) Because missing are counted as higher than any number things like "replace idealPA=1 if modmin >=150" will equal 1 if modmin==.

    Code:
    gen id=_n
    order id, first
    
    gen ideal==0
    replace ideal = 1 if modmin>=150 & modmin!=.
    replace ideal = 1 if vigmin>=75 & vigmin!=.
    
    gen intermed = 0
    replace intermed = 1 if ideal==0 & modmin>=1 & modmin <=149
    replace intermed = 1 if ideal==0 & vigmin>=1 & vigmin<=74
    
    gen poor=0
    replace poor=1 if modmin==. & vigmin==.
    
    gen idealPA = 1 if ideal==1
    replace idealPA =2 if intermed==1
    replace idealPA =3 if poor==1
    
    . tabulate idealPA
    
        idealPA |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |         21       21.00       21.00
              2 |         61       61.00       82.00
              3 |         18       18.00      100.00
    ------------+-----------------------------------
          Total |        100      100.00
    
    
    . list if idealPA==1, noobs
    
      +----------------------------------------------------------+
      | id   modmin   vigmin   ideal   intermed   poor   idealPA |
      |----------------------------------------------------------|
      |  4      180       30       1          0      0         1 |
      | 12      180        .       1          0      0         1 |
      | 15      180        .       1          0      0         1 |
      | 25       60      180       1          0      0         1 |
      | 35      180      105       1          0      0         1 |
      |----------------------------------------------------------|
      | 37      180       60       1          0      0         1 |
      | 38       60      120       1          0      0         1 |
      | 43      120      120       1          0      0         1 |
      | 44       40      180       1          0      0         1 |
      | 46      180        .       1          0      0         1 |
      |----------------------------------------------------------|
      | 47      180       20       1          0      0         1 |
      | 50      180       30       1          0      0         1 |
      | 55      150        .       1          0      0         1 |
      | 56       10      180       1          0      0         1 |
      | 62      180        .       1          0      0         1 |
      |----------------------------------------------------------|
      | 67       30      180       1          0      0         1 |
      | 70       90       90       1          0      0         1 |
      | 76      180        .       1          0      0         1 |
      | 77       60      120       1          0      0         1 |
      | 78      120       90       1          0      0         1 |
      |----------------------------------------------------------|
      | 82      180       60       1          0      0         1 |
      +----------------------------------------------------------+
    
    
    
     list if idealPA==3, noobs
    
      +-----------------------------------------------------------+
      |  id   modmin   vigmin   ideal   intermed   poor   idealPA |
      |-----------------------------------------------------------|
      |   2        .        .       0          0      1         3 |
      |  10        .        .       0          0      1         3 |
      |  11        .        .       0          0      1         3 |
      |  16        .        .       0          0      1         3 |
      |  27        .        .       0          0      1         3 |
      |-----------------------------------------------------------|
      |  54        .        .       0          0      1         3 |
      |  57        .        .       0          0      1         3 |
      |  83        .        .       0          0      1         3 |
      |  84        .        .       0          0      1         3 |
      |  86        .        .       0          0      1         3 |
      |-----------------------------------------------------------|
      |  88        .        .       0          0      1         3 |
      |  89        .        .       0          0      1         3 |
      |  92        .        .       0          0      1         3 |
      |  93        .        .       0          0      1         3 |
      |  94        .        .       0          0      1         3 |
      |-----------------------------------------------------------|
      |  97        .        .       0          0      1         3 |
      |  98        .        .       0          0      1         3 |
      | 100        .        .       0          0      1         3 |
      +-----------------------------------------------------------+
    
    
    . list if idealPA==2 & id<=22, noobs
    
      +----------------------------------------------------------+
      | id   modmin   vigmin   ideal   intermed   poor   idealPA |
      |----------------------------------------------------------|
      |  1      105       50       0          1      0         2 |
      |  3       60        .       0          1      0         2 |
      |  5       20        .       0          1      0         2 |
      |  6       60        .       0          1      0         2 |
      |  7       60        .       0          1      0         2 |
      |----------------------------------------------------------|
      |  8       30        .       0          1      0         2 |
      |  9       90        .       0          1      0         2 |
      | 13       60        .       0          1      0         2 |
      | 14       60        .       0          1      0         2 |
      | 17       90       20       0          1      0         2 |
      |----------------------------------------------------------|
      | 18       60        .       0          1      0         2 |
      | 19       15       15       0          1      0         2 |
      | 20       30        .       0          1      0         2 |
      | 21       60        .       0          1      0         2 |
      | 22       60        .       0          1      0         2 |
      |----------------------------------------------------------|
    
    
    *** Just checking for no overlap between categories
    . tabulate idealPA ideal
    
               |         ideal
       idealPA |         0          1 |     Total
    -----------+----------------------+----------
             1 |         0         21 |        21
             2 |        61          0 |        61
             3 |        18          0 |        18
    -----------+----------------------+----------
         Total |        79         21 |       100
    
    
    . tabulate idealPA intermed
    
               |       intermed
       idealPA |         0          1 |     Total
    -----------+----------------------+----------
             1 |        21          0 |        21
             2 |         0         61 |        61
             3 |        18          0 |        18
    -----------+----------------------+----------
         Total |        39         61 |       100
    
    
    . tabulate idealPA poor
    
               |         poor
       idealPA |         0          1 |     Total
    -----------+----------------------+----------
             1 |        21          0 |        21
             2 |        61          0 |        61
             3 |         0         18 |        18
    -----------+----------------------+----------
         Total |        82         18 |       100

    Comment


    • #3
      Dear David,

      Thank you for spending the time to respond to my question. I ran your code and it seems like everything checks out. I did forget one aspect though, although I think I was able to get it to work properly using your syntax.

      Participants will also be considered in the “ideal” category if they have any combination of moderate and vigorous activity that equals 150 or more per week(60 vigmin + 90 modmin). I generated a new variable called combinedmin by adding modmin and vigmin. I then added combinedmin to the syntax following the format that you suggested. See the addition below.

      The only piece of your syntax that did not give me the same output as you was the list if idealPA==1, noobs ; list if idealPA==3, noobs ; list if idealPA==2 & id<=22, noobs. When I ran those codes I get an error message( == invalid name). Any idea why that would be?








      gen combinedmin = modmin+vigmin

      gen ideal=0
      replace ideal = 1 if modmin>=150 & modmin!=.
      replace ideal = 1 if vigmin>=75 & vigmin!=.
      replace ideal = 1 if combinedmin>=150 & combinedmin!=.

      gen intermed = 0
      replace intermed = 1 if ideal==0 & modmin>=1 & modmin <=149
      replace intermed = 1 if ideal==0 & vigmin>=1 & vigmin<=74
      replace intermed = 1 if ideal==0 & combinedmin>=1 & combinedmin<=149

      gen poor=0
      replace poor=1 if modmin==. & vigmin==. & combined==.

      gen idealPA = 1 if ideal==1
      replace idealPA =2 if intermed==1
      replace idealPA =3 if poor==1



      Thanks again!

      Comment


      • #4
        Here's some different syntax.

        Code:
        gen ideal = inrange(modmin, 150, .) /// 
        | inrange(vigmin, 75, .)            ///
        | inrange(modmin + vigmin, 150, .) 
        
        gen intermed = ideal == 0 &         /// not ideal 
        (                                   /// plus one of more of 
        inrange(modmin, 1, 149) |           /// these 
        inrange(vigmin, 1, 74)  |           /// three 
        inrange(modmin + vigmin, 1, 149)    /// conditions 
        ) 
        
        * if both are missing, their sum is too 
        gen poor =  modmin == . & vigmin == . 
        
        gen idealPA = cond(ideal, 1, cond(intermed, 2,  cond(poor, 3, .)))

        Comment


        • #5
          Thank you Nick for giving me another way to accomplish this task. I will try this as well and post if I run into any issues.

          Comment

          Working...
          X