STATA 和 SAS 输入输出示范

STATA例子

关于这些输出的问题,有2篇文章做了最好的总结。(连玉君)
https://zhuanlan.zhihu.com/p/93793601
https://blog.csdn.net/arlionn/article/details/103855343

http://blog.sina.com.cn/s/blog_65b2919d0102w1i8.html

Stata结果输出:logout、esttab、outreg2

一文读懂结果输出命令大全(上)

https://www.lianxh.cn/news/79bdf97db717e.html

描述性统计

* 描述性统计
// ssc install sum2docx
local varlist "vol fdhld ret turnover price state"
sum2docx `varlist' using "sumstat.docx",replace ///
	stats(N mean(%9.2f) sd(%9.3f) min(%9.2f) median(%9.2f) max(%9.2f)) ///
	title(Table: Descriptive statistics)

相关系数矩阵

* 相关系数矩阵
**不用输出p值corr2docx
local varlist "vol fdhld ret turnover price state"
corr2docx `varlist' using "corr.docx", append spearman(ignore) pearson(pw) ///
	fmt(%8.3f) star title(Table: correlation coefficient matrix)
**需要输出p值esttab
local varlist "vol fdhld ret turnover price state"
estpost correlate `varlist', matrix
esttab using "corr.rtf", ///
	unstack not noobs compress nogaps replace star(* 0.1 ** 0.05 *** 0.01) ///
	b(%8.3f) p(%8.3f) title(Table: correlation coefficient matrix)

回归结果

global controls "lagvol lagret lagto lagsiz lagprc state"

reg vol CI1 
est store m1 
reg vol CI1 $controls i.month i.ind
est store m2
reg vol CI2 
est store m3
reg vol CI2 $controls i.month i.ind
est store m4

local s "using result.rtf"
local m "m1 m2 m3 m4"
esttab `m' `s', b(%6.4f) nogap compress star(* 0.1 ** 0.05 *** 0.01) ///
	ar2 scalar(N F) title(panel OLS) ///
	append indicate("Month FE =*.month" "Ind FE =*.ind")

描述统计量

help tabstat //Stata 官方命令
描述统计量组间均值差异检验
help ttest
help ttable2
help estout
相关分析命令
help pwcorr
help pwcorr_a
回归相关分析命令
help esttab
help outreg2
help logout
stata命令汇总
help sum2docx //输出基本统计量输出
help t2docx //输出均值差异检验结果输出
help corr2docx //输出 Pearson & Spearman 相关系数矩阵输出
help reg2docx //输出回归结果输出




use ret_indexret_cvi,clear
merge 1:1 firm Date using ar_vol
tab _merge
keep if _merge==3
drop _merge

preserve
local xlist "  ar  ret2  cvi fear_index"

logout, save(D:\car_2021\new_table\vol\Table12_Desc_STAT) excel  replace:  tabstat  ///
 `xlist'   , stats(n mean sd min p25 p50 p75 max) c(s) f(%6.5f)
restore

gen log_fear_index=log(fear_index)

tabstat ar  ret2  cvi fear_index, stats(n mean sd min max  skewness  kurtosis ) c(s) f(%6.3f)

 
reg ar  log_fear_index  
est store m1

reg ar log_fear_index  ret2   
est store m2

reg ar log_fear_index ret2 cvi 
est store m3

esttab  m1  m2 m3    /// 
using D:\car_2021\new_table\vol\table12.csv,replace compress nogap nonotes    ///
 se(%6.5f)  se  scalars(r2 r2_a N F)  star(* 0.1 ** 0.05 *** 0.01 ) obslast title("vol regression") addnotes("*** 1%  ** 5%  * 10%" "" "")
	


*********************************************
***********  Code for my research ***********
***********       2014/12/11    ***********
*********************************************

clear all 
set more off
set memo 200m
capture log close
***********

local workdir0 "D:\stock"
cd `workdir0'

local datafiles0: dir "`workdir0'" files "*.txt"

foreach datafile0 of local datafiles0 {
       rm `datafile0'
}
******


set logtype text
log using myproject_log.txt,replace


*********************************************
***********  Code for my research ***********
***********       2014/12/11    ***********
*********************************************


cd D:\stock

*** 清空文件夹中的 图形文件,便于反复调试程序 ***


local workdir "D:\stock"
cd `workdir'

local datafiles: dir "`workdir'" files "*.gph"

foreach datafile of local datafiles {
       rm `datafile'
}


*** 读入原始数据集 CSV 格式 ***
insheet using rawdata.csv,comma names
 

*** 计算需要的变量,并制作标签标识
rename f110201b pxl
label variable pxl  "每股现金股利"

rename   f011201a lev
label variable lev  "资产负债率"

rename   f090901b eps
label variable eps  "每股净利润"

gen fltgbl=1-(nshra/ nshrttl)
label variable fltgbl  "非流通股比率"

rename f091001a mas
label variable mas  "每股净资产"

rename f091801b mncf
label variable mncf  "每股现金流量"

gen tzzc=c002007000/ nshrttl
label variable tzzc  "每股投资总支出"

gen size=ln(a001000000)
label variable size  "企业规模"

*** 删除极端值 ***
sum pxl size lev eps fltgbl mas mncf tzzc
drop if lev>100 | tzzc>100

save mydata,replace


use mydata,clear

*** 散点图 ***
foreach variable of varlist size lev eps fltgbl mas mncf tzzc {

scatter pxl `variable' , saving(graph_`variable') 

}

graph combine graph_size.gph graph_lev.gph  graph_eps.gph  graph_fltgbl.gph  ///
graph_mas.gph  graph_mncf.gph  graph_tzzc.gph,saving(scatter_graph) 

graph export scatter_graph.png,replace
 
 
*** 描述性统计 ***
tabstat pxl lev eps fltgbl mas mncf tzzc, stats(n mean sd min max  skewness  kurtosis ) c(s) f(%6.3f)


*** 相关系数表 ***
corr pxl size lev eps fltgbl mas mncf tzzc


*** OLS 估计 四个模型  ****

use mydata,clear

*** 多重共线性检测 ***
reg pxl size lev eps fltgbl mas mncf tzzc
vif


****model 1****
reg pxl size 
est store m1

****model 2****
xi: reg pxl size i.year i.indcd
est store m2

****model 3****
reg pxl size lev eps fltgbl mas mncf tzzc
est store m3

****model 4****
xi:  reg pxl size lev eps fltgbl mas mncf tzzc i.year i.indcd
est store m4

****model 5 异方差稳健的标准误估计 ****
xi:  reg pxl size lev eps fltgbl mas mncf tzzc i.year i.indcd, robust
est store m5

  
logout , save(OLS_table) word replace fix(3): esttab m1 m2 m3 m4 m5, ///
mtitle(m1 m2 m3 m4 m5) b(%6.3f)   star(* 0.1 ** 0.05 *** 0.01 )  ///
scalar(r2 r2_a N F) compress nogap drop(_I* o._I*)

 
*** 斜率系数检验 ***
quietly  xi:  reg pxl size lev eps fltgbl mas mncf tzzc i.year i.indcd


*** 检验 两个斜率系数是否同时为零 ***
test lev eps


*** 检验 两个斜率系数是否相等 ***

test lev = eps



**** A. 面板回归  个体效应 ***

use mydata,clear
quietly tsset stkcd year

*** One-way Fixed Effect Models: Group Effects  ***
** 1.Within Group Effect Model **
xtreg pxl size lev eps fltgbl mas mncf tzzc, fe i(stkcd)
est store  g1
   ** areg to get the same result except for R2 **
areg pxl size lev eps fltgbl mas mncf tzzc, absorb(stkcd) 
   ** 聚类稳健标准误 **
xtreg pxl size lev eps fltgbl mas mncf tzzc, fe vce(cluster stkcd)
est store  g2

** 2. Between Group Effect Model: Group Mean Regression **
xtreg pxl size lev eps fltgbl mas mncf tzzc, be i(stkcd)  
est store  g3



**** B. 面板回归  时间效应 ***

*** One-way Fixed Effect Models: Time Effects ***
** 1.Within Time Effect Model **
iis year 
xtreg pxl size lev eps fltgbl mas mncf tzzc, fe i(year)  
est store  g4

** 2.Within Time Effect Model **
xtreg pxl size lev eps fltgbl mas mncf tzzc, be i(year) 
est store  g5



****   C. 双向效应   ****

*** Two-way Within Effect Model ***
tab year, gen(yr_dum) 
xtreg pxl size lev eps fltgbl mas mncf tzzc yr_dum2-yr_dum14, fe i(stkcd) 
est store  g6
   ** 聚类稳健标准误 **
xtreg pxl size lev eps fltgbl mas mncf tzzc yr_dum2-yr_dum14, fe vce(cluster stkcd)
est store  g7

esttab g1 g2 g3 g4 g5 g6 g7 using table5.csv,replace compress nogap ///
nonotes scalars(r2_a N F)  star(* 0.1 ** 0.05 *** 0.01 ) ///
obslast title("TABLE 5") drop( yr_dum*) addnotes("*** 1%  ** 5%  * 10%" "" "")



*** 固定效应还是随机效应:豪斯曼检验  ***
****model 1****
use mydata,clear
quietly tsset stkcd year

xtreg pxl size   ,fe
est store panel01_fe

xtreg pxl size  ,re
est store panel01_re

***  Hausman 检验***
hausman panel01_fe panel01_re,sigmamore

**** 结论:支持固定效应模型 ****



****model 2****
xtreg pxl size lev eps fltgbl mas mncf tzzc  ,fe
est store panel02_fe

xtreg pxl size lev eps fltgbl mas mncf tzzc  ,re
est store panel02_re

***  Hausman 检验***
hausman panel02_fe panel02_re,sigmamore

**** 结论:支持固定效应模型 ****

esttab panel01_fe panel01_re panel02_fe panel02_re using table6.csv, ///
replace compress nogap nonotes scalars(r2_a N F)  star(* 0.1 ** 0.05 *** 0.01 ) ///
obslast title("TABLE 6")  addnotes("*** 1%  ** 5%  * 10%" "" "")


set more on
clear all

STATA 输入输出

http://blog.sina.com.cn/s/blog_65b2919d0101qepl.html
options pagesize=53 linesize=76 pageno=1 nodate
formchar="|----|+|—"
;
在大数据运行中,我们不想输出log

dm log ‘clear;’ continue;

option nonotes;

proc printto log=“c:\test.txt”;
run;

proc printto log=null;
run;

proc print data=sashelp.class;
run;

%put ‘not thing show’ all;

proc printto log=log;
run;

proc print data=sashelp.class;
run;

%put all;

*** Point the log to its default destination;
proc printto;run;

proc printto print=null log=null;
run;quit;

proc print data=sashelp.class;
run;

proc means data=sashelp.class;
run;

proc printto;run;quit; 恢复log

1.输出LOG文件:

proc printto print=‘D:\shandong\log_print’ log=‘D:\shandong\log_orignal.log’ new;

2.1SAS输出结果怎么转成WORD

options nodate nonumber;

ods listing close;
ods rtf file=‘c:\name.doc’;
proc reg data=a;
model y=x;
run;
ods rtf close;

ods listing;

2.2SAS输出结果怎么转成PDF

options nodate nonumber;

ods listing close;
ods printer pdf file =‘odsprinter.pdf’;

proc reg data=a;
model y = x;
run;

ods printer close;

ods listing;

2.3SAS输出结果怎么转成XLS
options nodate nonumber;

ods listing close;
ods html file =‘D:\A1.xls’;
proc means data=sashelp.class ;
var age weight height;
title ‘格式大全’;
run;
ods html close;

ods listing;

2.4SAS输出结果怎么转成HTML文件

options nodate nonumber;

ods listing close;
ods html file =‘d:new.html’;
proc means data=sasuser.score;
var math;
title ‘数学成绩’;
run;
ods html close;

ods listing;

将SAS分析的结果写入到EXCEL中,这种表格 十分的 美观哦。

ods tagsets.excelxp file=“d:\test.xls” options(sheet_name=“print”) style=analysis;

proc print data=sashelp.class;run;

ods tagsets.excelxp options(sheet_name=“freq”);

proc freq data=sashelp.class;

tables sex;

run;

ods tagsets.excelxp close;

或者:

%macro o2xl(xlname,sdata);
ods tagsets.ExcelXP options(sheet_name="&name.") style=XLStatistical;
proc print data=&data. noobs label;
run;
%mend;

  • style 需要另外设置, label 表示在excel输出sas标签;
    ods results noresults;
    ods listing close;
    ods tagsets.ExcelXP file="…\file_name.xls" options(sheet_name=“xlname0”) style=XLStyle;
    proc print data=sdata0 noobs label;
    run;

*table 1;
%o2xl(xlname1,sdata1);
*table 2;
%o2xl(xlname2,sdata2);
……
ods tagsets.excelxp close;

一个综合的例子:

Logit 回归结果呈示:

options pagesize=53 linesize=76 pageno=1 nodate
formchar="|----|+|—";
ods listing close;

options nodate nonumber;
ods rtf file=‘d:\name.doc’;

title 'LOGISTIC回归模型示范 ';
proc logistic data=Data1 desc;
model car_acq_dummy=region_new region_country acq_industry equity_percentage acq_debt acq_tobin_q acq_book_to_market acq_state acq_cash_to_assets_ratio mean_rival_car region_state debt_state
/stb aggregate scale=none lackfit rsq covb;
output out=mmm p=phat lower=lcl upper=ucl /alpha=0.05;
;
run; quit;
ods rtf close;
ods listing;

set more off

clear

sjlog using eststo1, replace

sysuse auto

regress price weight mpg

eststo

regress price weight mpg foreign

eststo

estout, style(fixed)

sjlog close, replace

clear all

set more off

set memo 400m

version 11

capture log close

cd F:\match

set logtype text

log using mylog.txt, replace

insheet using data01.csv,comma names

clear all

log close

2.STATA回归输出格式总结,非常的全面和使用。

http://pan.baidu.com/s/1eQpJzpC

STATA回归输出格式的设定命令

http://repec.org/bocode/e/estout/

estimates table small large, b(%7.4f) se(%7.4f) stats(N r2_a)

sysuse auto,clear
tabstat price wei len mpg rep78, stats(mean sd min p50 max) c(s) f(%6.2f)

logout, save(mytable) word replace: tabstat price wei len mpg rep78, stats(mean sd min p50 max) c(s) f(%6.2f)

logout,save(mytable) word replace: pwcorr_a price wei len mpg rep78

logout,save(mytable) word replace: pwcorrs price wei len mpg rep78,sp
//pwcorrs 可以输出Spearman 相关系数表。

mat a=I(10)
mat list a
logout , save(mytable) word replace:mat list a,nohalf


sysuse auto,clear
reg price wei
est store m1
reg price wei len
est store m2
reg price wei len mpg foreign
est store m3

esttab m1 m2 m3

esttab m1 m2 m3,ar2 compress nogap star(* 0.1 ** 0.05 *** 0.01)

esttab m1 m2 m3,ar2 compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) brackets p

esttab m1 m2 m3,beta

label var weight “汽车重量”
esttab m1 m2 m3,label

esttab m1 m2 m3,margin //默认省略Constant
esttab m1 m2 m3,margin constant

esttab m1 m2 m3 using myout.html,replace //追加的话,replace换为

append
esttab m1 m2 m3 using myout.csv,replace
esttab m1 m2 m3 using myout.csv,replace compress nogap nonotes scalars(r2_a N F) star(* 0.1 ** 0.05 *** 0.01 ) obslast title(“输入标题”) addnotes("*** 1% ** 5% * 10%" “” “”)

增加小数位显示,P值的显示。

est tab, p(.10g)
——————————————
sysuse auto, clear
reg price rep head trunk
est store m1

esttab m1 using myout.csv,replace compress p(%6.4g) nogap nonotes scalars(r2_a N F) star(* 0.1 ** 0.05 *** 0.01 ) obslast title(“输入标题”) addnotes("*** 1% ** 5% * 10%" “” “”)

-----------------------例子-----------------------------
sysuse auto,clear
reg price wei
est store m1
reg price wei len
est store m2
reg price wei len mpg foreign
est store m3

logout , save(mylogout) word replace fix(3): esttab m1 m2 m3,mtitle(模型1 模型2 模型3) b(%6.3f) se(%6.2f) se star(* 0.1 ** 0.05 *** 0.01 ) scalar(r2 r2_a N F) compress nogap

以上比较牛逼!


sysuse auto,clear
reg price wei
est store m1
reg price wei len
est store m2
reg price wei len mpg foreign
est store m3

xml_tab m1 m2 m3 , replace
//默认显示标签,保存名称为stata_out.xml

xml_tab m1 m2 m3 , save(result) sheet(OLS) replace ///
tstat blew stats(r2 r2_a N)
xml_tab m1 m2 m3 , save(result) sheet(OLS) replace ///
tstat blew stats(r2 r2_a N) ///
drop(_Ioccup) font(“Times new Roman” 10) ///
title(table 1 表名) ///
tblank(1) format(NCCR3) ///
note(“注释”)
//中文标签要先修改或 nolabel
//save() 可以有具体路径
//可以有多个sheet

--------------------outreg2--------------------------
sysuse nlsw88,clear
tab race,gen(d_race)
drop d_race1
tab occu,gen(d_occu)
drop d_occu1
reg wage hours ttl_exp married
est store m1
reg wage hours ttl_exp married d_race*
est store m2
reg wage hours ttl_exp married d_race* d_occu*
est store m3
outreg2 [m1 m2 m3] using tab01,seeout replace

outreg2 [m1 m2 m3] using tab01,word replace
outreg2 [m1 m2 m3] using tab01,excel replace

outreg2 [m1 m2 m3] using tab01,word replace label

outreg2 [m1 m2 m3] using tab01,word replace tstat
//t统计量
outreg2 [m1 m2 m3] using tab01,word replace pvalue
//p值
outreg2 [m1 m2 m3] using tab01,word replace tstat tdec(2)///
rdec(3)
//t统计量小数点后两位 R2小数点后三位

因为对sas inputinfile 读取cards数据和外部文件,如txt, dat数据诸多的疑惑,以及对infile选项 truncover, missover, flowover,pad, lrecl等选项的一知半解。前些日子特地在论坛上提了两个问题:

input读入原始数据,datalines和txt中的区别?

http://bbs.pinggu.org/forum.php?mod=viewthread&tid=1571120&from^^uid=3269938

关于input输入原始数据

http://bbs.pinggu.org/forum.php?mod=viewthread&tid=2145400&from^^uid=3269938

请教了论坛的各位大牛,有davil2000,webgu, ziyenano, pobel等等,他们都给出了很好的解释和回答,但总体还是没有一个系统的解答,因此我在总结了各位大牛的回答的基础上,查阅了相关文献,有了一个初步的理解,写出来与大家分享,也希望各位大牛能补充说明。

首先说明,sas的input和infile语句无比强大,可以通过各种输入方式和各种选项实现任何有规律无规律数据的导入,光input, infile的功能我觉得都可以写成一本书了,因此本文不致力于详细讲解input和infile,事实上很多选项我也不懂,但是可以查阅一下sas help在自己需要的时候使用。本文主要对infile中容易误解的几个选项missover, truncover, pad以及input 列表输入和列输入的不同进行说明。

infile选项汇总.jpg

关于input的columninput (列输入)和listinput (列表输入).

简单来说:列输入就是严格按照变量指定的长度进行数据读取,忽略分隔符。如input name 1−10; inputhome40.; 都属于列输入。列表输入就是根据规定的分隔符(默认是空格)按照变量的顺序读取数据,多个空格按照一个处理,遇到空格即停止。如input name home:40. ;

假设有如下原始数据:

原始数据.jpg

需要注意的是,在windows和unix中的文本处理器如写字板创建上面的数据,数据每行属于可变长度,每行的长度就是灰色区域的长度,而在sas的datalines中,上述的数据是固定长度的,长度为80 bytes,后面没数据的地方以空格填充,在大型主机的某些数据文件中,数据行也是固定长度的,无数据的地方以空格填充。

首先分别以input的列输入和列表输入导入上述数据,假设数据位于d盘infile文件夹中,名字为emplist。

DATA test1;

INFILE "d:\infile\emplist.dat";

INPUT lastn $1-21 Firstn $ 22-31

   Empid $32-36 Jobcode $37-45;

RUN;

DATA test2;

INFILE “d:\infile\emplist.dat”;

INPUT lastn Firstn

    Empid Jobcode ;

RUN;

