Announcement

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

  • binning/looping/coding question

    Hello!

    I would like to create bins of a continous variable x. Each bin has a width of 0.05.

    Below is the "non-loop" version of what I want to do:

    capture drop bin
    generate bin = .
    replace bin =1 if x>=0 & x<0.05
    replace bin =2 if x>=0.05 & x<0.1
    replace bin =3 if x>=0.1 & x<0.15
    ....
    replace bin =N if x>=1 & x<1.05

    (therefore bin =. if x>=1.05)


    It goes without saying that I would rather do the "loop" version. I came up with this code:

    capture drop bin
    generate bin =.
    local k=1

    forvalues i = 0(0.05)1 {
    forvalues j = 0.05(0.05)1.05 {
    replace bin = `k' if x >= `i' & x < `j'
    local k = `k' + 1
    }
    }

    The code appears to work, except for the naming of the bins: I don't have bins "1", "2" , "3"...N; instead I have bins "20", "40","60"...."400"

    Can anyone help me fix this "issue"?

    Many thanks!

  • #2
    k advances 20 times inside the inner j-loop. Try move the counter to the outer i-loop:

    Code:
    forvalues i = 0(0.05)1 {
    forvalues j = 0.05(0.05)1.05 {
    replace bin = `k' if x >= `i' & x < `j'
    }
    local k = `k' + 1
    }
    Last edited by Ken Chui; 27 Apr 2021, 08:29.

    Comment


    • #3
      No need for any looping here. Your bin limits are all just multiples of 0.05. See this, compare the use of ceil() and note also

      https://www.stata-journal.com/articl...article=dm0002

      https://www.stata-journal.com/articl...article=dm0095

      Code:
      . clear
      
      . set obs 1000
      number of observations (_N) was 0, now 1,000
      
      . set seed 3082
      
      . gen y = runiform()
      
      . gen bin = 0.05 * floor(y / 0.05)
      
      . tab bin
      
              bin |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |         46        4.60        4.60
              .05 |         59        5.90       10.50
               .1 |         43        4.30       14.80
              .15 |         41        4.10       18.90
               .2 |         58        5.80       24.70
              .25 |         42        4.20       28.90
               .3 |         47        4.70       33.60
              .35 |         49        4.90       38.50
               .4 |         44        4.40       42.90
              .45 |         49        4.90       47.80
               .5 |         42        4.20       52.00
              .55 |         53        5.30       57.30
               .6 |         54        5.40       62.70
              .65 |         60        6.00       68.70
               .7 |         52        5.20       73.90
              .75 |         66        6.60       80.50
               .8 |         46        4.60       85.10
              .85 |         44        4.40       89.50
               .9 |         50        5.00       94.50
              .95 |         55        5.50      100.00
      ------------+-----------------------------------
            Total |      1,000      100.00
      
      .

      Comment


      • #4
        As always, many thanks for your clear response!

        Comment

        Working...
        X