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

RE: st: syntax error in forvalues range definition using a tempvar??


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: syntax error in forvalues range definition using a tempvar??
Date   Mon, 18 Jul 2005 13:06:32 +0100

Otherwise put, as I see it: 

In the context of -forvalues- `maxn' 
is a variable _name_ and 1/__000001 is unacceptable 
as an argument. 

In the context of -display- Stata leans over backwards 
to find a single something that can be displayed and, as 
Richard says, first sees the variable name and then 
jumps to displaying its value for the first observation.

forvalues i = 1/`=`maxn'[1]' { 


}

is another way to do it, but not as good as directly 
through -summarize-. (For this purpose, -summarize, meanonly-
would be even better.) 

Nick 
[email protected] 

Richard Williams
 
> At 05:00 PM 7/16/2005 -0700, Brian R. Sala wrote:
> >Can someone enlighten me on this syntax error, please.
> >
> >why does this fail:
> >
> >. tempvar maxn
> >
> >. egen byte `maxn' = max(assignment_count)
> >
> >. tab `maxn'
> >
> >    __000001 |      Freq.     Percent        Cum.
> >------------+-----------------------------------
> >           4 |     20,299      100.00      100.00
> >------------+-----------------------------------
> >       Total |     20,299      100.00
> >
> >. forvalues i = 1/`maxn' {
> >   2. di `i'
> >   3. }
> >invalid syntax
> >r(198);
> >
> >
> >clearly, `maxn' has a value. Superficially, this looks 
> equivalent to the 
> >syntax shown in the STATA 8 programming manual (pp. 206-207).
> 
> Actually, `maxn' is a variable and hence every case has a value for 
> it.  All cases may have the same value on the variable, but 
> it is still a 
> variable.  forvalues doesn't like you specifying a variable 
> here, it wants 
> a single number.
> 
> >yet when I do this:
> >
> >. tempvar max_assignments
> >. egen byte `max_assignments' = max(assignment_count)
> >. local maxn = `max_assignments'
> >. forvalues i = 1/`maxn' {
> >   2. display `i'
> >   3. }
> >1
> >2
> >3
> >4
> 
> Here, `maxn' is a constant and not a variable; what the local 
> statement has 
> done is set it equal to the value the first case has on 
> `max_assignments'.  Since every case has the same value on 
> `max_assignments' you are fine, but if `max_assignments' had 
> different 
> values you could be in trouble if the first case didn't have 
> the value you 
> want.
> 
> To further see this:
> 
> . webuse auto
> (1978 Automobile Data)
> 
> . local maxn = price
> 
> . di `maxn'
> 4099
> 
> 4099 is the value of the first case for price.
> 
> . sort length
> 
> . local maxn = price
> 
> . di `maxn'
> 3895
> 
> After sorting, we now have a different value for the first 
> case and hence 
> `maxn' now has a different value.
> 
> Perhaps a more common way to get what you want is to do 
> something like this:
> 
> . sum price
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>         price |        74    6165.257    2949.496       3291  
>     15906
> 
> . return list
> 
> scalars:
>                   r(N) =  74
>               r(sum_w) =  74
>                r(mean) =  6165.256756756757
>                 r(Var) =  8699525.974268789
>                  r(sd) =  2949.495884768919
>                 r(min) =  3291
>                 r(max) =  15906
>                 r(sum) =  456229
> 
> . local maxn = r(max)
> 
> . di `maxn'
> 15906
> 

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