上述程序提交后,结果如何,在此之前,先讲一下flowover选项。Infile默认是flowover选项。

在列输入时:如果input指针到了一行的最后一个字符时,有些变量还是没有得到值或者完整的值,指针就会跳到下一行的第一列开始读取数据,来填充没有值的变量。当下次input语句执行的时候,新的一行数据就会进入inputbuffer。

因此列输入数据如下:

列输入.jpg

说明,对于第二行数据,pilot没有达到jobcode所需要的长度,因此指针跳到下一行读取smith填补上来。完成之后进入数据步的下一个循环,执行下一次input,因此指针再次跳行,将leistner读入。而读取第五行tomas数据时,harald长度不够firstn用,所以指针跳到下一行将wade读入作为firstn,然后e1026作为empid,pilot仍然不够用,所以跳到下一行读取waugh作为jobcode;

在列表输入时,以空格为分隔符,数据可以正确地读入,但默认的flowover选项仍然会导致将下一行数据读入填充缺失值。结果如下:

列表输入.jpg

使用missover选项:

当在infile语句中使用missover选项时,它会抑制掉flowover,使得在读取短的数据行时,input指针不会跳到下一行来读取数据,相反,它使得没有数据的变量变缺失。

在列输入中加上missover选项时,结果如下:

列输入missover.jpg

我们发现的是,再也没有从下一行读取数据,可是jobcode的pilot和第6行的后面仍然没有正确读入。这是因为在列输入的时候,如果指针到了一行的结尾,仍然没能读入满足变量长度的数值,就会将该变量变缺失。解决方法:可以再加入pad选项,pad选项数据后面填上空格,使得每行的长度相同,默认lrecl=256。

如果列表输入infile加上missover选项,结果如下,不会像列输入那样将最后的变量变缺失。

列表输入missover.jpg

Truncover: 其实truncover的功能类似于missover 和pad合用的效果,它是针对列输入时,如果一行最后读入的数值仍然不能满足变量需要的长度,不会将变量缺失,而是将读入的值赋给该变量。列输入加truncover的结果同上。

当然,对于列表输入,truncover的结果也同上,是正确的结果。

关于PAD,pad固名思义,意思是将数据行的后面部分填上空格,这样就使得数据行变长,不致于让input指针在读取短数据行时到达数据行结尾从而跳行。Pad不会抑制flowover的作用,它会将数据后面的空白位置填上空格,空格的长度由lrecl=n决定,默认n为256,当然可以在infile选项中修改n的大小。这就是为什么在列输入中用missover和pad可以正确读取数据了,因为数据行的长度不再是数据实际的长度(可变长度),而是一个固定长度256,后面都是空格。

如果列表输入只加pad,不加missover,结果如下

列表输入pad.jpg

Pad不会抑制flowover,并且将多个空格作为一个处理,所以出现了上述结果。

关于读取外部文件和datalines中的区别,很多朋友都发现sas读取外部文件和datalines数据,数据结构完全一样,同样的程序,结果不同。

原因是:外部文件数据,数据行是可变长度,如上例。除非加上pad,以空格填充。

而在cards中的数据,看起来也是可变长度,但其实是固定长度,为80。结尾没有数据的地方都以空格填充。即默认有pad 。引用davil2000大神的回答:

Cardimagesystem option specifies that SAS source and data lines be processed as if thesewere punched card images—all exactly 80 bytes long and padded with blanks.

所以如下两个程序:

data test3;

input lastn $1-21Firstn $ 22-31

   Empid $32-36 Jobcode $37-45;

cards;

LANGKAMM SARAH E0045 Mechanic

TORRES JAN E0029 Pilot

SMITH MICHAEL E0065

LEISTNER COLIN E0116 Mechanic

TOMAS HARALD

WADE KIRSTEN E0126 Pilot

WAUGH TIM E0204 Pilot

;

run;

data test4;

input lastn Firstn

 Empid Jobcode ;

cards;

LANGKAMM SARAH E0045 Mechanic

TORRES JAN E0029 Pilot

SMITH MICHAEL E0065

LEISTNER COLIN E0116 Mechanic

TOMAS HARALD

WADE KIRSTEN E0126 Pilot

WAUGH TIM E0204 Pilot

;

run;

列输入可以正确读入,列表输入不行,因为没有missover选项,类同于读取外部文件时只加pad选项。

SAS STATA输入输出

http://blog.sina.com.cn/s/blog_65b2919d0101aom9.html
混合数据模型:常数和变量都放在common里面;
变截距模型:变量输入common里面;
变系数模型:c和变量都要放在cross—section specific里面。

http://www.ejxjy.com/ http://www.ejxjy.com/ 师大培训中心 63340811邮箱
https://www.enetedu.com/ https://www.enetedu.com/ jung@163邮箱

SAS Library http://pan.baidu.com/s/1kT64zqB
Reading Data into SAS
This page was adapted from a page created by Oliver Schabenberger. We thank Professor Schabenberger for permission to adapt and distribute this page via our web site.

  1. Reading data inline
  2. Reading data with column pointers
  3. Reading data from ASCII files
  4. Reading dBase files
  5. Creating a permanent data set
  6. Reading Excel files into SAS

6.1. Using the Import facility
6.2. Using PROC ACCESS
6.3. Using Dynamic Data Exchange (DDE)

SAS数据的输入&输出

  1.  SAS 数据的输入&输出(.TXT .DAT .XLS .MDB .SAV格式)
    

一、导入导出文本文件(txt格式)、纯数据文件(dat格式);其实都是导入导出DLM文件(.),需要指定分隔符号。

1.TAB分割,第一行为变量名(以txt为例,dat同样)

PROC IMPORT OUT=A

       DATAFILE= "D:\cha\1.txt"

       DBMS=TAB REPLACE;

GETNAMES=YES;

DATAROW=2;

RUN;

PROC EXPORT DATA=A

       OUTFILE= "D:\filelist.txt"

       DBMS=TAB REPLACE;

RUN;

DATA toads;

INFILE ’c:\MyRawData\ToadJump.dat’;

INPUT ToadName $ Weight Jump1 Jump2 Jump3;

RUN;

http://blog.sina.com.cn/s/blog_65abf1890100qthq.html

The following is a typical INFILE statement:

INFILE “C:\sasfiles\testfile.csv” DLM='09’x DSD LRECL=1024 TRUNCOVER FIRSTOBS=2;

The INPUT statement is used to list the variables you want to read from the file, for example:

DATA A; INFILE “C:\ sasfiles\testfile.csv”; INPUT VAR1 VAR2 VAR3; RUN;
DELIMITER= (DLM=)

