Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <njcoxstata@gmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Finding the corresponding variable |
Date | Sun, 7 Apr 2013 10:07:28 +0100 |
Should be gen closest = cond(abs(datea - `when') < abs(dateb - `when'), datea, dateb) Nick njcoxstata@gmail.com On 7 April 2013 09:21, Nick Cox <njcoxstata@gmail.com> wrote: > Your problem has several similarities with that posed by David > Ashcraft yesterday. Read the whole thread starting at > > http://www.stata.com/statalist/archive/2013-04/msg00225.html > > The closest (closer) date of two others is > > local when = mdy(1,1,2013) > > gen closest = cond(abs(datea - `when') > abs(dateb - `when'), datea, dateb) > > after which you can -list- it. > > Alternatively, given what you have, > > gen closest = cond(var1 < var2, datea, dateb) > > You need some rule for when two dates are equally close. > > Notes: > > 1. I recommend against meaningless names such as "var1", "var2", > "var3". You put strain on yourself and anyone reading your code to > remember what they are > > 2. -egen, rowmin()- is all right for what you did but using the > -min()- function is simpler with just two arguments. > > Nick > njcoxstata@gmail.com > > On 7 April 2013 05:24, Ching Wong <ching.y.wong@student.adelaide.edu.au> wrote: > >> I have TWO variables of dates, called "datea" and "dateb". I would >> like to calculate the number of days between a particular date (says, >> 1/01/2013) and the closest day either "datea" or "dateb". >> >> Firstly, I generated one variable called "var1" which is the number of >> days between the particular date and "datea" and also generated >> another variable called "var2" which is the number of days between the >> particular date and "dateb". >> >> Then I set up another variable "var3" in order to choose the minimun >> number of days between "var1" and "var2". By using the Command as >> following: >> ---> egen var3 = rowmin (var1 var2). >> >> My question is that after getting "var3", how I can list the >> corresponding "datea" or "dateb"? >> i.e. var3---> var1 or var2----> datea or dateb * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/