简体   繁体   English

使用日期范围 - 在面板中创建一个标志

[英]Working with date ranges - creating a flag in panel

I have two datasets:我有两个数据集:

  1. Panel - years from 2010 to 2020面板 - 从 2010 年到 2020 年
  2. Ranges for each firm (one or many) when they took a loan - also the range indicates the loan duration每个公司(一个或多个)在获得贷款时的范围 - 该范围还表示贷款期限

First can look like this (each company has all the year-observation records):首先可以是这样的(每个公司都有所有的年度观察记录):

   id  year
   1   2010
   1   ...
   1   2020
   2   2010
   2   ...
   2   2020

Second can look like this (so all variety, a company can have all years with a loan, some gaps in the beginning and in the end):第二个可以是这样的(所以各种各样,一家公司可以有所有年的贷款,在开始和结束时都有一些差距):

   id start end
   1  2010  2011
   1  2011  2014
   1  2017  2018
   1  2012  2020
   2  2014  2018
   3  2011  2012
   3  2015  2018
   3  2018  2020
   4  2011  2012
   4  2015  2018
   4  2010  2018

The idea is to merge the two, so each company gets 0 or 1 for a year, 1 if there was a loan that year (so any of the ranges matching), and 0 if a company didn't have a loan that year.想法是将两者merge ,因此每家公司在一年中得到 0 或 1,如果当年有贷款(因此任何范围匹配)则为 1,如果公司当年没有贷款则为 0。

Example company based on the above would look like this:基于以上内容的示例公司如下所示:

   id  year  flag
   3   2010    0
   3   2011    1
   3   2012    1
   3   2013    0
   3   2014    0
   3   2015    1
   3   2016    1
   3   2017    1
   3   2018    1
   3   2019    1
   3   2020    1

Hope that makes sense.希望这是有道理的。 I tried inrange() but there are too many different scenarios and my code gets messy, thought there is a simple and clean way to do it.我尝试inrange()但有太多不同的场景,我的代码变得混乱,认为有一种简单干净的方法可以做到这一点。

If you work on the second dataset, you can get something fit to merge with your main dataset.如果您处理第二个数据集,则可以获得merge与主数据集合并的内容。

clear 
input id start end
1  2010  2011
1  2011  2014
1  2017  2018
1  2012  2020
2  2014  2018
3  2011  2012
3  2015  2018
3  2018  2020
4  2011  2012
4  2015  2018
end 

gen long ID = _n 
gen toexpand = end - start + 1
expand toexpand 
bysort ID : gen year = start + _n - 1 
drop start end ID toexpand 
duplicates drop id year, force 
sort id year 
list, sepby(id)

    +-----------+
     | id   year |
     |-----------|
  1. |  1   2010 |
  2. |  1   2011 |
  3. |  1   2012 |
  4. |  1   2013 |
  5. |  1   2014 |
  6. |  1   2015 |
  7. |  1   2016 |
  8. |  1   2017 |
  9. |  1   2018 |
 10. |  1   2019 |
 11. |  1   2020 |
     |-----------|
 12. |  2   2014 |
 13. |  2   2015 |
 14. |  2   2016 |
 15. |  2   2017 |
 16. |  2   2018 |
     |-----------|
 17. |  3   2011 |
 18. |  3   2012 |
 19. |  3   2015 |
 20. |  3   2016 |
 21. |  3   2017 |
 22. |  3   2018 |
 23. |  3   2019 |
 24. |  3   2020 |
     |-----------|
 25. |  4   2011 |
 26. |  4   2012 |
 27. |  4   2015 |
 28. |  4   2016 |
 29. |  4   2017 |
 30. |  4   2018 |
     +-----------+

After the merge 1:1 id year it is merge 1:1 id year之后是

gen flag = _merge == 3 
``` 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM