Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: AW: Nested loops


From   Nick Cox <n.j.cox@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: Nested loops
Date   Thu, 06 Aug 2009 09:20:28 -0500

I don't know why Sarah is doing this, but in any event
I'd add a warning. When Stata creates temporary variables it uses variable names that begin with double underscores. You may type

. tempvar foobar

. gen `foobar' = 42

but Stata uses a variable name like __000111 and keeps up the translation `foobar' to __000111 so that you need not keep track of the underlying variable name. But often those names will be visible, not least in error messages.

However, there is no rule to prohibit your directly defining variables (or other things) with such names, as such names are perfectly legal. But watch out! There is now scope for moderately subtle bugs or misbehaviours. For example,

1. Stata will without warning drop variables you create beginning with __ on the grounds that they are temporary.

2. There is scope for confusion with other beasts whose names begin with __, say local macros. That may or may not apply here.

Regardless of syntax, I recommend never naming anything with a name beginning with __, even if for some reason that prefix seems natural. The occasional convenience of being able to do that is likely to be outweighed by bizarre results you don't understand or can't debug. You would be safe with a prefix such as "my", e.g. "my__001122".

Nick

Martin Weiss wrote:

Try this, with the caveat that your strategy looks like blowing a simple
issue out of all proportions...


*************
clear*
set obs 10


local shock "__31002 __31003 __31004 __31005a __31006a __31007 __31008
__31009 __31010 __31011 __31012a"
local miss "type time sev inc ass idio cop1 cop2 cop3 exp recov"

//"shock" members must exist...

foreach lev of local shock{
	gen `lev'=.
}

loc i 1

foreach level of local miss{
	local nextlevel "`:word `i' of `shock''"
	// di "`nextlevel'"
		gen `level'_un =(`nextlevel' == .a)
		gen `level'_uncl = (`nextlevel' == 90 | `nextlevel' == .b)
		gen `level'_miss = (`nextlevel' ==.)
		loc ++i
}
*************

Sarah Linde

I created the two macros, shock and miss.

local shock "__31002 __31003 __31004 __31005a __31006a __31007 __31008
__31009 __31010 __31011 __31012a"
local miss "type time sev inc ass idio cop1 cop2 cop3 exp recov"

Now I want to use loops to automate the following tasks:

gen type_un =(__31002 == .a)
gen type_uncl = (__31002 == 90 | __31002 == .b)
gen type_miss = (__31002 ==.)

gen time_un = (__31003 ==.a)
gen time_uncl = (__31003 == 90 | __31003 ==.b)
gen time_miss = (__31003 ==.)

gen sev_un = (__31004 ==.a) ...and so on


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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