Dummy
Q1. 生成dummy但是条件太多(string太长)
【 】合成控制法时候expression too long错误解决问题 - Stata专版 - 经管之家(原人大经济论坛) (pinggu.org)
local code1 ""C25","C26","C27","C28""
local code2 ""C29","C31","C32","C34""
local code3 ""C35","C36","C37","C38""
local code4 ""C39","C40","C41","I63""
local code5 ""I64","I65","M73""
gen hightech = inlist(industry,`code1') | inlist(industry,`code2')| inlist(industry,`code3')| inlist(industry,`code4')| inlist(industry,`code5')
Q2. 生成含有某个字段的dummy
【 】【求助】如何用stata查询不包含某一字符串的数据 - Stata专版 - 经管之家(原人大经济论坛) (pinggu.org)
gen PC=0
replace PC=1 if regexm( ActualControllerNatureID ,"1100")==1
流水型数据
Q3. 数据中出现重复项目
例如某公司(sktcd)的一项专利(Patentname)在多个年份公告了多次。
ssc install gtools, replace //gduplicaties是gtools中的命令
gduplicates tag stkcd year PatentName, g(tag)
bys stkcd year PatentName tag : gen count=_n
keep if count==1
drop count tag
gduplicates report stkcd year PatentName
Q4. 计算某条数据重复次数
例如已有数据是专利公告情况,需要知道某公司当年有多少项专利。另外,这之后出现的patentegrant的重复可由【Q3】解决。
duplicates tag stkcd year, generate(dup) //注意:tag不是变量名
gen patentgrant = dup+1
drop dup
Q5. 计算有几个是不同的(非重复值统计)
例如有一堆外商投资数据,想要知道某企业某年受到来自几个国家的投资(某国某年可能对该企业会有多次投资)
gduplicates tag stkcd year country, g(tag)
bys stkcd year country tag : gen count=_n
bys stkcd year: gen count2=count if count==1
bys stkcd year: egen countrynum=count( count2 )