Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: local macro tempvar troubles


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: local macro tempvar troubles
Date   Wed, 3 Oct 2007 12:55:39 +0100

I get the impression this isn't your real code. 

The essence of what I think is biting you can
be seen in 

	clear
	set obs 100
	tempvar frog  
	gen `frog' = 0 
	gen `toad' = 0 

If you run this, generation of `frog' will
work, but not that of `toad'. Why not? 

1. Local macros that have not been defined 
are treated as empty (null). Referring to 
a local macro that does not yet exist is 
not in itself an error, but its consequences
may be. In this case, `toad' does not exist, 
so Stata sees 

	gen = 0 

which is illegal in Stata. Specifying a 
-double- type does not affect this issue. 

2. You have two ways of declaring a local 
macro to Stata. You can explicitly define 
one 

	local answer "Stata" 
	local answer = 42 

When you do that you specify name (answer)
and contents (whatever they are) in one go. 

Alternatively, you can flag that you 
will be using a macro with a particular 
name. 

	tempname toad 

flags that you will be referring to `toad' 
later in your program. Stata will create a unique name
and let you use `toad' as an alias for it. 
In your case you want to create a temporary
variable, so the alternative 

	tempvar toad 

makes your intention even clearer. 

In your example you had a typo that you found. 
It's my bet, although it's not obvious from 
your example, that in your real code there is at
least one other typo. It's likely to be more subtle than
the difference between `frog' and `toad'. But it will
be there. 

In well-developed text editors, you will have a command that
lets you search for the identifier under the cursor, 
or that in equivalent terminology. So, this kind of error is 
easier to spot in a good editor. 

Nick 
n.j.cox@durham.ac.uk 

Timothy Dang
 
> I'm using Stata/IC 10.0 on a Mac, just in case that matters.
> 
> I'm writing a .do file using -ml- . I don't think that matters, but in
> any case, in the likelihood program I generate a lot of temporary
> variables, like so:
> -----
>     tempvar prHeart prSpade prClub prDmd
>     tempvar valMix valHt valSpd valClub valDiamond
>     tempvar MixLove expHeart expSpade expClub expDiamond expSum
> 
> 	gen double `prHeart'=0
> 	gen double `prSpade'=0
> 	gen double `prClub'=0
> 	gen double `prDmd'=0
> 	gen double `valMix'=0
> 	gen double `valHt'=0
> 	gen double `valSpd'=0    // concentrate on this line
> 	gen double `valClub'=0
> 	gen double `valDiamond'=0
> -----
> 
> On the line which says "gen double `valSpd'=0", it originally said
> "gen double `valSpade'=0". There was trouble with that, though. When I
> ran the program, it appeared that `valSpade' was null--it replaced the
> line with "gen double =0".
> 
> I "fixed" the problem by doing a search/replace all from "valSpade" to
> "valSpd". For some reason, that actually DID fix the problem. Now, the
> same thing is happening with the last line above. For the valSpd line
> it's nicely doing "gen double __00000S=0". For "gen double
> `valDiamond'=0" it makes "gen double =0", causing the error: "too few
> variables specified".
> 
> This kind of problem has been dogging me this evening, with different
> variable/macro names being the trouble. I'm flummoxed. Anyone know
> what I'm foolishly overlooking?

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index