Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: use of subinstr


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: use of subinstr
Date   Fri, 23 Mar 2012 14:09:55 +0000

The last sentence was too dogmatic. -subinstr- can be fine for some
problems with varlists. If it offers an easy and correct solution, go
for it.

On Fri, Mar 23, 2012 at 1:48 PM, Nick Cox <[email protected]> wrote:
> 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 <[email protected]> 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
>> [email protected]
>> +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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index