Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: putting string variable values into macros


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: putting string variable values into macros
Date   Mon, 18 Jul 2005 21:54:37 +0100

Both Michael Blasnik and Ben Jann suggested 

forval i = 1/20 { 
	local mac`n' = x[`n'] 
}

In some circumstances that could bite. Evaluating 
rather than copying could lose parts of long 
strings and/or leading blanks. A more general 
solution is 

forval i = 1/20 { 
	local mac`n' `"`=x[`n']'"' 
}

Note that 

gen obs=_n
forv n=1/20 {
 	qui su x if obs==`n', meanonly
 	local mac`n' = r(mean)
}

can be replaced by almost the same 
solution, thus avoiding the triple inefficiency 
of an extra variable, a repeated -summarize-, 
and a -summarize if- to boot: 

forv n=1/20 {
 	local mac`n' = x[`n'] 
}

Nick 
[email protected] 

reardon, sean f.
 
> i want to create a series of local macros that contain the values of a
> string variable:  something like this:
> 
> gen obs=_n
> forv n=1/20 {
> 	local mac`n' "value of string variable x if obs==`n'"
> }
> 
> the idea is i would end up with 20 macros (mac1 mac2, mac3, 
> ..., mac20),
> each of which would contain the corresponding value of a 
> string variable
> x.
> 
> i can do this if x is a numeric variable like this:
> 
> gen obs=_n
> forv n=1/20 {
> 	qui su x if obs==`n', meanonly
> 	local mac`n' = r(mean)
> }
> 
> but i can't think of how to do it x is a string variable. i'm sure
> there's something obvious, but i'm stuck.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index