You had, for a string variable -variable-,
local len = length(variable)
But as -variable- is a variable, what precisely goes into -len-? The
answer is that Stata uses the value for the first observation, i.e. that
is equivalent to
local len = length(variable[1])
That would be the right answer for your purpose whenever -variable[1]-
was no shorter than any other value. But there is no summary role
whereby -length()- looks at the entire set of values and works out some
summary length (e.g. the maximum, which is most often wanted in my
experience). As far as -length()- is concerned, it can only take a
single string argument whenever the assignment is to a macro.
The same issue arises whenever you do something like
. sysuse auto, clear
. di make
What gets displayed? Answer: make[1].
I have sympathies with anyone who says that this kind of input should
have been classed as a syntax error, but right at the beginning of Stata
it was decided that it was legal. My guess is that Stata's designers
decided that they would rather see some output than be stopped by a
syntax error.
Nick
[email protected]
Margaret R Grove
Ah yes - early morning fog here! No `j' needed. Nick - I'm not sure
what you mean about the length(variable) note. I tried running that
little program and it did what I wanted.
Nick Cox wrote:
> For a string variable, -length(variable)- is equivalent to
> -length(variable[1])-. That may or may not be a correct answer.
>
> Also `j' is the same as `i' here.
>
> Nick
> [email protected]
>
> Margaret R Grove
>
> Andri,
>
> Perhaps something like this ....
>
> local len=length(variable)
> local j=0
> forval i=1/`len' {
> local j=`j'+1
> gen str1 var`j'=substr(variable,`i',1)
> }
>
> Meg
>
> Andri Rauch wrote:
>
>> Dear all,
>> I should create single characters from very long strings (ie around
>>
> 500
>
>> characters).
>> My original variable abcdefg... should be transformed to:
>> variable_1: a, variable_2: b, variable_3: c, variable_4: d and so
>>
> on...
>
>> I assume it should be done by combining the substring and foreach
>> command.
>>
*
* 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/