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

st: RE: RE: 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: RE: RE: forvalues loop shuts down when asked to "jump over" certain values
Date   Mon, 11 Jan 2010 19:07:05 -0000

This strategy certainly seems reasonable. 

That is, there are two broad approaches to this problem. 

1. Gather the filenames using Stata or your OS. 

2. Cycle over the numeric elements of the filename pattern, checking for

You here illustrate #1 assuming Windows (note use of -dir-). People on
other platforms could use an equivalent with the command -ls-. 

Earlier contributions in the thread showed how to do #1 directly in
Stata using a macro extended function (or equivalently using the
convenience wrapper -fs- from SSC). 

I agree that focusing on the files (#1) is the better solution to the
original problem. My main concern in my previous post was to explain why
#2 as attempted by Dorothy did not work well because -forvalues- is best
done over integers. 

[email protected] 

Kieran McCaul

Point taken, Nick, how about this:

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


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

Nick Cox

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.

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