Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: forvalues loop shuts down when asked to "jump over" certain values


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: forvalues loop shuts down when asked to "jump over" certain values
Date   Fri, 8 Jan 2010 19:06:15 -0000

Invoking -round(,)- is a bit of a red herring here. It maps one binary
approximation of a decimal number to another binary approximation and
can't by itself solve the fundamental problem that most calculations
that are exact in decimal are _not_ exact in binary. 

Consider 

. di %20.18f 1/10
0.100000000000000010

. di %20.18f round(1/10, 0.1)
0.100000000000000010

You may think 0.1 should be easy to hold, but you are not a binary
computer! 

Above, and also in Kieran's example,  the calls to -round(,)- will do
nothing -- which means no harm too. 

Inspection of the archives shows many emails saying in effect don't do
what Dorothy did. We'll run a Tip in Stata Journal 10(1) spelling out
the pitfall and what to do instead. 

My best advice is 

0. Always remember: You may think decimal, but Stata works in binary. 

1. Loop over integers if at all possible. 

2. Never depend on arithmetic with non-integers yielding exact decimal
results. 

Here's one approach that respects all those: 

forvalues i = 1/13 {
	forval j = 1/100 { 
		local J : di %02.0f `j' 
		capture confirm file `i'.`J'SURVEY.csv
		if _rc == 0 {
			<blah blah blah> 
		}
	} 
}

That's more complicated than Kieran's code, but I think more robust too.


Nick 
[email protected] 

Kieran McCaul

try the following:

forvalues i = 101(1)1301 {
local j = round(`i'/100,0.01)
capture confirm file `j'SURVEY.csv
if _rc==0 {
blah blah blah
}}

Dorothy Bridges

I have a forvalues loop along the following lines:

forvalues i = 1.01(.01)13.01 {
capture confirm file `i'SURVEY.csv
if _rc==0 {
blah blah blah
}
}

My list of files is along the lines of:

1.01SURVEY.csv
1.02SURVEY.csv
1.03SURVEY.csv
1.04DATA.csv
1.05SURVEY.csv.

The loop works perfectly for the first three files, then stops when it
gets to 1.04DATA ... I want it to go on to 1.05SURVEY.  I attempted to
solve this problem with the capture confirm line, but no luck.  I
think I need another expression along the lines of, "if _rc~=0,
(continue the loop)".

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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