Say I have var1
that is continuous:
clear
set obs 1000
gen var1 = runiform()
sum var1
Now I want to create var2
based on ranges of var1
. I can do this as follows:
gen var2 = "Lowest" if var1<.25
replace var2 = "Low" if var1>=.25 & var1<.5
replace var2 = "High" if var1>=.5 & var1<.75
replace var2 = "Highest" if var1>=.75
I would like to be able to do this in one line. Pseudocode:
gen var2 = (ranges(0 .25 .5 .75 1) values("Lowest" "Low" "High" "Highest"))
A way to do something quite similar in R
using cut
is found at Create categorical variable in R based on range
Is there any command that can do something in Stata that is like the R version? Imagine that one has 10,000 ranges that are needing to go into var2
. Then a better method would help a lot.
Another way to do this on one line in Stata is clunky and is found at http://www.stata.com/support/faqs/data-management/multiple-operations/:
generate var2 = cond(var1<=.25, "Lowest", cond(var1<=.50, "Low", cond(var1<=.75, "High", cond(var1<=1.00, "Highest", ""))))
Is there a better way?