Masks
One way to get free Stata help is to post your question to the Statalist. A dedicated band of Stata users will go at it like a school of hungry piranhas at a drowning zebu, and odds are excellent that whatever baffled you will be ripped apart practically while you watch. Another is to just blog about your work.
A while ago I posted about keeping track of files I receive every week, with names such as client_YYYYMMDD.txt. I don't receive them on the same day, and I get a lot of them from several clients. It'd be nice to be able to make Stata look for them without me having to spell out the date of arrival. Well, there is a way. The YYYYMMDD variation poses no more of a challenge than any other way of describing a date -- e.g. December 4, 2008 -- once you get acquainted with the mask
feature of Stata's clock and date functions. Take this code for example:
local clients "foo bar"
local start=date("1nov2008","dmy")
local end=date(c(current_date),"dmy")
foreach client in `clients' {
forvalues i=`start'/`end' {
local x=string(`i',"%tdCYND") // one way to YYYYMMDD
local x: display %tdCYND `i' // and here's another
capture confirm file "`file_path'`client'_`x'.txt"
if _rc==0 {
di "Success. Got a file from `client' on `i'."
// your code goes here
}
}
}
This is a beautiful solution. Steli's comment in response to my post on leading zeroes had sent me riffling through the friendly manual (Data Management volume, dates and times section; page 76 if your manual is for Stata 10). That enlightened me on how the masks on Stata's time and clock functions work. The time to deploy that grain of wisdom arrived a few minutes ago.
One last thing: you may have noticed that this example is written in Stata 9. In Stata 10 the %td mask would be a little different, and the "dmy" part of the date function would be "DMY".