Announcement

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

  • How to Count the Number of the Students Having Grade Retention in a Dataset in Stata Code?

    As the title suggested, I would like to count the number of the students who had grade retention in K-12 system. For example, I want to know how many students who made normal grade progression, how many students who had one-time grade retention, how many students who had two-time grade retentions....etc. The dataset is structured in long format below,
    *Simulated dataset.
    clear
    input byte (id grade)
    1 1
    1 2
    1 2
    1 3
    1 4
    1 4
    1 4
    1 4
    1 5
    1 6
    1 6
    2 1
    2 2
    2 3
    2 4
    2 4
    2 5
    2 5
    2 6
    2 6
    2 6
    2 7
    3 1
    3 2
    3 3
    3 4
    3 5
    3 6
    3 7
    4 1
    4 1
    4 2
    4 3
    4 4
    4 5
    4 6
    5 1
    5 2
    5 3
    5 4
    5 5
    5 5
    6 1
    6 2
    6 3
    6 4
    6 4
    6 5
    7 1
    7 2
    7 3
    7 3
    7 5
    7 6
    7 7
    end
    ​​​​​​​Thank you for your help!
    Last edited by smith Jason; 25 Mar 2022, 19:28.

  • #2

    Code:
    bysort id grade : gen repeat = _n > 1 
    
    by id: egen num_repeats = total(repeat)
    You also have students who skipped grades as well.

    Code:
    bysort id (grade) :: gen skip = inrange(grade - grade{_n-1], 2, .) 
    by id : egen num_skips = total(skip)

    Comment


    • #3
      Thank you!

      Comment


      • #4
        Originally posted by Nick Cox View Post
        Code:
        bysort id grade : gen repeat = _n > 1
        
        by id: egen num_repeats = total(repeat)
        You also have students who skipped grades as well.

        Code:
        bysort id (grade) :: gen skip = inrange(grade - grade{_n-1], 2, .)
        by id : egen num_skips = total(skip)
        Thanks for your response.
        If I want to know the distribution about how many students who had grade retention by grade, how could I write Stata code?
        Thank you!

        Comment


        • #5
          See the help for the tag() function under egen.

          Comment


          • #6
            bys id grade : gen repeat = _n > 1
            egen tag = tag(grade)
            egen tags = total(repeat), by(grade)

            It seems that the code above can provide the answer after consulting the resources you mentioned. However, I noticed that the results obtained with the code is presented by total times instead of by person, because one person might have more than one-time grade retention. I really don't know how to develop the code further. Any hints or thoughts?
            Thank you for your help!

            Comment


            • #7
              See if this gets you closer to what you want. I used the data example in #1.

              Code:
              . bysort id grade : gen repeats = _n > 1 
              
              . egen num_repeats = total(repeats), by(id)
              
              . 
              . egen tag_id = tag(id)  
              
              . 
              . tab num_repeats if tag_id 
              
              num_repeats |      Freq.     Percent        Cum.
              ------------+-----------------------------------
                        0 |          1       14.29       14.29
                        1 |          4       57.14       71.43
                        4 |          1       14.29       85.71
                        5 |          1       14.29      100.00
              ------------+-----------------------------------
                    Total |          7      100.00
              
              . 
              . egen num_repeats_grade = total(repeats), by(id grade)
              
              . 
              . egen tag_id_grade = tag(id grade)
              
              . 
              . count if repeats 
                13
              
              . 
              . tab num_repeats_grade grade if tag_id_grade 
              
              num_repeat |                                    grade
                 s_grade |         1          2          3          4          5          6          7 |     Total
              -----------+-----------------------------------------------------------------------------+----------
                       0 |         6          6          6          3          5          3          3 |        32 
                       1 |         1          1          1          2          2          1          0 |         8 
                       2 |         0          0          0          0          0          1          0 |         1 
                       3 |         0          0          0          1          0          0          0 |         1 
              -----------+-----------------------------------------------------------------------------+----------
                   Total |         7          7          7          6          7          5          3 |        42 
              
              . 
              . list, sepby(id)
              
                   +----------------------------------------------------------------+
                   | id   grade   repeats   num_re~s   tag_id   num_re~e   tag_id~e |
                   |----------------------------------------------------------------|
                1. |  1       1         0          5        1          0          1 |
                2. |  1       2         0          5        0          1          1 |
                3. |  1       2         1          5        0          1          0 |
                4. |  1       3         0          5        0          0          1 |
                5. |  1       4         0          5        0          3          1 |
                6. |  1       4         1          5        0          3          0 |
                7. |  1       4         1          5        0          3          0 |
                8. |  1       4         1          5        0          3          0 |
                9. |  1       5         0          5        0          0          1 |
               10. |  1       6         0          5        0          1          1 |
               11. |  1       6         1          5        0          1          0 |
                   |----------------------------------------------------------------|
               12. |  2       1         0          4        1          0          1 |
               13. |  2       2         0          4        0          0          1 |
               14. |  2       3         0          4        0          0          1 |
               15. |  2       4         0          4        0          1          1 |
               16. |  2       4         1          4        0          1          0 |
               17. |  2       5         0          4        0          1          1 |
               18. |  2       5         1          4        0          1          0 |
               19. |  2       6         0          4        0          2          1 |
               20. |  2       6         1          4        0          2          0 |
               21. |  2       6         1          4        0          2          0 |
               22. |  2       7         0          4        0          0          1 |
                   |----------------------------------------------------------------|
               23. |  3       1         0          0        1          0          1 |
               24. |  3       2         0          0        0          0          1 |
               25. |  3       3         0          0        0          0          1 |
               26. |  3       4         0          0        0          0          1 |
               27. |  3       5         0          0        0          0          1 |
               28. |  3       6         0          0        0          0          1 |
               29. |  3       7         0          0        0          0          1 |
                   |----------------------------------------------------------------|
               30. |  4       1         0          1        1          1          1 |
               31. |  4       1         1          1        0          1          0 |
               32. |  4       2         0          1        0          0          1 |
               33. |  4       3         0          1        0          0          1 |
               34. |  4       4         0          1        0          0          1 |
               35. |  4       5         0          1        0          0          1 |
               36. |  4       6         0          1        0          0          1 |
                   |----------------------------------------------------------------|
               37. |  5       1         0          1        1          0          1 |
               38. |  5       2         0          1        0          0          1 |
               39. |  5       3         0          1        0          0          1 |
               40. |  5       4         0          1        0          0          1 |
               41. |  5       5         0          1        0          1          1 |
               42. |  5       5         1          1        0          1          0 |
                   |----------------------------------------------------------------|
               43. |  6       1         0          1        1          0          1 |
               44. |  6       2         0          1        0          0          1 |
               45. |  6       3         0          1        0          0          1 |
               46. |  6       4         0          1        0          1          1 |
               47. |  6       4         1          1        0          1          0 |
               48. |  6       5         0          1        0          0          1 |
                   |----------------------------------------------------------------|
               49. |  7       1         0          1        1          0          1 |
               50. |  7       2         0          1        0          0          1 |
               51. |  7       3         0          1        0          1          1 |
               52. |  7       3         1          1        0          1          0 |
               53. |  7       5         0          1        0          0          1 |
               54. |  7       6         0          1        0          0          1 |
               55. |  7       7         0          1        0          0          1 |
                   +----------------------------------------------------------------+
              
              .

              Comment


              • #8
                Thank you!

                Comment

                Working...
                X