Announcement

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

  • How can I Effectively Delete the Data Record (only one line) in Long Format Dataset with Stata Code?

    Hello,
    I have a long format dataset below. I really want to remove the data records having one line (grade=1/0 or grade=.)
    How can I achieve my goal with Stata code?
    Thank you!



    clear
    input str10 id byte (grade state)
    1 1 0
    1 2 0
    1 3 0
    1 4 0
    1 5 0
    1 6 0
    1 7 0
    1 8 0
    1 9 0
    1 10 0
    1 11 0
    1 12 0
    2 1 0
    2 2 0
    2 . .
    2 4 0
    2 . .
    2 6 0
    2 7 0
    2 8 0
    2 . .
    2 10 0
    2 11 0
    2 12 0
    3 1 0
    3 2 0
    3 3 0
    3 4 0
    3 5 0
    3 6 0
    3 7 0
    3 8 0
    3 9 0
    3 10 0
    3 . .
    3 11 0
    4 1 0
    4 . .
    4 3 0
    4 4 0
    4 5 0
    4 6 0
    4 . .
    4 . .
    4 . .
    4 . .
    4 . .
    4 12 0
    5 1 0
    5 2 0
    5 . .
    5 . .
    5 . .
    5 . .
    5 . .
    5 . .
    5 . .
    5 . .
    5 . .
    5 9 0
    6 1 0
    6 1 1
    7 1 0
    7 . .
    7 . .
    7 . .
    7 . .
    7 4 0
    7 5 0
    7 6 0
    7 7 0
    7 7 1
    8 . 0
    9 1 1
    10 . 1
    11 0 1
    end

  • #2
    What do you mean? There are no observations with grade = 1/0, and there can't be because "1/0" would only be possible with a string variable, which grade is not. Do you mean you want to eliminate all observations where grade is either 0, 1, or missing? If so
    Code:
    drop if inlist(grade, 0, 1, .)
    If that's not what you mean, please post back with a clear explanation of what you want. Perhaps better still, point out exactly which observations in the example data you wish to delete.

    Comment


    • #3
      Thank you. The data record with Id=8 id=9 id =10 id=11 is what I want to delete.

      Comment


      • #4
        My guess is that the objective is to delete any id that has just a single observation - id 8, 9, 10, and 11 at the end of the example data.
        Code:
        generate id_num = real(id)
        sort id_num, stable
        by id_num: generate todrop = _N==1
        tab id todrop
        drop if todrop==1
        drop todrop id_num
        tab id
        Code:
        . generate id_num = real(id)
        
        . sort id_num, stable
        
        . by id_num: generate todrop = _N==1
        
        . tab id todrop
        
                   |        todrop
                id |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        12          0 |        12
                10 |         0          1 |         1
                11 |         0          1 |         1
                 2 |        12          0 |        12
                 3 |        12          0 |        12
                 4 |        12          0 |        12
                 5 |        12          0 |        12
                 6 |         2          0 |         2
                 7 |        10          0 |        10
                 8 |         0          1 |         1
                 9 |         0          1 |         1
        -----------+----------------------+----------
             Total |        72          4 |        76
        
        . drop if todrop==1
        (4 observations deleted)
        
        . drop todrop id_num
        
        . tab id
        
                 id |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                  1 |         12       16.67       16.67
                  2 |         12       16.67       33.33
                  3 |         12       16.67       50.00
                  4 |         12       16.67       66.67
                  5 |         12       16.67       83.33
                  6 |          2        2.78       86.11
                  7 |         10       13.89      100.00
        ------------+-----------------------------------
              Total |         72      100.00
        
        .
        Added in edit: original posting crossed with post #3, so I guessed correctly! But I shouldn't have had to guess, as Clyde suggests.

        Comment


        • #5
          Thank you all!

          Comment

          Working...
          X