Statalist


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

RE: RE: Re: st: looping from 1 to a number defined by a scalar


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: RE: Re: st: looping from 1 to a number defined by a scalar
Date   Mon, 7 Jan 2008 15:44:41 -0000

I don't see that anyone mentioned that it is a bad 
idea to use a scalar for this purpose, because 
scalars and variables share the same name space. 

Here is a dopey example. 

. sysuse auto
(1978 Automobile Data)

. scalar mpg = 10

. forval i = 1/`=mpg' {
  2. di `i'
  3. }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

And another 

. scalar m = 10

. forval i = 1/`=m' {
  2. di `i'
  3. }
m ambiguous abbreviation
invalid syntax
r(198);

Asked to look for -mpg-, Stata uses the value -mpg[1]-. 

Asked to look for -m-, Stata can't decide whether you want 
-mpg[1]- or -make[1]-. 

This in practice might not bite very often, but when it does, it 
could prove very puzzling and waste a lot of your time. 

The problem is all documented, but quite simply, don't do this. 
Use locals instead! 

Nick
[email protected] 
 

[email protected]

There might be some typos in the previous code I said it worked. 
I would like to assure you that the = is required and it works:

. forval i=1/`=ng' {
  2. mat p=inv((r1+vu)/ns[`i'])*r1
  3. mat q=r2*(idd-p)
  4. mat xtilde`i' = xmean*p +mux3*(idd-p)+dz*q
  5. }


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