I am trying to loop through all unique values of a variable.
For example if the data set contains 9 observations and the variable of
interest is D which takes the values 0 0 0 2 2 2 4 4 4
I would like to loop through the unique values 0 2 4 .
Below is a terrible "fix" which makes the additional assumption that the
unique values are evenly spaced. ( I prefer not to make that
assumption). It basically extracts the number of unique values from
*inspect*, finds the minimum value through *summarize* and then
constructs the loop from scratch .
quietly inspect D
local nu=r(N_unique)
quietly sum D
local dmin = r(min)
local last = `nu' * `dmin'
forvalues d= `dmin'(`dmin') `last' {
...
}
Is there a better way to do that in stata or is that just not what stata
was built for ?