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: local and loops


From   Jeph Herrin <[email protected]>
To   [email protected]
Subject   Re: st: local and loops
Date   Tue, 20 Aug 2013 15:34:48 -0400

I see the same behaviour as Matt - the -local- assignment produces different results when within the loop than when without it. It is the same difference whether typed interactively or run from a -do- file.

Jeph


On 8/20/2013 3:21 PM, Nick Cox wrote:
Interesting. For the record, I am currently using Stata 10.1 on a
Mesolithic machine. It trims both inside and outside loops.

Nick
[email protected]


On 20 August 2013 20:17, Matthew White <[email protected]> wrote:
Hi Nick,

Thanks for your reply.

There were supposed to be exactly two spaces between 1 and 2, and the
second string specified to -strpos()- was supposed to be two
consecutive spaces.

Outside of a loop, the internal spaces in `foo' are actually preserved
on my computer, even without the double quotes:

. local foo a                  b                  c

. display strpos("`foo'", "  ")
2

. display `:length local foo'
39

If the spaces were trimmed, the length of `foo' would be 5.

However, when I put this in a loop, I get different results:

. forvalues i = 1/5 {
   2. local foo a                  b                  c
   3. }

. display strpos("`foo'", "  ")
0

. display `:length local foo'
5

When you define `foo' outside of a loop, do you get a length of 5 or 39?

Inside and outside of a loop, leading and trailing spaces are trimmed
unless double quotes are used, but as far as I can tell, internal
spaces are typically left untrimmed.

Thank you,
Matt

On Tue, Aug 20, 2013 at 3:05 PM, Nick Cox <[email protected]> wrote:
How many spaces are there between "1" and "2"?

Without " " as delimiters, Stata trims internal spaces to single
spaces in local macro definition. The " " insist on taking things
quite literally.

Try

local foo a                  b                  c

and see what happens.

So, -strpos()- can't find a double space as there isn't one.

I don't think looping is germane here.

Nick
[email protected]


On 20 August 2013 19:52, Matthew White <[email protected]> wrote:
Hi all,

Using Stata 13 for Windows 7, I'm trying to figure out an interaction
between -local- and loops. Normally when I define a local, multiple,
consecutive internal spaces are preserved. For example:

. local x 1  2

. display strpos("`x'", "  ")
2

Yet when I use the same definition in a loop, the internal spaces are trimmed:

. forvalues i = 1/5 {
   2. local x 1  2
   3. }

. display strpos("`x'", "  ")
0

Have others experienced this before? It's probably rare that this
would bite, but I'm running into such a situation now. Enclosing the
contents of the local by double quotes solves the problem:

. forvalues i = 1/5 {
   2. local x "1  2"
   3. }

. display strpos("`x'", "  ")
2

I'm just so used to dropping these quotes when I don't need them!

Thank you,
Matt

--
Matthew White
Senior Project Associate
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
www.poverty-action.org
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/



--
Matthew White
Senior Project Associate
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
www.poverty-action.org
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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