Announcement

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

  • Replicate recode results using floor, int or ceil function

    Hi all,

    I am trying to replicate recode results using floor, int or ceil function, my focus is recode a big data base with age variable, for this reason I would like use this function, lamentably i get differents results in frequency, example:

    Code:
    clear all
    set more off
    sysuse auto
    
    #delimit ;
     recode mpg
    (12/16= 1 12-16)
    (17/21= 2 17-21)
    (22/26= 3 22-26)
    (27/31= 4 27-31)
    (32/36= 5 32-36)
    (37/41= 6 37-41)
    (42/max= 7 34-max), gen(recodempg)
     ;
     #delimit cr
    
     tab recodempg
    
    
      RECODE of |
            mpg |
       (Mileage |
         (mpg)) |      Freq.     Percent        Cum.
    ------------+-----------------------------------
          12-16 |         14       18.92       18.92
          17-21 |         29       39.19       58.11
          22-26 |         20       27.03       85.14
          27-31 |          7        9.46       94.59
          32-36 |          3        4.05       98.65
          37-41 |          1        1.35      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    
    
    gen flormpg =4 * floor(mpg/4)
    tab flormpg
    
    
        flormpg |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             12 |         10       13.51       13.51
             16 |         25       33.78       47.30
             20 |         16       21.62       68.92
             24 |         12       16.22       85.14
             28 |          7        9.46       94.59
             32 |          3        4.05       98.65
             40 |          1        1.35      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    
    
    gen intempg =4 * int(mpg/4)
    tab intempg
    
    
        intempg |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             12 |         10       13.51       13.51
             16 |         25       33.78       47.30
             20 |         16       21.62       68.92
             24 |         12       16.22       85.14
             28 |          7        9.46       94.59
             32 |          3        4.05       98.65
             40 |          1        1.35      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    
    
    gen ceilmpg =4 * ceil(mpg/4)
    tab ceilmpg
    
    
        ceilmpg |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             12 |          2        2.70        2.70
             16 |         12       16.22       18.92
             20 |         24       32.43       51.35
             24 |         17       22.97       74.32
             28 |         11       14.86       89.19
             32 |          4        5.41       94.59
             36 |          3        4.05       98.65
             44 |          1        1.35      100.00
    ------------+-----------------------------------
          Total |         74      100.00

  • #2
    These all produce the same results as your recode command.
    Code:
    gen flormpg = 12 + 5 * floor((mpg-12)/5)
    tab flormpg
    
    gen intempg = 12 + 5 * int((mpg-12)/5)
    tab intempg
    
    gen ceilmpg =  7 + 5 * ceil((mpg-11)/5)
    tab ceilmpg

    Comment


    • #3
      Thanks William Lisowski your code works fine!.

      Regards

      Comment


      • #4
        Code:
        . sysuse auto,clear
        (1978 Automobile Data)
        
        . 
        . #delimit ;
        delimiter now ;
        .  recode mpg
        > (12/16= 1 12-16)
        > (17/21= 2 17-21)
        > (22/26= 3 22-26)
        > (27/31= 4 27-31)
        > (32/36= 5 32-36)
        > (37/41= 6 37-41)
        > (42/max= 7 34-max), gen(recodempg)
        >  ;
        (74 differences between mpg and recodempg)
        
        .  #delimit cr
        delimiter now cr
        . 
        . tab recodempg
        
          RECODE of |
                mpg |
           (Mileage |
             (mpg)) |      Freq.     Percent        Cum.
        ------------+-----------------------------------
              12-16 |         14       18.92       18.92
              17-21 |         29       39.19       58.11
              22-26 |         20       27.03       85.14
              27-31 |          7        9.46       94.59
              32-36 |          3        4.05       98.65
              37-41 |          1        1.35      100.00
        ------------+-----------------------------------
              Total |         74      100.00
        
        . gen recodempg2 = 5*(ceil(mpg/5.3333333333333333)-2)+11
        
        . tab recodempg2
        
         recodempg2 |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                 16 |         14       18.92       18.92
                 21 |         29       39.19       58.11
                 26 |         20       27.03       85.14
                 31 |          7        9.46       94.59
                 36 |          3        4.05       98.65
                 41 |          1        1.35      100.00
        ------------+-----------------------------------
              Total |         74      100.00
        
        .

        Comment


        • #5
          Thanks Scott for you reply.
          Regards

          Comment

          Working...
          X