小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Stata 轉(zhuǎn) Excel —— export excel 命令詳解

 萌糍粑 2019-12-10

作者:李剛 (中南財(cái)經(jīng)政法大學(xué))

連享會(huì)-文本分析與爬蟲專題班,西北工業(yè)大學(xué),2020.3.26-29

1. 應(yīng)用背景

數(shù)據(jù)處理過程中,通常面臨數(shù)據(jù)集格式轉(zhuǎn)換的難題,相比 Stata 的 .dta 數(shù)據(jù)文件,使用更廣泛的是 Excel 數(shù)據(jù)。那么如何將 .dta 數(shù)據(jù)轉(zhuǎn)換成以 .xls.xlsx 后綴的 Excel 數(shù)據(jù)呢?

本文介紹的 export excel 命令便是主要解決此問題。下面將圍繞用法應(yīng)用案例兩方面展開,主要介紹命令的使用。

若采用窗口操作,相應(yīng)的流程為:File → Export → Data to Excel spreadsheet(_.xls; _.xlsx)

2. 用法介紹

下面,我們分兩個(gè)小節(jié),分別介紹 export excel 的基本語法和各個(gè)選項(xiàng)。

2.1 基本語法

export excel 的基本語法如下:

export excel [using] filename [if] [in][, export_excel_options]

  • 其中,export excel 為命令主體,不可省略;
  • using 為指定使用的文件,導(dǎo)出整個(gè)數(shù)據(jù)集時(shí)可省略;
  • filename 為導(dǎo)出的 Excel 文件名;
  • ifin 可限定要導(dǎo)出的數(shù)據(jù)的范圍;
  • export_excel_options 為在基礎(chǔ)命令上添加的選項(xiàng),詳細(xì)介紹見 2.2 小節(jié)。

若只需導(dǎo)出部分變量名,則可在excel export 后面添加相應(yīng)的變量名,需要注意,此時(shí)的 using 不可省略,對應(yīng)的語法如下:

export excel [varlist] using filename [if] [in][, export_excel_options]

2.2 選項(xiàng)介紹

export excel 包含了豐富的選項(xiàng),下面從主要選項(xiàng) (Main Options) 和其他選項(xiàng) (Advanced Options) 兩方面進(jìn)行介紹,并重點(diǎn)選取數(shù)據(jù)處理常用的進(jìn)行詳細(xì)說明。

主要選項(xiàng) (Main Options)

選項(xiàng)用途
replace覆蓋已有文件
firstrow(variables or varlabels)設(shè)置導(dǎo)出數(shù)據(jù)第一行為變量名還是變量標(biāo)簽
sheet('sheetname')指定 sheetname
cell(start)start (upper-left) 開始寫入數(shù)據(jù)
sheetmodify不修改導(dǎo)出范圍之外的數(shù)據(jù),不能和 sheetreplace replace 選項(xiàng)連用
sheetreplace導(dǎo)出之前先將 sheet 數(shù)據(jù)清除 ,不能和 sheetreplace replace 選項(xiàng)連用
nolabel導(dǎo)出變量值,而不是變量的值標(biāo)簽
keepcellfmt保留已有表格的單元格格式

值得說明的是:常用的是 sheet('sheetname') firstrow(variables|varlabels) replace 選項(xiàng)。

  • sheet() 選項(xiàng)可以指定導(dǎo)出到 Excel 時(shí)數(shù)據(jù)的 sheet 名稱,可是實(shí)現(xiàn)按需求修改 sheet 名的功能,套用在循環(huán)中使用比較方便,可以參見下文 3.1 小節(jié) Task2 中的用法。
  • firstrow(variables|varlabels)選項(xiàng)可以指定導(dǎo)出到 Excel 時(shí)表頭為變量名還是變量標(biāo)簽,當(dāng)不添加 firstrow 選項(xiàng)時(shí),默認(rèn)時(shí)導(dǎo)出數(shù)據(jù),不包含變量名。
  • replace 選項(xiàng)如同其他命令中的用法,覆蓋已有數(shù)據(jù),一般必選,否則報(bào)錯(cuò) file already exists 。但當(dāng)使用sheet() 選項(xiàng)時(shí)repalce 可以省略,表示在一份 Excel 表格中寫入多張 sheet **。

