数据处理过程中,通常面临数据集格式转换的难题,相比 Stata 的
.dta
数据文件,使用更广泛的是 Excel 数据。那么如何将
.dta
数据转换成以
.xls
或
.xlsx
后缀的 Excel 数据呢?
本文介绍的
export excel
命令便是主要解决此问题。下面将围绕
用法
和
应用案例
两方面展开,主要介绍命令的使用。
若采用窗口操作,相应的流程为:
File → Export → Data to Excel spreadsheet(_.xls; _.xlsx)
。
下面,我们分两个小节,分别介绍
export excel
的基本语法和各个选项。
export excel
的基本语法如下:
export excel [using] filename [if] [in][, export_excel_options]
-
其中,
export excel
为命令主体,不可省略;
-
using
为指定使用的文件,导出整个数据集时可省略;
-
filename
为导出的 Excel 文件名;
-
if
和
in
可限定要导出的数据的范围;
-
export_excel_options
为在基础命令上添加的选项,
详细介绍见 2.2 小节。
若只需导出部分变量名,则可在
excel export
后面添加相应的变量名,
需要注意,此时的
using
不可省略
,对应的语法如下:
export excel [varlist] using filename [if] [in][, export_excel_options]
export excel
包含了丰富的选项,下面从主要选项 (Main Options) 和其他选项 (Advanced Options) 两方面进行介绍,并重点选取数据处理常用的进行详细说明。
选项
|
用途
|
replace
|
覆盖已有文件
|
firstrow(variables or varlabels)
|
设置导出数据第一行为
变量名
还是
变量标签
|
sheet("sheetname")
|
指定 sheetname
|
cell(start)
|
从
start
(upper-left) 开始写入数据
|
sheetmodify
|
不修改导出范围之外的数据,不能和
sheetreplace
replace
选项连用
|
sheetreplace
|
导出之前先将 sheet 数据清除 ,不能和
sheetreplace
replace
选项连用
|
nolabel
|
导出变量值,而不是变量的值标签
|
keepcellfmt
|
保留已有表格的单元格格式
|
值得说明的是:常用的是
sheet("sheetname")
firstrow(variables|varlabels)
replace
选项。
-
sheet()
选项可以指定导出到 Excel 时数据的 sheet 名称,可是实现按需求修改 sheet 名的功能,套用在循环中使用比较方便,可以参见下文 3.1 小节 Task2 中的用法。
-
firstrow(variables|varlabels)
选项可以指定导出到 Excel 时表头为变量名还是变量标签,
当不添加
firstrow
选项时,默认时导出数据,不包含变量名
。
-
replace
选项如同其他命令中的用法,覆盖已有数据,一般必选,否则报错
file already exists
。
但当使用
sheet()
选项时
repalce
可以省略
,表示在一份 Excel 表格中写入多张 sheet **。
下面为大家演示
sheet("sheetname")
和
firstrow(variables|varlabels)
的使用效果,注意仔细观察导出的 Excel 文件
sheet 名
和
表头
的区别。
sysuse auto, clear
* 设定 Excel 文件的子表 (Sheet) 名
export excel using "auto.xls", replace sheet("auto")
//结果:注意观察 Excel 文件的 Sheet 名
* 导出Excel的表头为变量名
preserve
keep make price mpg rep78
export excel using "auto_varname.xls", firstrow(variable) replace
restore
/*结果:auto_varname.xls 的前三行
make price mpg rep78
AMC Concord 4,099 22 3
AMC Pacer 4,749 17 3
*设定导出Excel的表头为变量名
preserve
keep make price mpg rep78
export excel using "auto_varlabel.xls", firstrow(varlabel) replace
restore
/*结果:auto_varlabel.xls 的前三行
Make and Model Price Mileage (mpg) Repair Record 1978
AMC Concord 4,099 22 3
AMC Pacer 4,749 17 3
选项 | 用途 |
---|
datestring(datetime_format) | 将时间格式数据导出为字符型 |
missing(repval) | 将缺失值导出为指定的缺失值标记(repval ),字符型或数值型均可, 如无此选项,默认导出为空格 |
locale(locale) | 当使用扩展的 ASCII 字符集可能需要此选项。默认的环境为 UTF-8 |
按照 rep78
变量(汽车 1978 年维修次数) 将 auto.dta 拆分成 Excel 格式的子集 。
- Task1: 按照
auto_rep78_i_.xls
命名文件,其中 i
表示相应维修次数; - Task2: 生成
auto_rep78_all.xls
数据集,其中一个 Sheet 对应一个子集。
提示:解决这两个问题的关键在于 2.2 部分重点介绍的 sheet("sheetname")
和firstrow(variables|varlabels)
两个选项的使用。
sysuse auto, clear
tabulate rep78 //列表呈现 rep78 的类别和频数分布
levelsof rep78,local(rep)
foreach i in `rep'{
preserve
keep if rep78 == `i'
export excel using "auto_rep78_`i'.xls", firstrow(variable) replace
export excel using "auto_rep78_all.xls", firstrow(variable) sheet(`i')
restore
使用 export excel
导出 nlsw88.dta
数据集的 变量名 和 变量标签 ,存在name
和 varlabel
两列,文件名为 nlsw88_varname_varlab.xls
。
处理思路: 导入 nlsw88.dta 数据 --> 使用 firstrow(variable) 选项获取变量名 (Data1) --> 使用 firstrow(varlabel)
选项获取变量标签(Data2) --> 合并 Data1 和 Data2 --> 由行转置为列 。
sysuse nlsw88, clear
*获取变量名
preserve
export excel using "nlsw88_varname.xls" in 1,firstrow(variable) replace
import excel using "nlsw88_varname.xls", clear
keep in 1
save "nlsw88_varname.dta", replace
restore
*获取变量标签
export excel using "nlsw88_varlab.xls" in 1, firstrow(varlabels) replace
import excel using "nlsw88_varlab.xls", clear
keep in 1
save "nlsw88_varlab.dta", replace
*合并、转置
use "nlsw88_varname.dta", clear
append using "nlsw88_varlab.dta"
sxpose, clear
rename _var1 varname
rename _var2 varlabel
list varname varlab, noobs
export excel using "nlsw88_varname_varlab.xls",firstrow(variable) replace
+-----------------------------------------+
| varname varlabel |
|-----------------------------------------|
| idcode NLS id |
| age age in current year |
| race race |
| married married |
| never_married never married |
|-----------------------------------------|
| grade current grade completed |
| collgrad college graduate |
| south lives in south |
| smsa lives in SMSA |
| c_city lives in central city |
|-----------------------------------------|
| industry industry |
| occupation occupation |
| union union worker |
| wage hourly wage |
| hours usual hours worked |
|-----------------------------------------|
| ttl_exp total work experience |
| tenure job tenure (years) |
+-----------------------------------------+
小彩蛋:describe, replace
命令可以快速实现上述需求,将数据集的 position, name, type, isnumeric, format, vallab, varlab 导出到一个新的数据集。
sysuse nlsw88, clear
describe, replace
describe
list name varlab, noobs
keep name varlab
export excel using "nlsw88_varname_varlab.xls",firstrow(variable) replace
* ##2.2 介绍 firstrow 和 sheet 的使用
sysuse auto, clear
*设定Excel文件的Sheet名
export excel using “auto.xls", replace sheet("auto")
*导出Excel的表头为变量名
preserve
keep make price mpg rep78
export excel using "auto_varname.xls", firstrow(variable) replace
restore
*设定导出Excel的表头为变量名
preserve
keep make price mpg rep78
export excel using "auto_varlabel.xls", firstrow(varlabel) replace
restore
* ## 3.1 案例一
sysuse auto, clear
levelsof rep78,local(rep)
foreach i in `rep'{
preserve
keep if rep78 == `i'
export excel using "auto_rep78_`i'.xls", firstrow(variable) replace
export excel using "auto_rep78.xls", firstrow(variable) sheet(`i')
restore
* ## 3.2 案例二
sysuse nlsw88, clear
*获取变量名
preserve
export excel using "nlsw88_varname.xls" in 1,firstrow(variable) replace
import excel using "nlsw88_varname.xls",clear
keep in 1
save "nlsw88_varname.dta", replace
restore
*获取变量标签
export excel using "nlsw88_varlab.xls" in 1, firstrow(varlabels) replace
import excel using "nlsw88_varlab.xls",clear
keep in 1
save "nlsw88_varlab.dta", replace
*合并、转置
use "nlsw88_varname.dta", clear
append using "nlsw88_varlab.dta"
sxpose, clear
rename _var1 varname
rename _var2 varlabel
list varname varlab, noobs
export excel using "nlsw88_varname_varlab.xls",firstrow(variable) replace
export命令啊Title[D] import excel -- Import and export Excel filesSyntaxLoad an Excel fileimport excel [using] filename [, import_excel_options]Load subset of variables from an Excel fileimport excel ext...
1、保证数据集里的字段和SQL语句里字段全部一致,包括sql语句里必须有系统字段
2、exportExcel()执行的时候,是先去执行SQL语句,再去到数据集里面进行不对,若有不一致的地方,则报列名无效错误
3、如果想导出所有数据,界面若分页,则使用ExportExcel(true,'',false) 没有分页,则使用ExportExcel()
4、界面分页时,若只想...
xls2dta命令:将excel文件转换成dta文件
在stata应用中,往往需要对多个excel中的数据进行处理,这就需将excel格式的数据转化为stata格式的数据,通常的做法是读入(import excel)、保存为dta(save)、合并(append)三步走。今天为大家介绍的xls2dta命令可以一步到位地将一个或多个excel转化为stata格式的dta文件,并可以同时实现横纵向合并...
真无语了,咋那么多客户要导出数据到Excel?还都动不动就好几万条到几十万?导出了都看不看啊,真是倒霉催的。唉,牢骚一顿,进入正题。 业务功能比较简单就是把数据库内的数据,导出到Excel文件,文件里也没有什么修饰,比如颜色,底纹之类的啥都不带,纯数据文件,客户要拿这个文件查看,做些筛选之类的操作。 起初就是用poi,很简单的方式,生成Excel并输出到输出流,直接让用户下载。可惜好
在Stata/SE 16.0中,您可以使用以下命令将dta格式数据存储为Excel:
export excel using filename.xlsx, replace
其中,filename.xlsx是要存储的Excel文件的名称,replace选项指示如果该文件已经存在,则将其替换。
请注意,您需要在Stata/SE 16.0中安装xlxp命令,才能将数据导出为Excel格式。您可以使用以下...
*数据是表格的形式,进常用到Excel
*在程序中经常可以看到有导出Excel文档,Excel导入数据的情况,现在我就说一下我学到的导出Excel
*导出Excel有两种方法,第一种是自己设置表头的,第二种是填充的,现在我说的是第一种方法(是自己设置表头)
第一种方法:
一、在视图上写调用的方法:
1、写方法:exportExcel 是方法(名称),在“导出按钮”调用exportExcel 方法
2、接着是获取参数(获取数据)
3、弹出提示框,进行提示(是否导出所有商品数据)
4、关闭提示框
5、在新标签打
public static void main(String[] args) throws Exception {
String sheetName = "测试Excel格式";
String sheetTitle = "测试Excel格式";
List<String> co...
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.SimpleDateForma...
shellout 命令
Description
shellout opens a document from inside Stata without having to specify the exact file path of the program. It also opens an appl
输出描述性统计
logout,save (miaoshu) excel replace:tabstat X roa size lev RISK DA, s(n mean sd max p50 min) c(s) long f(%9.3g)
输出相关性分析
logout,save (xiangguan) excel replace:pwcorr_a X roa size lev RISK DA...
import com.y9zb.core.SplitSet;
import com.y9zb.core.TimeUtil;
import org.apache.poi.hssf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax