stata:subinstr(s1,s2,s3,n)用法举例

//清除内存
clear
//设置5个样本
set obs 5
//增加变量v1,都是this is the day
gen v1 = "this is the my is day"
//增加新变量,v1_replace,其值是替换v1中的第一个is为X
gen v1_replace = subinstr(v1,"is","X",1)
// v1                        v1_replace
// this is the my is day    thX is the my is day
// this is the my is day    thX is the my is day
// this is the my is day    thX is the my is day
// this is the my is day    thX is the my is day
// this is the my is day    thX is the my is day

//增加新变量,v2_replace,其值是替换v1中的第一个 第二个is为X
gen v2_replace = subinstr(v1,"is","X",2)
order v2_replace ,after(v1)
// v1                        v2_replace
// this is the my is day    thX X the my is day
// this is the my is day    thX X the my is day
// this is the my is day    thX X the my is day
// this is the my is day    thX X the my is day
// this is the my is day    thX X the my is day
//增加新变量,v3_replace,其值是替换v1中的所有is为X
gen v3_replace = subinstr(v1,"is","X",.)
order v3_replace ,after(v1)
// v1                        v3_replace
// this is the my is day    thX X the my X day
// this is the my is day    thX X the my X day
// this is the my is day    thX X the my X day
// this is the my is day    thX X the my X day
// this is the my is day    thX X the my X day

 

posted @ 2023-03-08 07:56  myrj  阅读(286)  评论(0编辑  收藏  举报