Appropriate answers depend on whether these variables
are string or numeric. I'll guess string.
You can loop over possible lengths with -forval-:
let's have an example in which the lengths range
from 4 to 40:
forval l = 4(2)40 {
Now we need to find which variables are of each
length:
ds <varlist>, has(type `l')
If we find some such variables, we need to process them:
if "`r(varlist)'" != "" {
Now we need to cycle over those variables:
foreach v of var `r(varlist)' {
Now we need to cycle over the pairs of digits
local j = `l'/2
forval i = 1/`j' {
gen `v'_`i' = substr(`v',2 * `i' - 1,2)
}
}
}
Here it is again:
forval l = 4(2)40 {
ds <varlist>, has(type `l')
if "`r(varlist)'" != "" {
foreach v of var `r(varlist)' {
local j = `l'/2
forval i = 1/`j' {
gen `v'_`i' = substr(`v',2 * `i' - 1,2)
}
}
}
}
You need to
1. replace 4(2)40
2. plug in your <varlist>
3. check that the new names will be OK.
Nick
[email protected]
Jean Bouyer
> I would like to split a variable into several ones, but there are no
> separators. Thus I don't think I can use the split Stata command.
> I only know that variable must be split every two digits. For
> instance 123456 must be split into 12 34 and 56.
> My problem is that I have to do that for about hundred variables and
> that the number of digits is not the same for all the variables
> (although it is always even).
>
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/