Stata Technical Bulletin
STB-20
dm20 Date functions
Alan Riley, Stata Corporation, FAX 409-696-4601
Since the publication in the STB of A library of time series programs (see sts7.3 below), Stata Corp. has received numerous
requests for additional commands to manipulate dates. One request in particular piqued our interest and led to the development
of the commands described below. The user in question maintains Stata data sets of daily financial data which are collapsed
to create monthly averages and then used to generate financial forecasts. The Stata commands used to calculate these averages
and forecasts are stored in do-files that are executed automatically (they are executed as cron jobs on a Unix system). The user
wanted to make sure only full months of daily data were used; if the most recent month was not yet over, the user wanted to
discard its data.
This is a more subtle problem than it first appears. How can we tell in a do-file if the last observation comes from the last
day of the month? We wrote lastday to answer this question. But the problem isn’t solved yet. The last day of the month may
fall on a weekend. How can we tell if the last observation falls on the last business day of the month? Iastbday addresses this
question. Now, when a forecast is produced, how can we distinguish the historical observations from the projections? One way
is to compare the observation date to today’s date. Hence, the today command.
I hope the following commands will satisfy many of your requests. I am particularly interested in hearing from users about
other date-related problems for which Stata may not yet provide a ready solution. Any other comments or suggestions that you
have concerning these date-conversion programs are also appreciated.
New date commands
downame dowvar, generate (dvwar)
namedow dayvar, generat e(dowvar)
mnthname mvar, generate mnthaar)
namemnth mthvar, generate (mvar)
mdytodow mvardvaryvar, generate (dowv`ar)
lastday mvaryvar, genera,te(dvar)
Iastbday mvaryvar, generate(dvar)
ystrday mvar dvaryvar, generate (mvar dvaryvar)
today, generatemnaardvaryvar)
where
dayvar string variable containing day (Sunday-Saturday)
mthvar string variable containing month (January-December)
dowvar, dvar mvar, and yvar are defined as in [5d] dates.
These commands perform various date conversions. downame converts a numeric day of week to the corresponding name.
namedow performs the opposite conversion. Likewise, mnthname converts a numeric month to the corresponding name, and
namemnth performs the opposite conversion. mdytodow calculates the numeric day of the week from the month, day, and year.
lastday calculates the last calendar day for a given month and year, while Iastbday calculates the last business day for a
particular month and year. (“Business day” is defined as a day from Monday through Friday. Holidays are not taken into account).
ystrday calculates the previous month, day, and year given any month, day, and year. today simply reads the S-DATE
global macro (see [2] macros) and generates variables containing the current month, day, and year.