inp str2 product str2 group Jan Feb March April May June July August
A a 1 . 2 3 2 4 . 4
A b 1 . . 4 . . . 5
A b . . . 4 . 5 . 5
B a 1 . 2 3 2 4 . 4
B a 1 . . 4 . . . 5
B b . . . 4 . 5 . 5
C a 1 . 2 3 2 4 . 4
C a 1 . . 4 . . . 5
C c . . . 4 . 5 . 5
end
*****以上只是输入楼主资料
mvencode _all, mv(0)
foreach i of var Jan-August{
bys product group: egen `i'_new=total(`i')
replace `i'= `i'_new
drop `i'_new
}
duplicates drop
mvdecode _all, mv(0)
*****以上是回答楼主的第一个问题
loc s 0
g id=product+group
foreach k of var Jan-August{
loc s=`s'+1
ren `k' month`s'
}
reshape long month, i(id) j(year)
egen nid=group(id)
xtset nid year
tsspell, c(month<.)
egen start=min(year), by(id _spell)
egen end=max(year), by(id _spell)
egen dur=max(_seq),by(id _spell)
drop if month==.
su nid
loc mm=r(max)
forv j=1/`mm'{
duplicates drop _spell if nid==`j',force
}
drop id year nid _seq _spell _end month
*****以上是回答楼主第二个问题
*****最后start与end变数我用数字代表月份,如果楼主不是很满意,请自行修改一下