如图所示,为类似我数据的内容(怕牵涉隐私问题所以用excel模拟一下)
我想计算某一种行业中,同一投资主体在投资该行业时的前三年,该行业被投资的占比。
比如;如图所示,我想计算当A在2014年投资K(K所在行业为AAA)时,A在2011年2012年2013年这三年中,投资AAA行业的公司数量占这三年投资的所有公司数量的比重。如果选的年份的前三年数据缺失,比如:数据中只记录了A2011年到2018年的投资结果,想计算2012年时前三年的比重,则计算2011年的比重即可,2013年的就计算2011年和2012年的比重。
以下是我现在尝试的代码,得出来的结果是错的,但是实在是不会写下去了QAQ:
(investor:投资主体简称;investtime1:投资年份;industry1:被投资企业所在行业;target:被投公司简称)
*同一年总投资数
sort investor investtime1
by investor investtime1: egen in_count1 = count(investor)
*同一年同一行业投资数
sort investor industry1 investtime1
by investor industry1 investtime1: egen in_count2 = count(investor)
duplicates drop investor investtime1 in_count1 in_count2,force
destring investtime1, replace force
bys investor industry1: gen first_year=investtime1[1]
bys investor industry1: gen last_year=investtime1[_N]
gen start_year = investtime1 -3 if investtime1 -3 >= first_year & investtime1 -1 <= last_year
gen end_year = investtime1 -1 if investtime1 -3 >= first_year & investtime1 -1 <= last_year
order first_year last_year start_year end_year,after(investtime1)
local start_year1 = start_year
local end_year1 = end_year
by investor industry1: egen in_count3 = sum(in_count1) if investtime1>=`start_year1' & investtime1 <=`end_year1'
(第一次发帖不太熟悉规则,如有对大家造成理解困扰或其他问题请指出,非常非常感谢大家!!!)