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

From |
Margaret R Grove <margaret.r.grove@Dartmouth.EDU> |

To |
statalist@hsphsun2.harvard.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.

Meg

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.

Nick

n.j.cox@durham.ac.uk

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

n.j.cox@durham.ac.uk

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,500

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

characters).on...

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

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/

* * 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/

**References**:**st: Creating single characters from long strings***From:*"Andri Rauch" <Andri.Rauch@insel.ch>

**Re: st: Creating single characters from long strings***From:*Margaret R Grove <margaret.r.grove@Dartmouth.EDU>

**RE: st: Creating single characters from long strings***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**Re: st: Creating single characters from long strings***From:*Margaret R Grove <margaret.r.grove@Dartmouth.EDU>

**RE: st: Creating single characters from long strings***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: Creating single characters from long strings** - Next by Date:
**Re: st: How to attach a string to a numeric variable as its value lable** - Previous by thread:
**RE: st: Creating single characters from long strings** - Next by thread:
**st: RE: Creating single characters from long strings** - Index(es):

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