Bookmark and Share

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]

st: Local Macros Containing Strings and Spaces


From   Erika Kociolek <ekociole@gmail.com>
To   statalist@hsphsun2.harvard.edu
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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index