Announcement

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

  • Working with multiple if conditions

    Dear Statalist,

    I am browsing a dataset with multiple if conditions. Is it possible to identify the particular condition for which a particular record is selected?

    Thanks in advance.

    Shoummo

  • #2
    This may be what you are alluding to. groups here is from the Stata Journal and must be installed before it can be used.

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . gen wanted1 = mpg > 30
    
    . gen wanted2 = price < 8000
    
    . gen either = wanted1 | wanted2
    
    . groups wanted? either
    
      +----------------------------------------------+
      | wanted1   wanted2   either   Freq.   Percent |
      |----------------------------------------------|
      |       0         0        0      14     18.92 |
      |       0         1        1      55     74.32 |
      |       1         1        1       5      6.76 |
      +----------------------------------------------+
    
    . sort wanted?
    
    . list mpg price wanted? either , sepby(wanted?)
    
         +-------------------------------------------+
         | mpg    price   wanted1   wanted2   either |
         |-------------------------------------------|
      1. |  16   10,371         0         0        0 |
      2. |  14   14,500         0         0        0 |
      3. |  12   11,497         0         0        0 |
      4. |  21   15,906         0         0        0 |
      5. |  21    8,814         0         0        0 |
      6. |  14   13,466         0         0        0 |
      7. |  17    9,690         0         0        0 |
      8. |  21    8,129         0         0        0 |
      9. |  14   11,385         0         0        0 |
     10. |  25    9,735         0         0        0 |
     11. |  14   12,990         0         0        0 |
     12. |  12   13,594         0         0        0 |
     13. |  16   10,372         0         0        0 |
     14. |  17   11,995         0         0        0 |
         |-------------------------------------------|
     15. |  20    4,816         0         1        1 |
     16. |  18    4,010         0         1        1 |
     17. |  15    6,165         0         1        1 |
     18. |  25    6,850         0         1        1 |
     19. |  22    5,104         0         1        1 |
     20. |  17    4,749         0         1        1 |
     21. |  18    5,899         0         1        1 |
     22. |  19    4,733         0         1        1 |
     23. |  28    4,647         0         1        1 |
     24. |  18    4,516         0         1        1 |
     25. |  24    3,667         0         1        1 |
     26. |  25    5,799         0         1        1 |
     27. |  21    4,296         0         1        1 |
     28. |  18    4,060         0         1        1 |
     29. |  24    5,079         0         1        1 |
     30. |  26    4,453         0         1        1 |
     31. |  19    4,723         0         1        1 |
     32. |  18    5,788         0         1        1 |
     33. |  15    7,827         0         1        1 |
     34. |  17    6,342         0         1        1 |
     35. |  22    4,504         0         1        1 |
     36. |  18    5,798         0         1        1 |
     37. |  30    3,995         0         1        1 |
     38. |  19    4,424         0         1        1 |
     39. |  23    7,140         0         1        1 |
     40. |  26    6,486         0         1        1 |
     41. |  23    6,229         0         1        1 |
     42. |  14    6,303         0         1        1 |
     43. |  18    5,719         0         1        1 |
     44. |  25    4,482         0         1        1 |
     45. |  25    4,697         0         1        1 |
     46. |  20    3,291         0         1        1 |
     47. |  20    5,189         0         1        1 |
     48. |  28    4,499         0         1        1 |
     49. |  18    4,934         0         1        1 |
     50. |  19    4,181         0         1        1 |
     51. |  29    3,299         0         1        1 |
     52. |  21    4,187         0         1        1 |
     53. |  23    6,295         0         1        1 |
     54. |  19    4,082         0         1        1 |
     55. |  28    4,389         0         1        1 |
     56. |  26    3,895         0         1        1 |
     57. |  19    3,955         0         1        1 |
     58. |  18    4,890         0         1        1 |
     59. |  30    3,984         0         1        1 |
     60. |  19    5,172         0         1        1 |
     61. |  19    5,222         0         1        1 |
     62. |  16    5,886         0         1        1 |
     63. |  22    3,829         0         1        1 |
     64. |  14    5,379         0         1        1 |
     65. |  22    3,799         0         1        1 |
     66. |  16    5,705         0         1        1 |
     67. |  22    4,099         0         1        1 |
     68. |  24    4,195         0         1        1 |
     69. |  24    4,172         0         1        1 |
         |-------------------------------------------|
     70. |  41    5,397         1         1        1 |
     71. |  35    3,798         1         1        1 |
     72. |  31    3,748         1         1        1 |
     73. |  35    4,589         1         1        1 |
     74. |  34    4,425         1         1        1 |
         +-------------------------------------------+
    
    .
    Code:
    
    

    Comment


    • #3
      Originally posted by Nick Cox View Post
      This may be what you are alluding to. groups here is from the Stata Journal and must be installed before it can be used.

      Code:
      . sysuse auto, clear
      (1978 Automobile Data)
      
      . gen wanted1 = mpg > 30
      
      . gen wanted2 = price < 8000
      
      . gen either = wanted1 | wanted2
      
      . groups wanted? either
      
      +----------------------------------------------+
      | wanted1 wanted2 either Freq. Percent |
      |----------------------------------------------|
      | 0 0 0 14 18.92 |
      | 0 1 1 55 74.32 |
      | 1 1 1 5 6.76 |
      +----------------------------------------------+
      
      . sort wanted?
      
      . list mpg price wanted? either , sepby(wanted?)
      
      +-------------------------------------------+
      | mpg price wanted1 wanted2 either |
      |-------------------------------------------|
      1. | 16 10,371 0 0 0 |
      2. | 14 14,500 0 0 0 |
      3. | 12 11,497 0 0 0 |
      4. | 21 15,906 0 0 0 |
      5. | 21 8,814 0 0 0 |
      6. | 14 13,466 0 0 0 |
      7. | 17 9,690 0 0 0 |
      8. | 21 8,129 0 0 0 |
      9. | 14 11,385 0 0 0 |
      10. | 25 9,735 0 0 0 |
      11. | 14 12,990 0 0 0 |
      12. | 12 13,594 0 0 0 |
      13. | 16 10,372 0 0 0 |
      14. | 17 11,995 0 0 0 |
      |-------------------------------------------|
      15. | 20 4,816 0 1 1 |
      16. | 18 4,010 0 1 1 |
      17. | 15 6,165 0 1 1 |
      18. | 25 6,850 0 1 1 |
      19. | 22 5,104 0 1 1 |
      20. | 17 4,749 0 1 1 |
      21. | 18 5,899 0 1 1 |
      22. | 19 4,733 0 1 1 |
      23. | 28 4,647 0 1 1 |
      24. | 18 4,516 0 1 1 |
      25. | 24 3,667 0 1 1 |
      26. | 25 5,799 0 1 1 |
      27. | 21 4,296 0 1 1 |
      28. | 18 4,060 0 1 1 |
      29. | 24 5,079 0 1 1 |
      30. | 26 4,453 0 1 1 |
      31. | 19 4,723 0 1 1 |
      32. | 18 5,788 0 1 1 |
      33. | 15 7,827 0 1 1 |
      34. | 17 6,342 0 1 1 |
      35. | 22 4,504 0 1 1 |
      36. | 18 5,798 0 1 1 |
      37. | 30 3,995 0 1 1 |
      38. | 19 4,424 0 1 1 |
      39. | 23 7,140 0 1 1 |
      40. | 26 6,486 0 1 1 |
      41. | 23 6,229 0 1 1 |
      42. | 14 6,303 0 1 1 |
      43. | 18 5,719 0 1 1 |
      44. | 25 4,482 0 1 1 |
      45. | 25 4,697 0 1 1 |
      46. | 20 3,291 0 1 1 |
      47. | 20 5,189 0 1 1 |
      48. | 28 4,499 0 1 1 |
      49. | 18 4,934 0 1 1 |
      50. | 19 4,181 0 1 1 |
      51. | 29 3,299 0 1 1 |
      52. | 21 4,187 0 1 1 |
      53. | 23 6,295 0 1 1 |
      54. | 19 4,082 0 1 1 |
      55. | 28 4,389 0 1 1 |
      56. | 26 3,895 0 1 1 |
      57. | 19 3,955 0 1 1 |
      58. | 18 4,890 0 1 1 |
      59. | 30 3,984 0 1 1 |
      60. | 19 5,172 0 1 1 |
      61. | 19 5,222 0 1 1 |
      62. | 16 5,886 0 1 1 |
      63. | 22 3,829 0 1 1 |
      64. | 14 5,379 0 1 1 |
      65. | 22 3,799 0 1 1 |
      66. | 16 5,705 0 1 1 |
      67. | 22 4,099 0 1 1 |
      68. | 24 4,195 0 1 1 |
      69. | 24 4,172 0 1 1 |
      |-------------------------------------------|
      70. | 41 5,397 1 1 1 |
      71. | 35 3,798 1 1 1 |
      72. | 31 3,748 1 1 1 |
      73. | 35 4,589 1 1 1 |
      74. | 34 4,425 1 1 1 |
      +-------------------------------------------+
      
      .
      Code:
      
      
      Thank you Nick Cox, this will help.

      Comment

      Working...
      X