STATA学习笔记: 变量
1.新建变量
gen 变量名=变量值
egen 变量名=变量值
2. 变量重命名
(1)单个变量重命名
rename 旧变量名 新变量名
rename make mk
(2)批量重命名
rename (旧变量1 旧变量2 旧变量3) (新变量1 新变量2 新变量3)
rename (price mpg) (price1 mpg1)
rename rep78 headroom, upper
//upper选项,将rep78和headroom变量名修改为大写
(3)在合并不同数据库中的数据时,批量添加前缀/后缀,以标注数据来源
sysuse auto,clear
renvars price mpg,prefix(d1_)
3. 变量标签
添加变量标签,对变量进一步说明
(1)variable label:为变量添加/修改标签
webuse hbp4,clear
des
#为变量hbp添加标签“high blood pressure”
label variable hbp "high blood pressure"
des
#或者简写为
label var hbp "high blood pressure"
(2)value label:为类别变量的数字添加对应的文字
//分两步走
//第一步:定义标签"yesno"的内容 :数字0对应"no", 数字1对应"yes"
//label define命令
//"label define "+"值标签名称"+"每一个值对应的文字"
label define yesno 0 "no" 1 "yes"
//第二步:将变量"hbp"和"yesno"标签的内容关联起来
//label values命令
//"label values"+"变量名"+"值标签名"
label values hbp yesno
原始数据
建立数字-文字对应之后的数据
4. 列示变量
(1)des
sysuse nlsw88,clear
des
(2)ds
ds
//ds命令只显示所有变量的名称,可用于回归时复制变量名
ds,alpha
//ds命令,添加选项alpha,所有变量按照字母顺序列示
(3)des2
des2
点击value label中 indlal的蓝色链接,显示Indusrty类别变量的数字-文字对应表
查看数据,Indusrty的变量值以文字显示,对应的数字值为单击该文字,在表格第一行以灰色显示的数字
5.查找变量
(1)lookfor:Search for string in variable names and labels
webuse nlswork,clear
ds
lookfor code
//Find all occurrences of code in variable names and labels
lookfor married
//Find all occurrences of code or married in variable names and labels
lookfor code married
(2)lookfor_all:Searches for variables/patterns in Stata data files (.dta)
Description
lookfor_all is an extension of the Stata program lookfor. lookfor_all searches through all Stata data files in a current directory and
subdirectories for the variable(s) with at least one of the specified patterns. Each pattern could be a part/whole variable name and/or a word (part
of the word) in the variable label. lookfor_all outputs the list of data files where the pattern was found and the list of variables within every
data file that match the pattern.
(3)findname
Description
findname lists variable names of the dataset currently in memory in a compact or detailed format, and lets you specify subsets of variables to be
listed, either by name or by properties (e.g., the variables are numeric). In addition, findname leaves behind in r(varlist) the names of variables
selected so that you can use them in a subsequent command.
webuse nlswork,clear
findname
//All variables