Announcement

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

  • Changing variable's value by frequency order

    Hi, i have a variable x with 5 values (assume 1 to 5). When I do tab x I have that the value with higher frequency is 2 and the next value with higher frequency is 5. I want to recode 2 as 1, 5 as 2, and so on for all the values of my variable. Clearly I have to do that with many variables, that's why I cannot do it manually.

  • #2
    Why not just use the sort option?


    Code:
    . webuse nlswork, clear
    (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
    
    .. tab occ_code
    
     occupation |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |      3,008       10.59       10.59
              2 |      1,494        5.26       15.84
              3 |     10,974       38.62       54.47
              4 |      1,323        4.66       59.12
              5 |        438        1.54       60.67
              6 |      4,309       15.17       75.83
              7 |        571        2.01       77.84
              8 |      4,300       15.13       92.98
              9 |          6        0.02       93.00
             10 |        144        0.51       93.50
             11 |        194        0.68       94.19
             12 |          7        0.02       94.21
             13 |      1,645        5.79      100.00
    ------------+-----------------------------------
          Total |     28,413      100.00
    
    . tab occ_code, sort
    
     occupation |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              3 |     10,974       38.62       38.62
              6 |      4,309       15.17       53.79
              8 |      4,300       15.13       68.92
              1 |      3,008       10.59       79.51
             13 |      1,645        5.79       85.30
              2 |      1,494        5.26       90.56
              4 |      1,323        4.66       95.21
              7 |        571        2.01       97.22
              5 |        438        1.54       98.76
             11 |        194        0.68       99.45
             10 |        144        0.51       99.95
             12 |          7        0.02       99.98
              9 |          6        0.02      100.00
    ------------+-----------------------------------
          Total |     28,413      100.00
    Code:
    
    

    Comment


    • #3
      do you know that there is a "sort" option to -tab-; does that do what you want? otherwise, see
      Code:
      help recode

      Comment


      • #4
        Yes, indeed I knew that but my question is about recoding. How do I recode those 3, 6, 8 (in Nick's example) to 1, 2, 3

        Comment


        • #5
          Code:
          recode varname (3=1) (6=2) (8=3), gen(varname2)
          obviously, you should replace "varname" with the name of your variable and replace "varname2" with whatever name you want for the recoded variable; see
          Code:
          help recode

          Comment


          • #6
            Thanks Rich for the answer. But ideally I would like a general way of doing this given that it's I have many variables that I would like to recode.
            Last edited by Jean Jacques; 10 Dec 2020, 12:04.

            Comment


            • #7
              Here's some technique. You need labmask from the Stata Journal . In general, you can loop over the variables of interest.


              Code:
              webuse nlswork, clear
              
              foreach v in occ_code ind_code { 
                  bysort `v' : gen freq = -_N if !missing(`v')
                  egen `v'_2 = group(freq `v') 
                  capture labmask `v'_2, values(`v') decode
                  if _rc labmask `v'_2, values(`v')
                  drop freq 
                  _crcslbl `v'_2 `v'
              }
              
              tab1  *2

              Comment

              Working...
              X