Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Year to start 1st April


From   Richard Williams <Richard.A.Williams.5@nd.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Year to start 1st April
Date   Sun, 07 Mar 2004 16:42:38 -0500

At 03:46 PM 3/7/2004 -0500, Nick Winter wrote:
At 11:20 PM 3/5/2004 -0500, Richard Williams wrote:
Whoops. I think the original solution I posted quickly gets off a day. I think it is fixable, but how about this instead:

. gen finyear = year(datevisit)

. replace finyear = finyear - 1 if month(datevisit) < 4
Just a quick note to note, as it were, that this approach can be done in one line:

. gen finyear = year(datevisit) - (month(datevisit)<4)

(This works because the logical condition will evaluate to 0 or 1, as appropriate.

--Nick Winter
I had tried to do something like that but it wasn't working because I didn't have the parentheses on the final part of the expression right. For those determined to use as few lines as possible, here is yet another spacesaver:

. gen finyear = cond(month(datevisit) < 4, year(datevisit)-1, year(datevisit))

The -cond- option seems like a nice alternative to a generate/replace combo of commands.

My favorite solution so far is the ncyear solution suggested by Nick Cox - not so much because it saves typing but because it saves you a lot of time figuring out what to type. But, I suppose there is something to be said for avoiding the use of addons for relatively simple tasks, e.g. if the code will be run on different machines then portability is easier if you stick to built-in functions.

*
* 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/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index