[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. 


. di %20.18f 1/10

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

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

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

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.

[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:


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:

© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index