| 
    
 |   | 
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: -forvalues- fails to reach upper limit of range
Dear statalist,
Using Stata 8.2, I am having trouble repeating a process the correct  
number of times using -forvalues-.  The range is specified as (e.g.)
forval i = 1(.1)2
Here, 2 is the upper limit of the range.  But the final operation  
involving `i' ends at 1.9 rather than 2.
A simple example follows.  The second -forval- encloses a command to  
put the value of the counter `i' in a variable repeatedly.  The first  
-forval- causes this to repeat for different upper limits of the  
inner -forval- loop.  The output is a summary of the resulting  
variables.  Note the maximum values.  I expect the maximum value to  
be 1, then 2, then 3, etc.  Thanks for any help.
- Richard
. forval u = 1/10 {
  2.         clear
  3.         set obs 100
  4.         gen x = .
  5.         forval i = 1(.1)`u'             {
  6.                 local n = float(10*(`i'))
  7.                 qui replace x = `i' in `n'
  8.                 }
  9.         su
10. }
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |         1           1           .          1          1
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        10        1.45     .302765          1        1.9
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        20        1.95     .591608          1        2.9
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        30        2.45    .8803408          1        3.9
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        41           3    1.197915          1          5
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        51         3.5    1.486607          1          6
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        61           4    1.775293          1          7
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        71         4.5    2.063977          1          8
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        81           5    2.352658          1          9
obs was 0, now 100
(100 missing values generated)
    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           x |        91         5.5    2.641338          1         10
.
end of do-file
-------------------------------------------------
Richard Sherman
Department of Political Science, Leiden University
Leiden, Netherlands
http://homepage.mac.com/richard.sherman
*
*   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/