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

st: RE: tempname


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: tempname
Date   Fri, 4 Oct 2002 17:41:50 +0100

Jun Xu

> I have a question related to tempname.  I have a series of
> matrices obtained
> through post-estimation, and the number of matrices is
> determined by the
> info in the return or est list.  Further, I need to
> tempname these matrices.
>   I thought of an awkward way, not sure if there is any
> command in Stata can
> take care of that.  To make my question clear, I use a mock
> example that's
> similar to mine, but much simpler:
> *******************************************************
> capture program drop what
> program define what
> forval i=1/4 {
> tempname mat`i'
> }
> forval i=1/4 {
> mat `mat`i''=[`i']
> mat list `mat`i''
>
> }
> end
>
> what
> *******************************************************
>
> Is there any options after tempname that can facilitate such needs
> especially when I have several, not just the mat`i''s.  Many thanks.

I'm not clear from this exactly what you want to do,
but you appear to have all the technique you need to
get what you want in this area.

Note first that your two loops could be combined.

There's at least one extra trick which is worth knowing, however.
Within a loop controlled by -forvalues- or -foreach- (or
indeed -while-)

	tempname mymat

always generates a _new_ tempname, even if
`mymat' already exists. This is
not, to me, intuitive, but it's what
happens.

If you want to make later use of
_all_ the names you must put all them in a bag
for later use:

	local mymats "`mymats' `mymat'"

	...

	foreach m of local mymats {
		....
	}

This isn't radically new technique
over what you have, but it permits
less awkward code.

Nick
[email protected]

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