STATA学习笔记: 变量

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

在这里插入图片描述

  • 9
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值