Search code examples
stata

How can I combine categories?


I have a variable fruit with the following categories:

1
2
3
4
5
6
7
8
9
10
20
25

I want to collapse these as below:

1
2
3
4
5+

How can I do this?


Solution

  • Consider your example:

    clear
    input fruit
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    20
    25
    end
    
    tabulate fruit
    
          fruit |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          1        8.33        8.33
              2 |          1        8.33       16.67
              3 |          1        8.33       25.00
              4 |          1        8.33       33.33
              5 |          1        8.33       41.67
              6 |          1        8.33       50.00
              7 |          1        8.33       58.33
              8 |          1        8.33       66.67
              9 |          1        8.33       75.00
             10 |          1        8.33       83.33
             20 |          1        8.33       91.67
             25 |          1        8.33      100.00
    ------------+-----------------------------------
          Total |         12      100.00
    

    The following works for me:

    replace fruit = 5 if fruit >= 5
    
    tabulate fruit
    
          fruit |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          1        8.33        8.33
              2 |          1        8.33       16.67
              3 |          1        8.33       25.00
              4 |          1        8.33       33.33
              5 |          8       66.67      100.00
    ------------+-----------------------------------
          Total |         12      100.00