Announcement

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

  • Create a new variable from existing list of variables.

    I have four variables (var1-var4) each coded as 0/1. These four variables are mutually exclusive categories of a categorical variable ("dis_type") such that an individual can take one of four values (1-4). How can I generate the variable "dis_type" taking values 1 to 4 from the varlist "var1-var4". The dataset is as below:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(var1 var2 var3 var4) float id
    1 0 0 0  1
    1 0 0 0  2
    0 0 0 0  3
    0 0 0 1  4
    0 1 0 0  5
    0 0 0 0  6
    0 0 1 0  7
    1 0 0 0  8
    0 0 1 0  9
    0 0 0 0 10
    1 0 0 0 11
    1 0 0 0 12
    0 0 1 0 13
    0 0 0 1 14
    0 0 0 0 15
    0 0 0 1 16
    0 0 0 0 17
    0 1 0 0 18
    0 0 1 0 19
    0 0 1 0 20
    1 0 0 0 21
    1 0 0 0 22
    0 0 0 0 23
    0 0 0 0 24
    0 0 1 0 25
    0 0 0 0 26
    0 0 0 1 27
    0 0 0 0 28
    0 0 0 0 29
    0 0 0 0 30
    1 0 0 0 31
    0 1 0 0 32
    1 0 0 0 33
    1 0 0 0 34
    1 0 0 0 35
    1 0 0 0 36
    1 0 0 0 37
    0 1 0 0 38
    1 0 0 0 39
    1 0 0 0 40
    1 0 0 0 41
    1 0 0 0 42
    0 0 1 0 43
    0 0 1 0 44
    1 0 0 0 45
    0 0 1 0 46
    1 0 0 0 47
    0 1 0 0 48
    0 0 0 1 49
    0 0 1 0 50
    0 0 1 0 51
    0 0 1 0 52
    0 0 0 0 53
    1 0 0 0 54
    0 0 1 0 55
    0 0 0 1 56
    0 0 0 1 57
    1 0 0 0 58
    0 0 0 1 59
    0 0 0 0 60
    0 0 0 1 61
    0 0 1 0 62
    0 0 0 1 63
    0 0 0 0 64
    0 0 0 0 65
    end

  • #2
    With your data example (thanks!) I did this:

    Code:
    . list if (var1 + var2 + var3 + var4) != 1 
    
         +--------------------------------+
         | var1   var2   var3   var4   id |
         |--------------------------------|
      3. |    0      0      0      0    3 |
      6. |    0      0      0      0    6 |
     10. |    0      0      0      0   10 |
     15. |    0      0      0      0   15 |
     17. |    0      0      0      0   17 |
         |--------------------------------|
     23. |    0      0      0      0   23 |
     24. |    0      0      0      0   24 |
     26. |    0      0      0      0   26 |
     28. |    0      0      0      0   28 |
     29. |    0      0      0      0   29 |
         |--------------------------------|
     30. |    0      0      0      0   30 |
     53. |    0      0      0      0   53 |
     60. |    0      0      0      0   60 |
     64. |    0      0      0      0   64 |
     65. |    0      0      0      0   65 |
         +--------------------------------+
    
    . 
    . gen type = 0  
    
    . gen codes = "" 
    (65 missing values generated)
    
    . 
    . forval j = 1/4 { 
      2.         replace type = `j' if var`j' == 1 
      3.         replace codes = codes + "`j'" if var`j' == 1 
      4. }
    (21 real changes made)
    (21 real changes made)
    (5 real changes made)
    (5 real changes made)
    (14 real changes made)
    (14 real changes made)
    (10 real changes made)
    (10 real changes made)
    
    . 
    . tab codes type, missing 
    
               |                          type
         codes |         0          1          2          3          4 |     Total
    -----------+-------------------------------------------------------+----------
               |        15          0          0          0          0 |        15 
             1 |         0         21          0          0          0 |        21 
             2 |         0          0          5          0          0 |         5 
             3 |         0          0          0         14          0 |        14 
             4 |         0          0          0          0         10 |        10 
    -----------+-------------------------------------------------------+----------
         Total |        15         21          5         14         10 |        65
    I note that while 1 to 4 are mutually exclusive, they're not collectively exhaustive. The string variable codes would capture multiple instances of 1 if they existed.

    Comment

    Working...
    X