This option enables you to tell SAS what single character is used as a delimiter in the file you are reading. Some common delimiters are comma (,), vertical pipe (|), semi-colon (?, and tab. At this time the TAB needs to be by its hexadecimal value. For the ASCII systems (UNIX, Windows, and Linux), the value is 09. For EBCDIC systems (z/OS and MVS), the value is 05. The syntax looks like this for the pipe: DLM=’|’. It looks like this for a TAB on ASCII systems: DLM='09’x.

data a;

infile “d:\aaa.txt” delimiter='09’x ;

input AcquirorPERMN DateAnnounced yymmdd10.;

format DateAnnounced yymmdd10.;

run;

DATA toads0;

INFILE “D:\Prof_zhang\t1999-2004.csv” delimiter = ‘,’ MISSOVER DSD lrecl=32767 firstobs=2;

INPUT t1 t2 t3 $ t4 $ t5 $ t6-t80;

RUN;

2.SPACE分割,第一行为变量名

PROC IMPORT OUT=A

       DATAFILE= "D:\a\word.txt"

       DBMS=DLM REPLACE;

DELIMITER='20'x;

GETNAMES=YES;

DATAROW=2;

RUN;

PROC EXPORT DATA=A

       OUTFILE= "C:\b.txt"

       DBMS=DLM REPLACE;

DELIMITER='20'x;

RUN;

二、导入导出EXCEL文件(xls格式)程序如下:

PROC IMPORT OUT=A

       DATAFILE= "F:\cc.xls"

       DBMS=EXCEL REPLACE;

SHEET="Sheet1$";

GETNAMES=YES;

RUN;

PROC EXPORT DATA=WORK.A

       OUTFILE= "D:\export1.xls"

       DBMS=EXCEL REPLACE;

SHEET="nameofsheet";

RUN;

http://blog.sina.com.cn/s/blog_743c902a0100z6e2.html

%macro o2xl(xlname,sdata);
ods tagsets.ExcelXP file=“d:\name.xls” options(sheet_name="&name.") style=XLStatistical;

proc print data=&data. noobs label;
run;

%mend;

  • style 需要另外设置, label 表示在excel输出sas标签;
    ods results noresults;
    ods listing close;
    ods tagsets.ExcelXP file="…\file_name.xls" options(sheet_name=“xlname0”) style=XLStyle;
    proc print data=sdata0 noobs label;
    run;

*table 1;
%o2xl(xlname1,sdata1);
*table 2;
%o2xl(xlname2,sdata2);
……
ods tagsets.excelxp close;

单个数据表输出:

ods results noresults;
ods listing close;
ods tagsets.ExcelXP file=“d:\file_name.xls” options(sheet_name=“sheet1”) style=XLStyle;

proc print data=sdata0 noobs label;
run;

ods tagsets.excelxp close;

三、导入导出EXCEL文件(CSV格式)程序如下:

PROC IMPORT OUT=A

       DATAFILE= "D:\export1.csv"

       DBMS=CSV REPLACE;

GETNAMES=YES;

RUN;

DATA music;

INFILE ’c:\MyRawData\Bands.csv’ DLM = ’,’ DSD MISSOVER;

INPUT BandName :$30. GigDate :MMDDYY10. EightPM NinePM TenPM ElevenPM;

RUN;

PROC EXPORT DATA=A

       OUTFILE= "D:\export1.csv"

       DBMS=CSV REPLACE;

RUN;

四、导入导出ACCESS文件(mdb格式)程序如下:

PROC IMPORT OUT=A

       DATATABLE= "username"

       DBMS=ACCESS REPLACE;

DATABASE="D:\all\userinfo.mdb";

RUN;

PROC EXPORT DATA=A

       OUTTABLE= "export1"

       DBMS=ACCESS REPLACE;

DATABASE="D:\example.mdb"; *must be an exsited database;

RUN;

五、导入SPSS文件(SAV格式)程序如下:

PROC IMPORT OUT=A

       DATAFILE= "d:\aa.sav"

       DBMS=SAV REPLACE;

RUN;

proc export data=A

       outfile= "d:\l2.sav" 

      DBMS=SAV REPLACE;

run;

六、导入文件(STATA格式)程序如下:

proc import out=a

   datafile = "d:\aaa.dta"

   REPLACE;
   run;

proc contents data=a;
run;

proc export data=dd.return outfile=‘D:\pro_car\return.dta’;

run;

SAS导出到STATA格式:

%include “C:\Program Files\SAS\savastata.sas”;

data result;
set a;
run;

%savastata(D:, -replace);

Reading PC SAS Data Files & Formats into SPSS for Windows
Under SAS, create a library defined as the output file for an export job, e.g.:
LIBNAME SPSS XPORT ‘C:\SPSS\SASXPRT1.DAT’;
which creates a SAS export file called SASXPRT1.DAT in a directory called C:\SPSS, referred to under SAS as libname SPSS. Any of these names can be changed, except for the engine type XPORT. Note that the library is associated with a DOS file, rather than a DOS directory.
If you need user-defined formats to be copied with the data, associate a different libname with the XPORT engine, and copy your formats catalog to a scratch dataset (called, say, FORMTS) in the WORK directory, e.g.,
LIBNAME FORMT XPORT ‘C:\SPSS\SASFORMT.DAT’; PROC FORMATS LIBRARY=LIBRARY CNTLOUT=FORMTS;
Use PROC COPY to put the relevant data into the empty file, e.g.,
PROC COPY IN=DATA OUT=SPSS; SELECT MYDSET;
which puts dataset DATA.MYDSET into the export file. If you made a format file in step 2, then also execute the command:
PROC COPY IN=WORK OUT=FORMT; SELECT FORMTS;
Under SPSS, execute the command:
GET SAS DATA=‘C:\SPSS\SASXPRT1.DAT’ DSET(MYDSET) /FORMATS=‘C:\SPSS\SASFORMT.DAT’ FSET(FORMTS).
assuming the file name and location to be as specified in step 1; omit the /FORMATS switch if you didn’t do the stuff in step 2.

现将要保存的数据集放在一个文件夹中,然后读入到临时文件,最后输出到一个EXCEL表格。sheet1/2/3…
%macro test(lib);
options noxwait;
x “del d:\test.xls”;
libname dd ‘d:\jjj’;
data test1;
set dd.airline;
run;
data test2;
set dd.alcohol;
run;
data test3;
set dd.andy;
run;

proc sql noprint;
select count(memname) into:_nobs from sashelp.vtable where libname="%upcase(&lib)";
select memname into:_memname separated by " " from sashelp.vtable where libname="%upcase(&lib)";
quit;

libname test excel “d:\test.xls”;

%do i=1 %to &_nobs.;
%let tablename=%scan(&_memname.,&i.);
data test.&tablename.;
set &tablename.;
run;
%end;
libname test clear;
%mend;
%test(work);

可以使用系统内置的tagsets.excelxp来分worksheet输出
ods tagsets.excelxp file=“d:\test.xls” options(sheet_name=“print”) style=analysis;
proc print data=sashelp.class;run;

ods tagsets.excelxp options(sheet_name=“freq”);
proc freq data=sashelp.class;
tables sex;
run;

ods tagsets.excelxp close;

SAS Learning Module

Inputting data into SAS

This module will show how to input raw data into SAS, showing how to read instream data and external raw data files using some common raw data formats. Section 3 shows how to read external raw data files on a PC, UNIX/AIX, and Macintosh, while sections 4-6 give examples showing how to read the external raw data files on a PC, however these examples are easily converted to work on UNIX/AIX or a Macintosh based on the examples shown in section 3.

  1. Reading free formatted data instream

One of the most common ways to read data into SAS is by reading the data instream in a data step - that is, by typing the data directly into the syntax of your SAS program. This approach is good for relatively small datasets. Spaces are usually used to “delimit” (or separate) free formatted data. For example:

DATA cars1;

INPUT make $ model $ mpg weight price;

CARDS;

AMC Concord 22 2930 4099

AMC Pacer 17 3350 4749

AMC Spirit 22 2640 3799

Buick Century 20 3250 4816

Buick Electra 15 4080 7827

;

RUN;

After reading in the data with a data step, it is usually a good idea to print the first few cases of your dataset to check that things were read correctly.

title “cars1 data”;

PROC PRINT DATA=cars1(obs=5);

RUN;

Here is the output produced by the proc print statement above.

cars1 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

  1. Reading fixed formatted data instream

Fixed formatted data can also be read instream. Usually, because there are no delimiters (such as spaces, commas, or tabs) to separate fixed formatted data, column definitions are required for every variable in the dataset. That is, you need to provide the beginning and ending column numbers for each variable. This also requires the data to be in the same columns for each case. For example, if we rearrange the cars data from above, we can read it as fixed formatted data:

DATA cars2;

INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;

CARDS;

AMC Concord2229304099

AMC Pacer 1733504749

AMC Spirit 2226403799

BuickCentury2032504816

BuickElectra1540807827

;

RUN;

TITLE “cars2 data”;

PROC PRINT DATA=cars2(obs=5);

RUN;

The benefit of fixed formatted data is that you can fit more information on a line when you do not use delimiters such as spaces or commas.

Here is the output produced by the proc print statement above.

cars2 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

  1. Reading fixed formatted data from an external file

Suppose you are using a PC and you have a file named cars3.dat, that is stored in the c:\carsdata directory of your computer. Here’s what the data in the file cars3.dat look like:

AMC Concord2229304099

AMC Pacer 1733504749

AMC Spirit 2226403799

BuickCentury2032504816

BuickElectra1540807827

To read the file cars3.dat, use the following syntax.

DATA cars3;

INFILE “c:\carsdata\cars3.dat”;

INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;

RUN;

TITLE “cars3 data”;

PROC PRINT DATA=cars3(obs=5);

RUN;

Here is the output produced by the proc print statement above.

cars3 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

Suppose you were working on UNIX. The UNIX version of this program, assuming the file cars3.dat is located in the directory ~/carsdata, would use the syntax shown below. (Note that the “~” in the UNIX pathname above refers to the user’s HOME directory. Hence, the directory called carsdata that is located in the users HOME directory.)

DATA cars3;

INFILE “~/carsdata/cars3.dat”;

INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;

RUN;

TITLE “cars3 data”;

PROC PRINT DATA=cars3(obs=5);

RUN;

Likewise, suppose you were working on a Macintosh. The Macintosh version of this program, assuming cars3.dat is located on your hard drive (called Hard Drive) in a folder called carsdata would look like this.

DATA cars3;

INFILE ‘Hard Drive:carsdata:cars3.dat’;

INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;

RUN;

TITLE “cars3 data”;

PROC PRINT DATA=cars3(OBS=5);

RUN;

In examples 4, 5 and 6 below, you can change the infile statement as these examples have shown to make the programs appropriate for UNIX or for the Macintosh.

  1. Reading free formatted (space delimited) data from an external file

Free formatted data that is space delimited can also be read from an external file. For example, suppose you have a space delimited file named cars4.dat, that is stored in the c:\carsdata directory of your computer.

Here’s what the data in the file cars4.dat look like:

AMC Concord 22 2930 4099

AMC Pacer 17 3350 4749

AMC Spirit 22 2640 3799

Buick Century 20 3250 4816

Buick Electra 15 4080 7827

To read the data from cars4.dat into SAS, use the following syntax:

DATA cars4;

INFILE “c:\carsdata\cars4.dat”;

INPUT make $ model $ mpg weight price;

RUN;

TITLE “cars4 data”;

PROC PRINT DATA=cars4(OBS=5);

RUN;

Here is the output produced by the proc print statement above.

cars4 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

  1. Reading free formatted (comma delimited) data from an external file

Free formatted data that is comma delimited can also be read from an external file. For example, suppose you have a comma delimited file named cars5.dat, that is stored in the c:\carsdata directory of your computer.

Here’s what the data in the file cars5.dat look like:

AMC,Concord,22,2930,4099

AMC,Pacer,17,3350,4749

AMC,Spirit,22,2640,3799

Buick,Century,20,3250,4816

Buick,Electra,15,4080,7827

To read the data from cars5.dat into SAS, use the following syntax:

DATA cars5;

INFILE “c:\carsdata\cars5.dat” delimiter=’,’;

INPUT make $ model $ mpg weight price;

RUN;

TITLE “cars5 data”;

PROC PRINT DATA=cars5(OBS=5);

RUN;

Here is the output produced by the proc print statement above.

cars5 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

  1. Reading free formatted (tab delimited) data from an external file

Free formatted data that is TAB delimited can also be read from an external file. For example, suppose you have a tab delimited file named cars6.dat, that is stored in the c:\carsdata directory of your computer.

Here’s what the data in the file cars6.dat look like:

AMC Concord 22 2930 4099

AMC Pacer 17 3350 4749

AMC Spirit 22 2640 3799

Buick Century 20 3250 4816

Buick Electra 15 4080 7827

To read the data from cars6.dat into SAS, use the following syntax:

DATA cars6;

INFILE “c:\carsdata\cars6.dat” DELIMITER='09’x;

INPUT make $ model $ mpg weight price;

RUN;

TITLE “cars6 data”;

PROC PRINT DATA=cars6(OBS=5);

RUN;

Here is the output produced by the proc print statement above.

cars6 data

OBS MAKE MODEL MPG WEIGHT PRICE

1 AMC Concord 22 2930 4099

2 AMC Pacer 17 3350 4749

3 AMC Spirit 22 2640 3799

4 Buick Century 20 3250 4816

5 Buick Electra 15 4080 7827

  1. Problems to look out for

If you read a file that is wider than 80 columns, you may need to use the lrecl= parameter on the infile statement.

  1. For more information

For more detailed information on reading raw data into SAS, see Reading data into SAS in the SAS Library.

To learn how to create permanent SAS system files, see the SAS Learning Module on Reading and writing SAS system files.

For information on creating and recoding variables once you have entered your data, see the SAS Learning Module on Creating and recoding variables.

Section 2.4

First program

  • Read internal data into SAS data set uspresidents;

DATA uspresidents;

INPUT President $ Party $ Number;

DATALINES;

Adams F 2

Lincoln R 16

Grant R 18

Kennedy D 35

;

RUN;

Second program

  • Read data from external file into SAS data set;

DATA uspresidents;

INFILE ‘c:\MyRawData\President.dat’;

INPUT President $ Party $ Number;

RUN;

Section 2.5

  • Create a SAS data set named toads;

  • Read the data file ToadJump.dat using list input;

DATA toads;

INFILE ‘c:\MyRawData\ToadJump.dat’;

INPUT ToadName $ Weight Jump1 Jump2 Jump3;

RUN;

  • Print the data to make sure the file was read correctly;

PROC PRINT DATA = toads;

TITLE ‘SAS Data Set Toads’;

RUN;

Section 2.6

  • Create a SAS data set named sales;

  • Read the data file OnionRing.dat using column input;

DATA sales;

INFILE ‘c:\MyRawData\OnionRing.dat’;

INPUT VisitingTeam $ 1-20 ConcessionSales 21-24 BleacherSales 25-28

    OurHits 29-31 TheirHits 32-34 OurRuns 35-37 TheirRuns 38-40;

RUN;

  • Print the data to make sure the file was read correctly;

PROC PRINT DATA = sales;

TITLE ‘SAS Data Set Sales’;

RUN;

Section 2.7

  • Create a SAS data set named contest;

  • Read the file Pumpkin.dat using formatted input;

DATA contest;

INFILE ‘c:\MyRawData\Pumpkin.dat’;

INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.

    (Score1 Score2 Score3 Score4 Score5) (4.1);

RUN;

  • Print the data set to make sure the file was read correctly;

PROC PRINT DATA = contest;

TITLE ‘Pumpkin Carving Contest’;

RUN;

Section 2.9

First Program

  • Create a SAS data set named nationalparks;

  • Read a data file NatPark.dat mixing input styles;

DATA nationalparks;

INFILE ‘c:\MyRawData\NatPark.dat’;

INPUT ParkName $ 1-22 State $ Year @40 Acreage COMMA9.;

RUN;

PROC PRINT DATA = nationalparks;

TITLE ‘Selected National Parks’;

RUN;

Second Program

  • Create a SAS data set named nationalparks;

  • Read a data file NatPark.dat mixing input styles;

DATA nationalparks;

INFILE ‘c:\MyRawData\NatPark.dat’;

INPUT ParkName $ 1-22 State $ Year Acreage COMMA9.;

RUN;

PROC PRINT DATA = nationalparks;

TITLE ‘Selected National Parks’;

RUN;

Section 2.10

DATA weblogs;

INFILE ‘c:\MyWebLogs\dogweblogs.txt’;

INPUT @’[’ AccessDate DATE11. @‘GET’ File :$20.;

RUN;

PROC PRINT DATA = weblogs;

TITLE ‘Dog Care Web Logs’;

RUN;

Section 2.11

  • Create a SAS data set named highlow;

  • Read the data file using line pointers;

DATA highlow;

INFILE ‘c:\MyRawData\Temperature.dat’;

INPUT City $ State $

    / NormalHigh NormalLow

    #3 RecordHigh RecordLow;

RUN;

PROC PRINT DATA = highlow;

TITLE ‘High and Low Temperatures for July’;

RUN;

Section 2.12

  • Input more than one observation from each record;

DATA rainfall;

INFILE ‘c:\MyRawData\Precipitation.dat’;

INPUT City $ State $ NormalRain MeanDaysRain @@;

RUN;

PROC PRINT DATA = rainfall;

TITLE ‘Normal Total Precipitation and’;

TITLE2 ‘Mean Days with Precipitation for July’;

RUN;

Section 2.13

  • Use a trailing @, then delete surface streets;

DATA freeways;

INFILE ‘c:\MyRawData\Traffic.dat’;

INPUT Type $ @;

IF Type = ‘surface’ THEN DELETE;

INPUT Name $ 9-38 AMTraffic PMTraffic;

RUN;

PROC PRINT DATA = freeways;

TITLE ‘Traffic for Freeways’;

RUN;

Section 2.14

First Program

DATA icecream;

INFILE ‘c:\MyRawData\IceCreamSales.dat’ FIRSTOBS = 3;

INPUT Flavor $ 1-9 Location BoxesSold;

RUN;

Second Program

DATA icecream;

INFILE ‘c:\MyRawData\IceCreamSales2.dat’ FIRSTOBS = 3 OBS=5;

INPUT Flavor $ 1-9 Location BoxesSold;

RUN;

Third Program

DATA class102;

INFILE ‘c:\MyRawData\AllScores.dat’ MISSOVER;

INPUT Name $ Test1 Test2 Test3 Test4 Test5;

RUN;

Fourth Program

DATA homeaddress;

INFILE ‘c:\MyRawData\Address.dat’ TRUNCOVER;

INPUT Name $ 1-15 Number 16-19 Street $ 22-37;

RUN;

Section 2.15

First Program

DATA reading;

INFILE ‘c:\MyRawData\Books.dat’ DLM = ‘,’;

INPUT Name $ Week1 Week2 Week3 Week4 Week5;

RUN;

Third Program (second program intentionally omitted)

DATA music;

INFILE ‘c:\MyRawData\Bands.csv’ DLM = ‘,’ DSD MISSOVER;

INPUT BandName :$30. GigDate :MMDDYY10. EightPM NinePM TenPM ElevenPM;

RUN;

PROC PRINT DATA = music;

TITLE ‘Customers at Each Gig’;

RUN;

Section 2.16

PROC IMPORT DATAFILE =‘c:\MyRawData\Bands2.csv’ OUT = music REPLACE;

RUN;

PROC PRINT DATA = music;

TITLE ‘Customers at Each Gig’; RUN;

Section 2.17

(data must be read from a spreadsheet)

PROC IMPORT DATAFILE = ‘c:\MyExcelFiles\OnionRing.xls’ DBMS=XLS OUT = sales;

RUN;

PROC PRINT DATA = sales;

TITLE ‘SAS Data Set Read From Excel File’; RUN;

Section 2.18

First Program (data must be read from a spreadsheet)

  • Read an Excel spreadsheet using DDE;

FILENAME baseball DDE ‘CLIPBOARD’;

DATA sales;

INFILE baseball NOTAB DLM='09’x DSD MISSOVER;

LENGTH VisitingTeam $ 20;

INPUT VisitingTeam CSales BSales OurHits TheirHits OurRuns TheirRuns;

RUN;

Second Program (data must be read from a spreadsheet, must replace DDE triplet with DDE triplet for your own files

  • Read an Excel spreadsheet using DDE;

OPTIONS NOXSYNC NOXWAIT;

X ‘“C:\MyFiles\BaseBall.xls”’;

FILENAME baseball DDE ‘Excel|C:\MyFiles[BaseBall.xls]sheet1!R2C1:R5C7’;

DATA sales;

INFILE baseball NOTAB DLM='09’x DSD MISSOVER;

LENGTH VisitingTeam $ 20;

INPUT VisitingTeam CSales BSales OurHits TheirHits OurRuns TheirRuns;

RUN;

Section 2.19

First Program

DATA distance;

Miles = 26.22;

Kilometers = 1.61 * Miles;

RUN;

PROC PRINT DATA = distance;

RUN;

Second Program

DATA Bikes.distance;

Miles = 26.22;

Kilometers = 1.61 * Miles;

RUN;

PROC PRINT DATA = Bikes.distance;

RUN;

Section 2.20

First Program

LIBNAME plants ‘c:\MySASLib’;

DATA plants.magnolia;

INFILE ‘c:\MyRawData\Mag.dat’;

INPUT ScientificName $ 1-14 CommonName $ 16-32 MaximumHeight

 AgeBloom Type $ Color $;

RUN;

Second Program

LIBNAME example ‘c:\MySASLib’;

PROC PRINT DATA = example.magnolia;

TITLE ‘Magnolias’;

RUN;

Section 2.21

First Program

DATA ‘c:\MySASLib\magnolia’;

INFILE ‘c:\MyRawData\Mag.dat’;

INPUT ScientificName $ 1-14 CommonName $ 16-32 MaximumHeight

 AgeBloom Type $ Color $;

RUN;

Second Program

PROC PRINT DATA = ‘c:\MySASLib\magnolia’;

TITLE ‘Magnolias’;

RUN;

Section 2.22

DATA funnies (LABEL = ‘Comics Character Data’);

INPUT Id Name $ Height Weight DoB MMDDYY8. @@;

LABEL Id = ‘Identification no.’

 Height = 'Height in inches'

 Weight = 'Weight in pounds'

 DoB    = 'Date of birth';

INFORMAT DoB MMDDYY8.;

FORMAT DoB WORDDATE18.;

DATALINES;

53 Susie 42 41 07-11-81

54 Charlie 46 55 10-26-54

55 Calvin 40 35 01-10-81

56 Lucy 46 52 01-13-55

;

  • Use PROC CONTENTS to describe data set funnies;

PROC CONTENTS DATA = funnies;

RUN;

STATA数据标准化

标准化 归一化 等等说法。标准化,使数据整齐划一,美观,但是 回归中,系数变化,标准误 显著性 不变。
https://blog.csdn.net/myy3075/article/details/87924718

egen stdx= std(price) , mean(0) std(1) 单个变量实现办法之一

* by Rujiang
* 19Dec2019
capture program drop stand
program stand
    version 14.1
    syntax varlist [if] [in] [, prefix(name) methods(name)]
    if "`methods'" == "" {
    
        if "`prefix'" == "" {
            local prefix "norm_"
        }
        
        foreach var in `varlist' {
            qui: summarize `var' `if' `in'
            qui: generate `prefix'`var' = (`var'- r(mean)) / r(sd) `if' `in'
            qui: label variable `prefix'`var' "normalized `var'"
            di "====> `prefix'`var' generated"
        }
            
        di "-----------xxxxxx-----------"
        di "all the variables you specified are **normalized**"
    }
    
    
    if "`methods'" == "minmax" {
    
            if "`prefix'" == "" {
                local prefix "minmax_"
            }
        
        foreach var in `varlist' {
            qui: summarize `var' `if' `in'
            qui: generate `prefix'`var' = (`var'- r(min)) / (r(max) - r(min)) `if' `in'
            qui: label variable `prefix'`var' "minmax standardized `var'"
            di "====> `prefix'`var' generated"
        }
            
        di "-----------xxxxxx-----------"
        di "all the variables you specified are **minmax standardized**"
    }

    
    if "`methods'" != "" {
        if "`methods'" != "minmax" {
            di as err `"nothing happened, only methods("minmax") allowed"'
        }
    }
    
end


使用这个命令,可以一个个变量变换,也可以批量变换,很不错。

. stand price
====> norm_price generated
-----------xxxxxx-----------
all the variables you specified are **normalized**

. stand price, methods("minmax")
====> minmax_price generated
-----------xxxxxx-----------
all the variables you specified are **minmax standardized**


. stand price-head, methods("minmax") prefix("stand_")
====> stand_price generated
====> stand_mpg generated
====> stand_rep78 generated
====> stand_headroom generated
-----------xxxxxx-----------
all the variables you specified are **minmax standardized**

. su stand*

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
 stand_price |         74    .2278444    .2338086          0          1
   stand_mpg |         74    .3205965    .1995001          0          1
 stand_rep78 |         69    .6014493    .2474831          0          1
stand_head~m |         74    .4266409    .2417128          0          1


欢迎使用Markdown编辑器

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

新的改变

我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:

  1. 全新的界面设计 ,将会带来全新的写作体验;
  2. 在创作中心设置你喜爱的代码高亮样式,Markdown 将代码片显示选择的高亮样式 进行展示;
  3. 增加了 图片拖拽 功能,你可以将本地的图片直接拖拽到编辑区域直接展示;
  4. 全新的 KaTeX数学公式 语法;
  5. 增加了支持甘特图的mermaid语法1 功能;
  6. 增加了 多屏幕编辑 Markdown文章功能;
  7. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间;
  8. 增加了 检查列表 功能。

功能快捷键

撤销:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜体:Ctrl/Command + I
标题:Ctrl/Command + Shift + H
无序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
检查列表:Ctrl/Command + Shift + C
插入代码:Ctrl/Command + Shift + K
插入链接:Ctrl/Command + Shift + L
插入图片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替换:Ctrl/Command + G

合理的创建标题,有助于目录的生成

直接输入1次#,并按下space后,将生成1级标题。
输入2次#,并按下space后,将生成2级标题。
以此类推,我们支持6级标题。有助于使用TOC语法后生成一个完美的目录。

如何改变文本的样式

强调文本 强调文本

加粗文本 加粗文本

标记文本

删除文本

引用文本

H2O is是液体。

210 运算结果是 1024.

插入链接与图片

链接: link.

图片:Alt

带尺寸的图片:Alt

居中的图片:Alt

居中并且带尺寸的图片:Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1

设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列第二列第三列
第一列文本居中第二列文本居右第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPEASCIIHTML
Single backticks'Isn't this fun?'‘Isn’t this fun?’
Quotes"Isn't this fun?"“Isn’t this fun?”
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。2

注释也是必不可少的

Markdown将文本转换为 HTML

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n − 1 ) ! ∀ n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb NΓ(n)=(n1)!nN 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z − 1 e − t d t . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.Γ(z)=0tz1etdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06Mon 13Mon 20已完成进行中计划一计划二现有任务Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

张三李四王五你好!李四, 最近怎么样?你最近怎么样,王五?我很好,谢谢!我很好,谢谢!李四想了很长时间, 文字太长了不适合放在一行.打量着王五...很好... 王五, 你怎么样?张三李四王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Raphaël 2.3.0开始我的操作确认?结束yesno
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. mermaid语法说明 ↩︎

  2. 注脚的解释 ↩︎


版权声明:本文为weixin_40496687原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。