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 |
tashi lama <ltashi32@hotmail.com> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: use of subinstr |

Date |
Fri, 23 Mar 2012 14:15:42 +0000 |

Thank you guys for your help. I am a beginer in stata programming although my little background in C++ and BASIC programming has helped me a lot to understand and learn Stata programming. I have few questions on stata basics which I hope you guys will throw some light on or at least give me a lead. 1. What are extended functions and why do we use it? My initial thought would be to extend the length of the string like you guys did for subinstr. 2. when you say unab varlist :var* what is the code doing? As a matter of fact, what is unab? Is varlist a macro here? Why are we using ":" here? Actually what is ":"? 3. when you say ds var* local varlist `r(varlist)' for each x of local varlist { code... } instead of 3., could we have said foreach x of varlist var* { code... } Please explain me what is the diff between two codes. And when you say local varlist `r(varlist)', what are you doing? are you defining a macro called varlist and storing it in r(varlist)? If that is the case, I don't understand why is that we have to store? Pls explain. Thanks again so much, Tashi > Date: Fri, 23 Mar 2012 13:48:39 +0000 > Subject: Re: st: use of subinstr > From: njcoxstata@gmail.com > To: statalist@hsphsun2.harvard.edu > > 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/ * * 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:*Nick Cox <n.j.cox@durham.ac.uk>

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

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

- Prev by Date:
**Re: st: use of subinstr** - Next by Date:
**st: Favorite Stata command contest results** - Previous by thread:
**Re: st: use of subinstr** - Next by thread:
**RE: st: use of subinstr** - Index(es):