Announcement

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

  • bysort gen n = _n with if condition 1/2/3/4 is true

    Hello everybody, I have a hospital list (ik) doing several surgeries (haupt_op).
    Now I want create a new variable (n_patients1), which counts the number of Haupt_op by ik, but only if all Haupt_op contain 1 or 2 or 3 or 4.

    Code:
     ik   haupt_op    n_patients1
        
      1.          1              
      2.          2             3
      2.          3              
      2.          7
      3.          8             3
    
      3.          3
      3.          4             
      4.          2             2
      4.          3
      5.          7
    
     6.           7
     7.           7             3
     7.           9
     7.           4
    
     7.           8
     8.           4            1
     9.           4             3
     9.           2
     9.           8
    Code:
    bysort ik: gen n_patients1 = _N if _n == 1 & haupt_op==1 | haupt_op==2  | haupt_op==3  | haupt_op==4
    .
    As you can see my code counts every ik containing haupt_op 1 to 4. How can I tell stata, to only generate the new variable if haupt_op is 1/2/3/4 and nothing else?
    Last edited by frederik baumgermany; 20 Mar 2019, 10:22.

  • #2
    Hi Frederik,

    I'm not absolutely sure if I understand correctly what you plan. But how about this:
    Code:
    clear
    input ik haupt_op
    1 1
    2 2
    2 3
    2 7
    3 8
    3 3
    3 4
    4 2
    4 3
    5 7
    6 7
    7 7
    7 9
    7 4
    7 8
    8 4
    9 4
    9 2
    9 8
    end
    bysort ik (haupt_op) : generate n_patients1=_N if (haupt_op[1]>=1 & haupt_op[_N]<=4)
    list , sepby(ik)
    Is this what you want? What it does is sort by haupt_op inside each block of ik, and only calculate the result for blocks where the first observation is greater or equal to 1 and the last observation is lower or equal to 4.

    Kind Regards
    Bela

    PS: Please note that you are far more likely to receive a quick and appropriate answer when you ask your question with ready-to-use sample data (have a look at the FAQ and .help dataex in order to learn how to do so).

    Comment


    • #3
      Hello Frederik,
      I am not sure if you want to generate a variable that counts the number of times one particular haupt_op occurs within ik, or whether you want to count the number of times any of the values 1 to 4 of haupt_op occur (essentially treating 1-4 as the same).


      If you want the first,
      Code:
      levelsof haupt_op , local(haupts)
      foreach haupts of local haupts {
      egen n_patients`haupts'  = count(haupt_op== `haupts' ) , by(ik)
      }
      If you want the last, I think this will work:
      Code:
      egen n_patients1 = count(haupt_op==1 | haupt_op==2 | haupt_op==3 | haupt_op==4) , by(ik)
      I am not sure what your code is doing. However, in the future, I would always put brackets around the conditions so that Stata knows (and future you) how to evaluate the expression.
      Code:
       
       bysort ik: gen n_patients1 = _N if (_n == 1) & (haupt_op==1 | haupt_op==2  | haupt_op==3  | haupt_op==4)

      Comment


      • #4
        I don't understand this problem either. But I think use of egen, count() in #3 won't work, as it counts non-missings. True-or-false expressions evaluate as 1 or 0 and so are always non-missing. As a result you will just get the number of observations in each group. egen, total() may be what is intended here.

        Comment


        • #5
          Edit: This crossed with Nick's post in #4 & Frederik's response in #5

          So, like the others, I'm not exactly sure what you want, but I have included the suggestions made by Daniel Bela and Oscar Smallenbroek above, plus one of my own in case one of them gives you what you are looking for:

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input byte(ik haupt_op)
          1 1
          2 2
          2 3
          2 7
          3 8
          3 3
          3 4
          4 2
          4 3
          5 7
          6 7
          7 7
          7 9
          7 4
          7 8
          8 4
          9 4
          9 2
          9 8
          end
          Code:
          . table ik haupt_op, row col
          
          ------------------------------------------------------------------
                    |                        haupt_op                      
                 ik |     1      2      3      4      7      8      9  Total
          ----------+-------------------------------------------------------
                  1 |     1                                                1
                  2 |            1      1             1                    3
                  3 |                   1      1             1             3
                  4 |            1      1                                  2
                  5 |                                 1                    1
                  6 |                                 1                    1
                  7 |                          1      1      1      1      4
                  8 |                          1                           1
                  9 |            1             1             1             3
                    |
              Total |     1      3      3      4      4      3      1     19
          ------------------------------------------------------------------
          
          
          . tabulate ik haup
          
                     |                                   haupt_op
                  ik |         1          2          3          4          7          8          9 |     Total
          -----------+-----------------------------------------------------------------------------+----------
                   1 |         1          0          0          0          0          0          0 |         1
                   2 |         0          1          1          0          1          0          0 |         3
                   3 |         0          0          1          1          0          1          0 |         3
                   4 |         0          1          1          0          0          0          0 |         2
                   5 |         0          0          0          0          1          0          0 |         1
                   6 |         0          0          0          0          1          0          0 |         1
                   7 |         0          0          0          1          1          1          1 |         4
                   8 |         0          0          0          1          0          0          0 |         1
                   9 |         0          1          0          1          0          1          0 |         3
          -----------+-----------------------------------------------------------------------------+----------
               Total |         1          3          3          4          4          3          1 |        19
          
          
          * Oscar's suggestions
          egen n_patients1 = count(haupt_op==1 | haupt_op==2 | haupt_op==3 | haupt_op==4) , by(ik)
          bysort ik: gen n_patients2 = _N if (_n == 1) & (haupt_op==1 | haupt_op==2  | haupt_op==3  | haupt_op==4)
          
          * Daniels suggestion
          bysort ik (haupt_op) : generate n_patients3=_N if (haupt_op[1]>=1 & haupt_op[_N]<=4)
          
          * My own
          gen is1_4 = (inrange( haupt_op,1,4))
          by ik: egen total1_4 = total(is1_4)  // counts the number of operations where operation type ==1,2,3 or 4
          
          list, sepby( ik) noobs
          
            +-------------------------------------------------------------------+
            | ik   haupt_op   n_pati~1   n_pati~2   n_pati~3   is1_4   total1_4 |
            |-------------------------------------------------------------------|
            |  1          1          1          1          1       1          1 |
            |-------------------------------------------------------------------|
            |  2          2          3          3          .       1          2 |
            |  2          3          3          .          .       1          2 |
            |  2          7          3          .          .       0          2 |
            |-------------------------------------------------------------------|
            |  3          3          3          .          .       1          2 |
            |  3          4          3          .          .       1          2 |
            |  3          8          3          .          .       0          2 |
            |-------------------------------------------------------------------|
            |  4          2          2          2          2       1          2 |
            |  4          3          2          .          2       1          2 |
            |-------------------------------------------------------------------|
            |  5          7          1          .          .       0          0 |
            |-------------------------------------------------------------------|
            |  6          7          1          .          .       0          0 |
            |-------------------------------------------------------------------|
            |  7          4          4          .          .       1          1 |
            |  7          7          4          .          .       0          1 |
            |  7          8          4          .          .       0          1 |
            |  7          9          4          .          .       0          1 |
            |-------------------------------------------------------------------|
            |  8          4          1          1          1       1          1 |
            |-------------------------------------------------------------------|
            |  9          2          3          .          .       1          2 |
            |  9          4          3          3          .       1          2 |
            |  9          8          3          .          .       0          2 |
            +-------------------------------------------------------------------+
          Last edited by David Benson; 20 Mar 2019, 12:19.

          Comment

          Working...
          X