Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

st: RE: AW: AW: confirm


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: AW: AW: confirm
Date   Wed, 18 Aug 2010 12:28:37 +0100

It's entirely a side issue, but it is easy to avoid spelling out 01, 02, 03, ..., 09. You just map integers to integers displayed in leading zero format: 

forval i = 1/31 { 
	local I : di %02.0f `i' 
	<code using `I'> 
} 

For more detail, if desired, see (despite the title) 

SJ-10-1 pr0051  . . . . . . . . . . . . Stata tip 85: Looping over nonintegers
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/10   SJ 10(1):160--163                                (no commands)
        tip on using forvalues to loop over numbers

Nick 
n.j.cox@durham.ac.uk 

Martin Weiss

Like so:

clear*

//generate files
forv i =1/9{
	drop _all
	gen var1=1
	save 2006010`i', replace
}

//make "10" a missing file
forv i =11/20{
	drop _all
	gen var1=1
	save 200601`i', replace
}


//check for presence of files
local YEAR  2006
local MONTH 01 02 
local DAY 01 02 03 04 05 06 07 /* 
*/  08 09 10 11 12 13 14 15 16  /* 
*/ 17 18 19 20 


foreach Y of local YEAR  {
foreach M of local MONTH {
foreach D of local DAY {

scalar day = mdy(`M',`D',`Y')
scalar satsun = dow(day)

if !inlist(satsun,.,0,6){
	capt conf file "`Y'`M'`D'.dta"
	if _rc{
		di as err "This file `Y'`M'`D'.dta is not there, I am afraid"
	}
	else{
		di in red "This file `Y'`M'`D'.dta is already there!"
	}
}

}
}
}

Martin Weiss

Just -capture- the -confirm- statement, and condition on the return code

sysuse auto, clear
foreach var in newvariable price weight{
capt conf new variab `var'
di in red _rc
}

Dimitri Szerman

Here's what I'm trying to do. I have a directory with many files, one for
each weekday of the year. Some files are missing, and I want to find out
which files are missing. The filenames make it easier to use a loop and
confirm, and I came up with this:

local YEAR  2006
local MONTH 01 02 03 04 05 06 07 08 09 10 11 12
local DAY 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31

 foreach Y of local YEAR  {
 foreach M of local MONTH {
 foreach D of local DAY {

 scalar day = mdy(`M',`D',`Y')
 scalar satsun = dow(day)

if satsun!=0 & satsun!=6 & satsun!=. {
confirm file "`Y'`M'`D'.csv"
}

 }
 }
 }

Now, my problem is, this code will stop on the first missing file. What I'd
like is to have is Stata printing a list of missing files. Any ideas would
be much appreciated.


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index