Statalist


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

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


From   "Kieran McCaul" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: RE: forvalues loop shuts down when asked to "jump over" certain values
Date   Sat, 9 Jan 2010 06:25:43 +0800

...

Point taken, Nick, how about this:


shell dir *survey.csv > files.txt /A:-D /B

clear

insheet using files.txt
levelsof  v1, local(files)

foreach f of local files {
	di in r  "`f' " 
}

No need for the -confirm- since if it's in the dir listing then it
exists.



-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: Saturday, 9 January 2010 3:06 AM
To: [email protected]
Subject: st: RE: RE: forvalues loop shuts down when asked to "jump over"
certain values

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/


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