Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Finding the corresponding variable

From   Nick Cox <>
To   "" <>
Subject   Re: st: Finding the corresponding variable
Date   Sun, 7 Apr 2013 09:21:18 +0100

Your problem has several similarities with that posed by David
Ashcraft yesterday. Read the whole thread starting at

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.


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.


On 7 April 2013 05:24, Ching Wong <> 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:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index