1. 前言
在目前工作中,用stata清洗及分析數(shù)據(jù)脚翘,感覺很順滑。無奈不少同學(xué)因?yàn)閔elp文件里的英文望而卻步绍哎。
帶著學(xué)習(xí)和分享的目的来农,根據(jù)工作經(jīng)驗(yàn),給大家整理一些常用以及不太常用但很有用的命令崇堰,并對該命令的help文件進(jìn)行有側(cè)重的詳解沃于。
2. 描述
subinst(s1,s2,s3,n)
:字符串函數(shù)。將字符串s1中的字符串s2出現(xiàn)的前n個(gè)海诲,替換成字符串s3繁莹。
subinword(s1,s2,s3,n)
:字符串函數(shù)。將字符串s1中的單詞s2出現(xiàn)的前n個(gè)特幔,替換成字符串s3咨演。
3. 參數(shù)
s1
:要替換的字符串所有內(nèi)容。
s2
:要進(jìn)行替換的字符串蚯斯。
s3
:要替換成的字符串薄风。
n
:整數(shù)(≥0),或者缺失(.)拍嵌;缺失表示替換所有s2
4. 舉例
*將this is the day中的第一個(gè)is替換成X
subinstr("this is the day","is","X",1) = "thX is the day"
*將this is the hour中的前2個(gè)is替換成X
subinstr("this is the hour","is","X",2) = "thX X the hour"
*將this is this中的所有is替換成X
subinstr("this is this","is","X",.) = "thX X thX"
*將this is the day中的第一個(gè)is單詞替換成X
subinword("this is the day","is","X",1) = "this X the day"
*將this is the hour中的所有is單詞替換成X
subinword("this is the hour","is","X",.) = "this X the hour"
*將this is this中的所有th單詞替換成X遭赂,沒有th單詞不進(jìn)行替換
subinword("this is this","th","X",.) = "this is this"
5.補(bǔ)充
* 替換某個(gè)字符串變量var中的所有空格,并生成新的變量newvar
gen newvar=subinstr(var," ","",.)