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!
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!
Comment