[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Creating single characters from long strings

From   Margaret R Grove <margaret.r.grove@Dartmouth.EDU>
Subject   Re: st: Creating single characters from long strings
Date   Wed, 26 Mar 2008 09:54:33 -0400

Thanks much, Nick! I should have known that one ... it has messed up my code on a few occasions.

Nick Cox wrote:

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.
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.
Margaret R Grove


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)


Andri Rauch wrote:

Dear all,
I should create single characters from very long strings (ie around

My original variable abcdefg... should be transformed to:
variable_1: a, variable_2: b, variable_3: c, variable_4: d and so

I assume it should be done by combining the substring and foreach
* For searches and help try:
*   For searches and help try:

© Copyright 1996–2019 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index