下面為大家演示 sheet('sheetname')firstrow(variables|varlabels) 的使用效果,注意仔細(xì)觀察導(dǎo)出的 Excel 文件 sheet 名表頭 的區(qū)別。

sysuse auto, clear

* 設(shè)定 Excel 文件的子表 (Sheet) 名
export excel using 'auto.xls', replace sheet('auto')

//結(jié)果:注意觀察 Excel 文件的 Sheet 名

* 導(dǎo)出Excel的表頭為變量名
preserve
keep make price mpg rep78
export excel using 'auto_varname.xls', firstrow(variable) replace
restore

/*結(jié)果:auto_varname.xls 的前三行
make price mpg rep78
AMC Concord 4,099 22 3
AMC Pacer 4,749 17 3
*/

*設(shè)定導(dǎo)出Excel的表頭為變量名
preserve
keep make price mpg rep78
export excel using 'auto_varlabel.xls', firstrow(varlabel) replace
restore

/*結(jié)果:auto_varlabel.xls 的前三行
Make and Model Price Mileage (mpg) Repair Record 1978
AMC Concord 4,099 22 3
AMC Pacer 4,749 17 3
*/

其他選項(xiàng) (Advanced Options)

選項(xiàng)用途
datestring(datetime_format)將時(shí)間格式數(shù)據(jù)導(dǎo)出為字符型
missing(repval)將缺失值導(dǎo)出為指定的缺失值標(biāo)記(repval),字符型或數(shù)值型均可,
如無此選項(xiàng),默認(rèn)導(dǎo)出為空格
locale(locale)當(dāng)使用擴(kuò)展的 ASCII 字符集可能需要此選項(xiàng)。默認(rèn)的環(huán)境為 UTF-8

3. 應(yīng)用案例

3.1 案例一

按照 rep78 變量(汽車 1978 年維修次數(shù)) 將 auto.dta 拆分成 Excel 格式的子集 。

  • Task1: 按照 auto_rep78_i_.xls 命名文件,其中 i 表示相應(yīng)維修次數(shù);
  • Task2: 生成 auto_rep78_all.xls數(shù)據(jù)集,其中一個(gè) Sheet 對應(yīng)一個(gè)子集。

提示:解決這兩個(gè)問題的關(guān)鍵在于 2.2 部分重點(diǎn)介紹的 sheet('sheetname')firstrow(variables|varlabels) 兩個(gè)選項(xiàng)的使用。

sysuse auto, clear

tabulate rep78 //列表呈現(xiàn) rep78 的類別和頻數(shù)分布

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
}

3.2 案例二

使用 export excel 導(dǎo)出 nlsw88.dta數(shù)據(jù)集的 變量名變量標(biāo)簽 ,存在namevarlabel兩列,文件名為 nlsw88_varname_varlab.xls 。

處理思路: 導(dǎo)入 nlsw88.dta  數(shù)據(jù) --> 使用 firstrow(variable) 選項(xiàng)獲取變量名 (Data1) --> 使用 firstrow(varlabel) 選項(xiàng)獲取變量標(biāo)簽(Data2) --> 合并  Data1  和  Data2  --> 由行轉(zhuǎn)置為列 。

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

*獲取變量標(biāo)簽
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

*合并、轉(zhuǎn)置
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 命令可以快速實(shí)現(xiàn)上述需求,將數(shù)據(jù)集的 position, name, type, isnumeric, format, vallab, varlab 導(dǎo)出到一個(gè)新的數(shù)據(jù)集。

sysuse nlsw88, clear
describe, replace
describe
list name varlab, noobs
keep name varlab
export excel using 'nlsw88_varname_varlab.xls',firstrow(variable) replace

4. 附:文中所有代碼

* ##2.2 介紹 firstrow 和 sheet 的使用
sysuse auto, clear
*設(shè)定Excel文件的Sheet名
export excel using “auto.xls', replace sheet('auto')

*導(dǎo)出Excel的表頭為變量名
preserve
keep make price mpg rep78
export excel using 'auto_varname.xls', firstrow(variable) replace
restore

*設(shè)定導(dǎo)出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

*獲取變量標(biāo)簽
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

*合并、轉(zhuǎn)置
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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多