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

st: temporary variables (was: xpose or reshape)


From   "Wallace, John" <[email protected]>
To   <[email protected]>
Subject   st: temporary variables (was: xpose or reshape)
Date   Tue, 12 Oct 2004 10:47:59 -0700

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

-JW

-----Original Message-----
From: Nick Cox [mailto:[email protected]] 
Sent: Tuesday, October 12, 2004 5:08 AM
To: [email protected]
Subject: st: RE: RE: xpose or reshape

A step or two more general. I'll post on SSC. 

*! NJC 1.0.0 12 Oct 2004 
program sxpose 
	version 8 
	syntax , clear [ firstnames destring ] 

(code snipped for brevity)

Nick 
[email protected] 

> -----Original Message-----

Wallace, John
(snipped)
It looks like a clear case for -xpose-, except that all the data are
brought into Stata as strings because the first row (filenames) are
strings.  -xpose- results in an empty dataset.  I'm looking at reshape
and stack, but I can't figure out how they could accomplish what I'm
looking for:

	v1	v2		v3		v4		v5
v6	v7	v8
1	v1	.		Date		type		group
noise	bkd	bkdsd
2	v2	foo1.chp	10/11/2004	U133A_2	lymph	1.4
46.7	0.28
3	v3	foo2.chp	10/11/2004	U133A_2	lymph	1.24
50.4	0.28
4	v4	foo3.chp	10/11/2004	U133A_2	lymph	1.5
32.4	0.26
5	v5	foo4.chp	10/11/2004	U133A_2	heart	1.25
18.6	0.09

(or actually, ideally)

	v1		Date		type		group	noise
bkd	bkdsd
1	foo1.chp	10/11/2004	U133A_2	lymph	1.4	46.7
0.28
2	foo2.chp	10/11/2004	U133A_2	lymph	1.24	50.4
0.28
3	foo3.chp	10/11/2004	U133A_2	lymph	1.5	32.4
0.26
4	foo4.chp	10/11/2004	U133A_2	heart	1.25	18.6
0.09

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