STATA 学习笔记:导入数据

STATA 学习笔记:导入数据

1. excel 格式的数据导入

(1)打开excel

cd "该excel所在的文件夹路径"
//shell out 工作簿名称.格式
shell out auto.xls

shellout 命令
Description

shellout opens a document from inside Stata without having to specify the exact file path of the program. It also opens an application with or
without specifying a file document to be opened.

(2)导入excel

//import excel 工作簿名称.格式, firstrow sheet(Sheet1) clear
//sheet(Sheet1) 指定选取该工作表中的工作表1
import excel auto.xls, firstrow sheet(Sheet1) clear
//注意:clear前面没有逗号

命令encode

Description

encode creates a new variable named newvar based on the string variable varname, creating, adding to, or just using (as necessary) the value label
newvar or, if specified, name.  Do not use encode if varname contains numbers that merely happen to be stored as strings; instead, use generate
newvar = real(varname) or destring; see real() or [D] destring.
webuse hbp2,clear
des sex

在这里插入图片描述

encode sex, generate(gender)
//基于字符串变量sex生成新的数值变量gender,此外还添加了值标签,即文字-数字对应
//gender变量的值female对应数字1,male对应数字2
des gender

在这里插入图片描述
在这里插入图片描述

(3)存储

save myauto.dta,replace
//将转化成stata专用格式的数据存储下来
//数据存储地址即为STATA界面下方显示的当前工作目录

在这里插入图片描述

2. STATA数据导出为excel

sysuse auto,clear
export excel auto,firstrow(variables) replace
//export excel 工作簿名称
//firstrow(variables):将变量名称作为excel第一行
shellout auto.xls

在这里插入图片描述

sysuse auto,clear
export excel auto1.xlsx,firstrow(varlabels) replace
//将dta格式数据转换成名称为auto1,格式为xlsx的工作簿
//firstrow(varlabels)将变量标签作为工作簿第一行
shellout auto1.xlsx

在这里插入图片描述

2. 导入.txt格式数据

import delimited命令

clear
copy http://www.stata.com/examples/auto.csv auto.csv
import delimited auto, colrange(:3) rowrange(8)
//Read the first three columns and last four rows of auto.csv into Stata and list the data
list

在这里插入图片描述

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值