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 Macros Containing Strings and Spaces


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Local Macros Containing Strings and Spaces
Date   Sat, 6 Oct 2012 20:52:42 +0100

For "a more subtle program" read "a more subtle problem".

On Sat, Oct 6, 2012 at 8:48 PM, Nick Cox <[email protected]> wrote:
> Bottom line first. A space is just one character, so in principle
> nothing different is needed.
>
> Looking at your code
>
>  forvalues i = 1/_N{
>     foreach v of local dairy{
>       replace grouping = "Dairy" if description == "`v'"
>     }
>     foreach m of local fruit{
>       replace grouping = "Fruit" if description == "`m'"
>     }
>  }
>
> the outer loop is totally unnecessary. Note also that nothing within
> the outer loop does things differently referring to the current value
> of `i'. Stata automatically loops over observations with syntax like
> this.
>
> You don't
> spell out what "doesn't work" means, but Stata will object to the
> literal "_N" any way as -forvalues- will not evaluate it for you.
> Also, a loop over one value is not a problem, but superfluous.
>
> A more subtle program is that
>
> local dairy "Organic Eggs" "Nonfat Milk"
>
> won't work as you want, as the outer " " get stripped as string delimiters.
>
> Code like this.
>
>  local dairy `" "Organic Eggs" "Nonfat Milk" "'
> local fruit "Conventional Cherries"
>
> generate grouping == ""
>
> foreach v of local dairy{
>       replace grouping = "Dairy" if description == "`v'"
> }
> replace grouping = "Fruit" if description == "`fruit'"
>
> should work.
>
> Nick
>
> On Sat, Oct 6, 2012 at 7:48 PM, Erika Kociolek <[email protected]> wrote:
>
>> My question relates to strings in local macros with spaces.  I'll
>> first relate what I am trying to do. I have a string variable that has
>> spaces in it.  It looks something like this:
>>
>> description
>> Organic Eggs
>> Nonfat Milk
>> Conventional Cherries
>>
>> For each observation, I would like to generate a new variable,
>> grouping, based on the content of this description variable.  I.e.
>>
>> generate grouping = ""
>> replace grouping = "Poultry/Dairy" if (description == "Organic Eggs" |
>> description == "Nonfat Milk")
>>
>> However, I have so many description values that I would rather do
>> something like:
>>
>> local dairy "Organic Eggs" "Nonfat Milk"
>> local fruit "Conventional Cherries"
>>
>> generate grouping == ""
>>
>> forvalues i = 1/_N{
>>    foreach v of local dairy{
>>      replace grouping = "Dairy" if description == "`v'"
>>    }
>>    foreach m of local fruit{
>>      replace grouping = "Fruit" if description == "`m'"
>>    }
>> }
>>
>> This does not work. In troubleshooting this problem, I noticed that
>> when I write the command: display "`poultry'", Stata returns Organic
>> EggsNonfat Milk. If I write the command: display `"`poultry'"' Stata
>> returns Organic Eggs" "Nonfat Milk. My question is, how do I address
>> strings with spaces in local macros?
*
*   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