Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

st: merge and nearest value

From   Francesco <>
Subject   st: merge and nearest value
Date   Sun, 19 Aug 2012 01:22:15 +0200

Dear Statalist,

I wish again that you could help me with this particular merging problem...

Let say I have a dataset A as:

A            2
A            5
A            20
B            10
B            2

and I have another dataset B as :

TYPE  Special_Date
A              2
A              6
A              20
A              22
B              5
B              6

The question is : I would like to obtain the difference between the
date of each observation in A and the closest special date in B with
the same type. In case of ties I would take the latest date of the

For example I would obtain here

TYPE   DATE   Difference
A            2            0=2-2
A            5            -1=5-6
A            20            0=20-20
B            10           +4=10-6
B            2             -3=2-5

I was thinking of reshaping the dataset B in order to have the special
dates in column for each type, merging then on type with A, creating a
difference variable between the date and each special date, and taking
the minimum...
But this involves creating a lot of variables and maybe there is
something more simple ?

Many thanks for your suggestions,

Best Regards,
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index