0

我有以下数据集

A       B      begin_yr       end_yr
asset  brown    2007            2010
asset  blue     2008            2008
basics caramel  2015            2015
cows    dork    2004            2006

我想要A并且B有代表每一年的行。

我每年都扩大:

gen x = end_yr - begin_yr
expand x +1

这给了我以下信息:

A         B      begin_yr       end_yr    x
asset   brown     2007            2010    3
asset   brown     2007            2010    3
asset   brown     2007            2010    3
asset   brown     2007            2010    3
asset   blue      2008            2008    0
basics  caramel   2015            2015    0
cows    dork      2004            2006    2

最终,我想要以下数据集:

A         B      begin_yr       end_yr    x    year
asset   brown     2007            2010    3    2007
asset   brown     2007            2010    3    2008
asset   brown     2007            2010    3    2009
asset   brown     2007            2010    3    2010
asset   blue      2008            2008    0    2008
basics  caramel   2015            2015    0    2015
cows    dork      2004            2006    2    2004
cows    dork      2004            2006    2    2005
cows    dork      2004            2006    2    2006

这是我到目前为止所拥有的:

gen year = begin_yr if begin_yr!=end_yr

如何填充变量的其余部分year

4

2 回答 2

2

以下对我有用:

clear

input strL A strL B begin_yr end_yr
asset  brown 2007 2010
basics caramel 2015 2015
cows dork 2004 2006
end

generate id = _n
expand 2

clonevar year = begin_yr
bysort id: replace year = end_yr[2] if _n == _N

drop if _n == 3

tsset id year
tsfill

foreach var in  A B begin_yr end_yr {
    bysort id: replace `var' = `var'[1]
}

list

     +--------------------------------------------------+
     |      A         B   begin_yr   end_yr   id   year |
     |--------------------------------------------------|
  1. |  asset     brown       2007     2010    1   2007 |
  2. |  asset     brown       2007     2010    1   2008 |
  3. |  asset     brown       2007     2010    1   2009 |
  4. |  asset     brown       2007     2010    1   2010 |
  5. | basics   caramel       2015     2015    2   2015 |
     |--------------------------------------------------|
  6. |   cows      dork       2004     2006    3   2004 |
  7. |   cows      dork       2004     2006    3   2005 |
  8. |   cows      dork       2004     2006    3   2006 |
     +--------------------------------------------------+
于 2018-11-20T19:10:57.120 回答
2

这是@Pearly Spencer 代码的一个转折点:

clear

input strL A  strL B begin_yr  end_yr
asset  brown    2007            2010
basics caramel  2015            2015
cows    dork    2004            2006
end

gen toexpand = end - begin + 1 

expand toexpand 

bysort A : gen year = begin + _n  - 1 

list, sepby(A) 

     +--------------------------------------------------------+
     |      A         B   begin_yr   end_yr   toexpand   year |
     |--------------------------------------------------------|
  1. |  asset     brown       2007     2010          4   2007 |
  2. |  asset     brown       2007     2010          4   2008 |
  3. |  asset     brown       2007     2010          4   2009 |
  4. |  asset     brown       2007     2010          4   2010 |
     |--------------------------------------------------------|
  5. | basics   caramel       2015     2015          1   2015 |
     |--------------------------------------------------------|
  6. |   cows      dork       2004     2006          3   2004 |
  7. |   cows      dork       2004     2006          3   2005 |
  8. |   cows      dork       2004     2006          3   2006 |
     +--------------------------------------------------------+

对此没有任何反对tssettsfill但也不需要。

于 2018-11-20T19:21:16.557 回答