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

st: RE: temporary variables (was: xpose or reshape)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: temporary variables (was: xpose or reshape)
Date   Tue, 12 Oct 2004 19:54:55 +0100

The main thing is not to use a variable at 
all. 

local tvar = lower(_var`i'[1]) 
local tvar = subinstr("`tvar'"," ","",.)
local tvar = subinstr("`tvar'","#","no_",.)
local tvar = subinstr("`tvar'","%","pct_",.)
local tvar = subinstr("`tvar'","(","_",.)
local tvar = subinstr("`tvar'",")","",.)
capture rename _var`i' `tvar' 

Nick 
[email protected] 

Wallace, John

> > The final step in my original transposition problem was to get the
> > variable names out of the first row of observations (see end of
> > message).  I've accomplished this with the following do-file 
> > code, but I
> > suspect it could be tightened up some, particularly with 
> regard to the
> > way I'm handling a temporary variable.
> > 
> > ----------begin good code--------------
> > qui d
> > local no_vars = r(k)
> > forval i=1/`no_vars'{
> > 	local tvarname: di _var`i'[1]
> > 	gen tvar = "`tvarname'"
> > 	qui replace tvar = lower(tvar)
> > 	qui replace tvar = subinstr(tvar," ","",.)
> > 	qui replace tvar = subinstr(tvar,"#","no_",.)
> > 	qui replace tvar = subinstr(tvar,"%","pct_",.)
> > 	qui replace tvar = subinstr(tvar,"(","_",.)
> > 	qui replace tvar = subinstr(tvar,")","",.)
> > 	local tvarname: di tvar[1]
> > 	qui drop tvar
> > 	capture rename _var`i' `tvarname'
> > }
> > drop in 1
> > ---------end good code-----------------
> > 
> > Rather than using -gen tvar- I thought I should be able to use
> > . tempvar tvar
> > . set `tvar'[1] = "'tvarname'"
> > . qui replace `tvar' = lower(`tvar') 
> > etc
> > 
> > but I get errors when I try this: -set __000000- not allowed; 
> > '__000000'
> > not recognized

*
*   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