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]

st: Local Macros Containing Strings and Spaces


From   Erika Kociolek <[email protected]>
To   [email protected]
Subject   st: Local Macros Containing Strings and Spaces
Date   Sat, 6 Oct 2012 11:48:07 -0700

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?

Thank you in advance!
*
*   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