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   Richard Williams <[email protected]>
To   [email protected]
Subject   Re: st: syntax error in forvalues range definition using a tempvar??
Date   Sat, 16 Jul 2005 21:50:57 -0500

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



-------------------------------------------
Richard Williams, Notre Dame Dept of Sociology
OFFICE: (574)631-6668, (574)631-6463
FAX: (574)288-4373
HOME: (574)289-5227
EMAIL: [email protected]
WWW (personal): http://www.nd.edu/~rwilliam
WWW (department): http://www.nd.edu/~soc

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