Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: use of subinstr |

Date |
Fri, 23 Mar 2012 13:48:39 +0000 |

I think with your approach you need to worry about even more: 1. What about extra space as well _before_ each word to be substituted? Otherwise zapping "nana " could change "banana " to "ba". 2. What is above solves one problem but creates another as commonly there is no initial space at the beginning of strings. 3. To worry about side-effects, e.g, by zapping spaces you may combine two words into one. With variable names that just will produce errors. You could take your approach further by replacing "foo " with " ". I use -subinstr- all the time. But I think it's a bad idea for processing varlists. Nick On Fri, Mar 23, 2012 at 1:31 PM, Eric Booth <eric.a.booth@gmail.com> wrote: > <> > > On Mar 23, 2012, at 4:44 AM, Nick Cox wrote: >> 1. As I should have remembered, -subinstr- can bite you. It never >> promises to act on "words", meaning here variable names. Consider >> >> . local stuff "aaa aa a" >> >> . tokenize "`stuff'" >> >> . forval i = 1/3 { >> 2. local show : subinstr local stuff "``i''" "" >> 3. di "`show'" >> 4. } >> aa a >> a aa a >> aa aa a >> >> Here, when asked to zap "aaa", it finds it and deletes it. When asked >> to zap "aa" it zaps it as part of "aaa", and similarly with "a" as >> part of "aaa". With real variable names, this might bite as an error, >> or you might get garbage. > > > Here's another way I've done it in the past (but this is sloppier than the method(s) Nick suggests): > Since variable names don't have spaces, you could change the extended_fcn -subinstr- from > >> local show : subinstr local stuff "`i'" "" > > to > >> local show : subinstr local stuff "`i' " "" > > > where a space is added after the thing you are substituting 'from' so that you are getting full words substituted. > To do so, you'd need to add an extra space to the end of the macro or the command above would miss the last var in your macro (which could cause other issues if you use this macro elsewhere). An illustration: > > > ********** > clear > set obs 1 > foreach x in a aa aaa aaaa { > g `x' = 1 > } > > > **the last run is wrong > ds > loc stuff `r(varlist)' > foreach i in `stuff' { > local show : subinstr local stuff "`i' " "" //note the space trailing `i' > di "`show'" > } > > **corrected: > ds > loc stuff `r(varlist)' > loc stuff `"`stuff' "' //added > foreach i in `stuff' { > local show : subinstr local stuff "`i' " "" //note the space trailing `i' > di "`show'" > } > **************** > > > - Eric > > > __ > Eric A. Booth > Public Policy Research Institute > Texas A&M University > ebooth@ppri.tamu.edu > +979.845.6754 > > > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**RE: st: use of subinstr***From:*tashi lama <ltashi32@hotmail.com>

**Re: st: use of subinstr***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: use of subinstr***From:*tashi lama <ltashi32@hotmail.com>

**Re: st: use of subinstr***From:*Eric Booth <eric.a.booth@gmail.com>

**Re: st: use of subinstr***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: use of subinstr***From:*Eric Booth <eric.a.booth@gmail.com>

**Re: st: use of subinstr***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: use of subinstr***From:*Eric Booth <eric.a.booth@gmail.com>

- Prev by Date:
**st: AW: RE: Calling -ml- program with more than one equation!** - Next by Date:
**Re: st: -alpha-, unexpected behavior** - Previous by thread:
**Re: st: use of subinstr** - Next by thread:
**Re: st: use of subinstr** - Index(es):