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

Re: st: how to merge to datasets containing dates?


From   Joseph Coveney <[email protected]>
To   Statalist <[email protected]>
Subject   Re: st: how to merge to datasets containing dates?
Date   Mon, 16 Jan 2006 10:00:27 +0900

Andr� Paul wrote:

I have to datasets I would like to merge.

One of them contains a Firm ID and an application date (for a credit)
The other one contains the firm IDs + the starting and finishing dates of
accounts +additional data(let's call them X).

I would like to merge both files by firm ID and dates, but the problem is
that in the second data set I have a period (rather than a precise date) and
I would like the application date to match the correct time period.
An example:

[Redacted]

--------------------------------------------------------------------------------

Take a look at -joinby- to set things up, and then select with
an -inrange()-.  The technique is illustrated with your example below.

Joseph Coveney

clear
set more off
tempfile tmpfil0
input byte ID str11 date byte credit_number
1 "1 nov 2003" 1
1 "1 jan 2004" 2
1 "5 dec 2004" 3
end
save `tmpfil0'
clear
input byte ID str11 starting_date str11 finishing_date str2 X
1 "1 oct 2003" "30 sep 2004" "x1"
1 "1 oct 2004" "30 sep 2005" "x2"
end
joinby ID using `tmpfil0' // <-- Here (ID is optional in this case)
erase `tmpfil0'
foreach var of varlist *date {
   generate int `var'_dt = date(`var', "dmy")
   drop `var'
   rename `var'_dt `var'
   format `var' %dCY-N-D
}
keep if inrange(date, starting_date, finishing_date) // <-- And here
list, noobs abbreviate(20) clean
exit

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index