Two points:
1. The -ds- command does quite a lot of work for what is needed
here. An efficient alternative is
cap confirm variable `m'`y'
2. Austin's use of `c(Mons)' underlines that some useful
little lists are hard-wired into Stata. To see some others,
type
creturn list
and scroll to the end. Some discussion was given at
SJ-4-2 pr0013 . . . . . . . . . . . Stata tip 9: Following special sequences
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/04 SJ 4(2):223 (no commands)
tip for using creturn to access special sequences stored
as c-class values
and this is reprinted in "33 Stata Tips" from StataCorp.
Nick
[email protected]
Austin Nichols
> Thomas Speidel--
> The -capture- command does not work any magic you want it to. It
> simply suppresses any error messages, so a command that fails to run
> and returns an error will still fail to run when preceded by -cap-.
> What you want to do is put together a list in a -local- of the
> variables that exist in your data, e.g.
> clear
> set obs 10
> gen Sector=1
> gen Feb2007=2
> gen Dec2006=6
> gen Oct2006=4
> gen Nov2006=84
> gen Sep2006=897
> gen Jan2007=4.3
> local oklist
> forv y=2005/2009 {
> foreach m in `c(Mons)' {
> cap ds `m'`y'
> if _rc==0 local oklist `oklist' `m'`y'
> }
> }
> order `oklist'
> d
>
> On 6/12/07, Thomas Speidel <[email protected]> wrote:
> > I am trying to order the variables of a dataset in a chronological
> > fashion (the month+year is stored in the the varname - the
> content of
> > the variables is _not_ a date). I have to allow for the
> possibility of
> > additional variables reflecting future/past dates, hence the lengthy
> > order command. I am not sure why this is happening, since
> I thought the
> > capture option should take care of it, but the following
> code does not
> > produce what I had expected:
> >
> > set obs 10
> > gen Sector=1
> > gen Feb2007=2
> > gen Dec2006=6
> > gen Oct2006=4
> > gen Nov2006=84
> > gen Sep2006=897
> > gen Jan2007=4.3
> > capture order Sector Jan2006 Feb2006 Mar2006 Apr2006 May2006 Jun2006
> > Jul2006 Aug2006 Sep2006 Oct2006 Nov2006 Dec2006 Jan2007
> Feb2007 Mar2007
> > Apr2007 May2007 Jun2007 Jul2007 Aug2007 Sep2007 Oct2007
> Nov2007 Dec2007
> > Jan2008 Feb2008 Mar2008 Apr2008 May2008 Jun2008 Jul2008
> Aug2008 Sep2008
> > Oct2008 Nov2008 Dec2008
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/