Bookmark and Share

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


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

RE: st: fill or expand data


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: fill or expand data
Date   Tue, 12 Oct 2010 22:10:40 +0100

Eric gave an excellent precise example formulation. This code is in his spirit but omits a loop over observations and any restructuring of the data other than -expand- in place. 

clear
inp id  str14(yearenter   yearexit)    yearcont
1  "10/80"             "2/81"           81
1  "3/81"                "4/81"          81
1 "11/85"             "2/87"          87
end

foreach x of var yeare* { 
	g `x'2 = date(`x', "M19Y") 
	g `x'3 = year(`x'2) 
	format `x'2 %tdMon_CCYY 
}

l 
gen nexpand = yearexit3 - yearenter3 + 1 
expand nexpand
drop nexpand 
bysort id yearenter yearexit : replace yearcont = yearenter3[1] + _n - 1 
l

Nick 
[email protected] 

Eric Booth

Be sure to show us what you've tried.   I don't think -tsfill- can be used to get the target dataset you describe.

Here's one way to get your target dataset:

***********************!
//input your dataset//
clear
inp id  str14(yearenter   yearexit)    yearcont
1  "10/80"             "2/81"           81
1  "3/81"                "4/81"          81
1 "11/85"             "2/87"          87
end

//first, put your dates into a format stata understands//
ds yeare*
foreach x in `r(varlist)' {
g `x'2 = date(`x', "M19Y")
format `x'2 %tdMon_CCYY
}


//create range of years and reshape//
g range = ""
forval n = 1/`=_N' {
	local start = year(yearenter2) in `n'
	local stop = year(yearexit2) in `n'
	di "`start' to `stop'"
	numlist "`start'(1)`stop'"
	replace range = "`r(numlist)'" in `n'
	}
split range, p(" ")
drop range yearenter yearexit
g i = _n
reshape long range, i(i) j(obs)
drop i
drop if mi(range)
l

On Oct 12, 2010, at 12:16 PM, <[email protected]>

> My question is about to display or fill my data set. I have tried the
> command tsfill but I don't know how  continue to arrive at my final
> data set.
> I have  this data:
> id  year enter   year exit    yearcont
> 1  10/80             2/81           81
> 1  3/81                4/81          81
> 1 11/85             2/87          87
> 
> And I want to have this one
> 
> id  year enter   year exit    yearcont
> 1  10/80               3/81           80
> 1   10/80              3/81            81
> 1   3/81               4/81             81
> 1   11/85             2/87             85
> 1    11/85            2/87            86
> 1  11/85             2/87              87


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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