stata数据清理与高效语法

Stata-字符串变量处理

2020-04-16  本文已影响0人  古城路揸fit人

字符型转数值型 destring命令

//destring命令有几个常用的选项 
//gen和replace指向是否产生新变量和替代旧变量
// ignore忽略这些字符,相当于把这些字符给删除了
//force是直接强行把无法转变的样本替换为缺失值
destring code year date size lev, replace ignore(" -/,%")

字符型转类别型

rencode gov, replace

rencodeall gov1 gov2, replace

字符型分解

split year, parse(-)

字符串函数

strmatch
drop if strmatch(x,"*xx*") //变量x中包含字符字段xx被删掉
//strmatch(s1,s2) 。1 if s1 matches the pattern s2; otherwise, 0
//如果s1匹配了s2的模式就等于1
subintr
replace x = subinstr(x," ","",.)
//subinstr(s1,s2,s3,n).s1, where the first n occurrences in s1 of s2 have been replaced with s3
//前n个s2用s3代替
substr
replace x = substr(x,1,3)
replace x = substr(x,-6,6)
//substr(s,n1,n2)。 Description:  the substring of s, starting at n1, for a length of n2
//从n1位开始截取到n2位
上一篇 下一篇

猜你喜欢

热点阅读