Data Import/Export

use , sysuse , webuse , save

*****Syntax
* use filename [, clear nolabel]
* use [varlist] [if] [in] using filename [, clear nolabel]

*****Examples
use "\path\to\auto" , clear
* 或者cd到数据所在目录
cd "\path\to"
use auto , clear
use make-foreign using auto , clear
use if foreign == 0 using auto , clear
use using auto if foreign==1 , clear
use "https://www.stata-press.com/data/r16/auto" , clear
use "http://yuanrui.pro/slides/Statistics%20with%20STATA/document/data/auto" , clear
local web "http://yuanrui.pro/slides/Statistics%20with%20STATA/document/data"
use "`web'/cgss2005raw",clear
*****Syntax
* sysuse dir [, all]               /* 可以使用的系统数据集 */
* sysuse ["]filename["] [, clear]

*****Examples
sysuse auto,clear
*****Syntax
* webuse set [https://]url[/]    /* 指定默认读取的网络路径 */

*****Examples
webuse lifeexp                   /* 默认为https://www.stata-press.com/data/r16/ */
use https://www.stata-press.com/data/r16/lifeexp  /* 等效webuse lifeexp */
*****Syntax
* save [filename] [, save_options]
* saveold filename [, saveold_options]

*****Example
saveold "C:\Users\Administrator\Desktop\auto.dta", replace version(12)

import / export excel

*****Syntax
* import excel [using] filename [, import_excel_options]
***Load subset of variables from an Excel file
* import excel extvarlist using filename [, import_excel_options]
* import excel [using] filename, describe

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

*****Examples
webuse auto
export excel auto, firstrow(variables)
import excel auto.xls, firstrow clear
import excel auto.xls, sheet("Sheet1") cellrange(:D70) firstrow clear

import / export delimited

insheet 在 stata13 之后被 import delimited 取代,但仍可使用。

*****Syntax
* import delimited [using] filename [, import_delimited_options]
***Rename specified variables from a delimited text file
* import delimited extvarlist using filename [, import_delimited_options]
***选项delimiter("char"|tab)用以指定分隔符  

* export delimited [using] filename [if] [in] [, export_delimited_options]
* export delimited [varlist] using filename [if] [in] [, export_delimited_options]

*****Examples
copy "https://www.stata.com/examples/auto.csv" auto.csv
import delimited auto, delimiter(",")    
import delimited auto, rowrange(3:6) clear
import delimited auto, colrange(:3) rowrange(8) clear

webuse auto,clear
export delimited make mpg rep78 foreign in 1/10 using myauto, replace

infile / outfile (free format)

*****Syntax
* infile varlist [_skip[(#)] [varlist [_skip[(#)] ...]]] using filename [if] [in] [, options]

*****Examples
sysuse auto, clear
sort price
keep make price mpg rep78 foreign
keep in 1/10
outfile using myout, replace
type myout.raw

infile str18 make price mpg rep78 str8 foreign using myout,clear /*指定变量类型*/
/*Read data in myout.raw into memory, automatically creating a value label for foreign and storing it as a byte rather than a str8*/
infile str18 make price mpg rep78 byte foreign:origin using myout, automatic clear 
describe foreign
list
label list

clear
label define myorigin 0 "Domestic" 1 "Foreign"
infile str18 make price mpg rep78 byte foreign:myorigin using myout
list
label list

infile (fixed format)

infile using mydict
infile using mydict, using(mydata)
infile using mydict if b==1
infile using mydict if runiform()<=.1

infix (fixed format)

infix rate 1-4 speed 6-7 acc 9-11 using highway.raw
infix using highway.dct
infix 2 lines 1: id 1-6 str name 7-36 2: age 1-2 sex 4 using emp.raw
infix using emp.dct

import sas

* import sas [using] filename [, options]

import spss

*****Syntax
* import spss [namelist] [if] [in] using filename [, options]
*****Examples
copy "https://www.stata.com/sampledata/auto.sav" auto.sav
import spss auto
import spss make weight using auto, clear

input

input str20 name age str6 sex
	"A. Doyle" 22  male
	"Mary Hope" 37 "female"
	"Guy Fawkes" 48 male
end

行列对调的数据

copy "http://yuanrui.pro/slides/Statistics%20with%20STATA/document/data/chp1/d5.txt" d5.txt
copy "http://yuanrui.pro/slides/Statistics%20with%20STATA/document/data/chp1/d51.txt" d51.txt
type d5.txt        /*常规数据*/
type d51.txt       /*对调数据*/
insheet using d51.txt, clear 
xpose, clear       /*对调*/
rename v1 year
rename v2 invest
rename v3 income
rename v4 consume

STATA官方提供的资料

help dta_contents
help dta_examples
help dta_manuals

本节命令:
use , sysuse , webuse
import excel , import delimited , infile , infix , input
copy , type , rename , xpose , cd , local