; I% X. X$ r3 P5 u M 读入杂乱数据 . O( Z+ @, z$ i( L+ x3 A2 D在不确定从哪一列开始读入数据,但知道读入的数据均位于某一特定字符或字符串之后时,可采用@’character’列指针。# R3 ~$ _- W" C4 S
如:有字符串如下,需读入Breed:后面的字符串 & a+ m% Q# t4 rMy dog Sam Breed: Rottweiler Vet Bills: $478 1 w9 Z2 N: N% H6 X# r B Y + j: b- R/ c( z5 L(1)SAS 语句:Input @’Breed: ’ DogBreed $; 读入内容: Rottweil X7 M1 l4 c6 f# T6 z
读入Breed:后面的字符串,赋给DogBreed,读入时到空格时,自动结束。; |" U" y, p- g6 @
(2)SAS 语句:Input @’Breed:’ DogBreed $20.; 读入内容:Rottweiler Vet Bill 3 y. Z( c2 a& x4 J$ h6 g读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20。 2 f: y% u* i9 W3 Y& c(3)SAS语句:Input @’Breed:’ DogBreed 20.; 读入内容:Rottweiler - ~( D/ a2 w+ w% B, F+ K 读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20,但遇到空格时不再继续读数据。$ @ }4 i' E5 V" }" A
从原始数据中读入多行数据作为SAS的一条观测 8 }2 V3 H0 m8 L% [+ a使用行指针: 2 U( r6 Z, E+ c/ s1 l# z: Y; V4 ?' u6 T‘ / ’—— 到下一行读数据 ) U1 A( f" }& T, \$ l H‘#n ’——到第n 行读数据 , y! {6 @) u3 [: g h& p+ sINPUT City $ State $ / NormalHigh NormalLow #3 RecordHigh RecordLow;- e. H: Q- M2 m$ A
从一行原始数据中读入多个观测 3 A4 }, \5 J" G0 D8 r在Input语句末尾使用@@标示,告诉SAS继续读入本行后面的数据。, g- o; K4 b N4 J4 s# K
INPUT City $ State $ NormalRain MeanDaysRain @@; : A0 F f/ [# `$ ?+ H# t% p1 b有选择的读入原始数据 n, X) m- O0 M, S% f5 @SAS让用户无需读入所有的原始数据,然后再判断是否是用户所需要的数据。用户仅需先读入足够的变量,以判断该条观测是否为自己所需,然后在INPUT语句后以@结尾,以使SAS读数据的指针停在此处,保留此行数据。然后用户使用IF语句判断读入的观测是否为所需数据,若是,则使用第二个INPUT语句继续读入其余数据。9 T8 M( Q4 [8 R D2 N
INPUT Type $ @; 1 J9 K1 H% Z# X/ u0 ^# Q+ p IF Type = ’surface’ THEN DELETE;+ r. b$ M/ c7 l+ _
INPUT Name $ 9-38 AMTraffic PMTraffic; 1 U. ~& Z( f1 U, K5 t@ & @@" A5 D4 B7 a3 V+ q& s% Q
(1) 均为锁定数据行的标示;; h& M! C( d2 Y# @! o
(2) @标示在SAS进入下个循环之前就释放锁定的数据行,而@@标示在继续锁定数据行 ) s- d! x c; |( K( x; Y- r# w在INFILE语句中控制输入的选项 ( h$ F) q: O/ i( E(1)FIRSTOBS=n : 从n条观测开始读入数据 : z& S. H6 i: P; G) ]/ ]; a(2)OBS=n 读入n条观测 . S8 r) ~2 n' v7 F1 r, F5 H+ f(3)当读进内存的观测长度小于INPUT语句设定的长度时 3 \9 p+ [- T9 R6 C/ T) ]当SAS指针到达一条记录的末尾,而在INPUT语句中尚有未读入的变量时,SAS默认继续读入下一行数据。+ `8 Z7 J8 S0 j1 Y) ~9 z/ _( J6 n
MISSOVER:不读入下一行数据,而将未赋值的变量以缺失值填充。* i% \ y8 T% J! v
TRUNCOVER:当使用column或格式化读入方式时,某些数据行长度小于其他数据行长度时,使用TRUNCOVER选项,可防止SAS读入下一行数据。7 h+ k/ m# f4 ~+ N, k) w9 u4 N g
使用DATA步读入分隔符文件 : a9 g8 c$ k& Z2 B( \9 ]2 C" M在INFILE语句中使用DLM= 选项或者DSD选项可以读入以特定符号作为分隔符的原始文件。 ( D# Z) k3 m' U1 l- k6 x; M) k0 |(1)The DLM= option (i.e. DLM=’&’) 0 u3 ]- w$ h" m* b! d如果是以Tab作为分隔符,则使用DLM=’09’X命令8 z, N i6 h- @2 ]! Q( `
(2)The DSD option:主要有三个功能 6 `4 X5 S) c& @! ^6 o忽略单引号内的分隔符; s$ r: }: t# Y9 G7 ?& D" L
不将引号作为数据读入SAS; ! T3 X7 L2 o+ a将一行内连续两个单引号作为一个缺失值处理。/ |9 Q7 R( H) ?" U
使用IMPORT程序步读入分隔符文件7 l% l: j& m/ w, O/ R1 J# [
IMPORT 程序的功能 5 g4 U6 }" J. Z! {# y* O) {; `(1) 自动扫描数据文件,并确定变量的类型(数值型或字符型); 3 S Y" U4 v$ h(2) 为字符型变量,自动设定变量的长度;0 S. W7 o7 \: N
(3) 识别一些日期型数据; & f, A. b! }) D(4) 将两个连续的分隔符作为一个缺失值读入SAS2 I9 b) j$ m$ J) i2 W- A5 W" T
(5) 读入引号内数据 ! C( V* y; ~6 B(6) 自动将原始数据中不存在的变量赋缺失值;* ]" D; ]5 J8 u' {) @. h9 J) s
PROC IMPORT DATAFILE=’filename’ OUT=data-set; " E$ p U) h. n8 `( J% HSAS根据读入文件的扩展名确定文件的类型。若读入文件没有正确的扩展名,或者是DLM文件,用户必须在IMPORT程序步中使用DBMS=option 选项。当读入数据集的名称已经存在于SAS库中,可用REPLACE选项将原数据覆盖。6 u8 p" P+ j$ M
PROC IMPORT DATAFILE=’filename’ OUT=data-set DBMS=identifier REPLACE;5 F) y% f, t1 Q$ U
在默认情况下,IMPORT程序步将第一行数据作为变量的名称。若第一行数据并非变量名,可在IMPORT语句后使用GETNAMES=NO语句。 + H/ P) S% J1 E# Z若IMPORT程序读入的是分隔符文件,默认分隔符为空格。若不是,则需使用DILIMITER=statement语句指定分隔符。7 d5 r: ~. F& m8 u- V
PROC IMPORT DATAFILE=’filename’ OUT=data-set ) ?7 ^* u* N* F' Z( ^ DBMS=DLM REPLACE;2 m$ S8 x4 H, L, [9 R8 l- B' P1 n
GETNAMES=NO; 0 U o: F7 L" n5 F+ f DELIMITER=’delimiter-character’; & P# r+ m F& O7 ?& h" u! i/ `- w8 xRUN;# `/ P% R' [3 M! K1 n4 L: \# T' \; q
使用IMPORT程序步读入PC文件: K, l( H+ |$ t* c/ W. Y
PROC IMPORT DATAFILE=’filename’ OUT=data-set% J8 ~/ J$ m& R' t5 \. ?
DBMS=identifier REPLACE;. v* D) E, m0 n: W3 b
列示SAS数据集的内容 ; x1 ^; t/ Q' P, ~" b! H+ e& C$ KPROC CONTENTS DATA=data-set; 0 H+ E; f7 t2 h" O, nCONTENTS程序步的功能是显示SAS对数据集的具体描述,主要内容有: % S0 m: q% i& C( S! d(1) 数据集描述; X/ C2 `1 H* [/ U
数据集的名称; $ U9 X7 T; Y4 c+ n, j! L9 ^2 Z5 _- ` 观测的数量;9 D2 S. q; B) w3 J; }4 X
变量的数量;* @3 s: J9 e/ T: [ c) U. x
创建日期 7 I8 m$ h) ~" L; M A. ^(2) 变量描述( T5 y7 B. b- u# ?
变量类型; , z Y0 B( J7 N( p5 C 变量长度; 4 |4 g* G V3 q- s! G; e) R; B* [ 变量的输出格式; - C, K2 F; t$ Y0 V 变更的输入格式;) ^- K3 O4 o9 F4 J' s
变量标识。7 b/ s* W( v1 a. }
3 ]. k1 @. V( k+ l. a" m3 }
实例:4 ~0 r. h; Y$ `" {/ @! v
1.读入逗号分隔数据:cars_novname.csv# D& ^' \5 I" [2 Z" w2 _! D
1 v1 q+ r& \5 a9 O% z* W ? G
Acura,MDX,SUV,Asia,All,"$36,945 ","$33,337 ",3.5,6,265,17,23,4451,106,189 $ F8 P9 e5 N1 C1 g vAcura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820 ","$21,761 ",2,4,200,24,31,2778,101,172 % G* Y' J& l" H* W6 m* |1 t2 a; }7 NAcura,TSX 4dr,Sedan,Asia,Front,"$26,990 ","$24,647 ",2.4,4,200,22,29,3230,105,183; {8 g2 j8 W- @" |! n
Acura,TL 4dr,Sedan,Asia,Front,"$33,195 ","$30,299 ",3.2,6,270,20,28,3575,108,186 ( M2 f3 S7 B. h' j. |2 XAcura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755 ","$39,014 ",3.5,6,225,18,24,3880,115,1970 G' e9 ]0 a& p% t) R0 Y. {: a$ y/ n3 K
9 C: _; {9 L& I, B1 U U% Y
proc import datafile="cars_novname.csv" out=mydata dbms=csv replace; " v1 ^. y8 B0 u H+ w1 g: f getnames=no;3 p4 W, a! t7 Q5 |; ^
run; B" B% X: r L$ F& @
4 Y9 M: V! j: I! G0 G! Y; u+ q8 _# aproc contents data=mydata;$ @+ L5 ]0 f6 k# A
run;2 L+ O- @$ I! J( w {
, F% [% @$ n' H/ {
SAS creates default variable names as VAR1-VARn when variables names are not present in the raw data file. & a% T' k+ M" A' R % F/ A& ]: b8 N6 v2.读入制表键分隔的数据: 1 v E- M# V2 pproc import datafile="cars.txt" out=mydata dbms=tab replace;$ r* J+ \/ c& D
getnames=no;, h5 W' d9 w: o8 j: K9 Z
run; 7 D5 ]: X( a. O' v( F3.根据不同任务将不同的数据集永久保存到对应任务的文件夹下: 1 ^4 n5 o4 _ G; v8 V3 B! Mlibname dis "c:\dissertation"; * d" g# n: p7 ]5 j& xproc import datafile="cars.txt" out=dis.mydata dbms=dlm replace; + |/ ]8 |2 y% k5 F8 i0 G delimiter='09'x;/ f6 w) b, _. V
getnames=yes;) S7 L/ ^# F e( e9 g
run;4 X: \4 {% G E5 d7 b7 G8 E
3.读入空格键分隔的数据:+ _- B6 V( P ?8 B; i
proc import datafile="cars_sp.txt" out=mydata dbms=dlm replace;5 e& o0 W% \5 g; i" @" ?
getnames=no;/ z2 A! K- u5 y2 n
run; , g) k/ b' ]' Z% u4 s: L# W4.分隔符的终极例子:! \- [, Y0 f8 J4 z7 L
Other kinds of delimiters. B5 W' e! U8 ^# }9 H
You can use delimiter= on the infile statement to tell SAS what delimiter you are using to separate variables in your raw data file. For example, below we have a raw data file that uses exclamation points ! to separate the variables in the file. $ v0 T3 r, U) w9 E+ P2 |* a5 F H! ?6 n2 o, y: [$ ]3 _
22!2930!40990 p0 T- e9 _% C! A# l% r
17!3350!4749 # o- V0 ?" {. M% X22!2640!3799! l" ]$ u5 ~) x& T
20!3250!4816 ; K2 n T1 X- q3 L* [15!4080!7827 # |) |/ J7 n, ]2 _7 bThe example below shows how to read this file by using delimiter='!' on the infile statement.4 s% J8 Y% S. m% _
# @5 y" H7 D$ r$ _4 iDATA cars; # S; P4 Z# j7 b2 lINFILE 'readdel1.txt' DELIMITER='!' ; % q4 m- J0 v, V- ^5 ]INPUT mpg weight price;5 L6 r& ~- l8 i0 V0 R' Q
RUN;* S1 v) n; H8 u( M; o0 X$ U( m
. p7 X% b1 t6 {& C2 u, B9 q
PROC PRINT DATA=cars; b, P: z+ g$ X, vRUN;" E+ P; m5 @/ s3 i# a
As you can see in the output below, the data was read properly.. w& R6 r! a' X) ]( V& ~8 w0 _
+ q- F" f% _& P7 b' x: x
OBS MPG WEIGHT PRICE ; o+ n* }8 ]+ m+ _1 j; v+ w* }# k1 q
1 22 2930 40997 x! N: Z5 d% A9 s1 U4 ~0 o
2 17 3350 4749 4 R2 v n" C2 E3 22 2640 3799 . U: V m5 n) v) W+ h0 [4 20 3250 4816, u1 s3 v( s% U
5 15 4080 7827& q6 A+ {2 C& S* M% `5 R
It is possible to use multiple delimiters. The example file below uses either exclamation points or plus signs as delimiters. 7 d8 q7 I+ B0 `. h _2 L) |5 \0 S4 y" h0 x3 X$ l
22!2930!4099) L0 ?# P6 `- T9 S/ p6 e n' E
17+3350+4749 4 g- V0 ^5 F. V22!2640!3799 Q8 m# f- m# T5 c p% w7 c
20+3250+4816+ e& { l4 M" _4 G
15+4080!7827 # p, p7 ?; o) w f/ b6 O! m3 rBy using delimiter='!+' on the infile statement, SAS will recognize both of these as valid delimiters. ! d A* G: v/ O$ b6 [; l 6 y9 w, `0 l5 Y: q: H# V" zDATA cars;# m! m! o5 K$ q2 \5 I
INFILE 'readdel2.txt' DELIMITER='!+' ;# j1 U8 j! h9 m6 d, M1 W# l
INPUT mpg weight price;* l* ~& ]3 f ^+ j4 J A
RUN; 4 U7 }# T+ y2 p: U4 l& N! _4 U, }! z+ C3 u/ |+ h
PROC PRINT DATA=cars; & g! B3 V. k2 ?8 F) x4 h2 ZRUN;9 g3 ?; s2 Q( q& p, X* u+ A
As you can see in the output below, the data was read properly.1 X# K( H/ u2 _" F9 E
& _, F e- c2 l# xOBS MPG WEIGHT PRICE9 Q' t2 Z1 _9 Z' c5 U }
" e5 k: M+ L7 w& Y }
1 22 2930 4099 3 [. b3 }- l l2 17 3350 4749, w, X* Q! P+ K1 a# k/ S
3 22 2640 3799" E% R' k( C% [5 m8 e5 L5 R
4 20 3250 4816; i* F2 G+ ~, h! ^/ v, V0 o
5 15 4080 78276 \0 \( ? F7 a& _) I
3 I" @, L7 R+ Q+ l
+ G5 J. V2 u( v
import缺陷及注意事项: 4 Q/ X2 k c8 P& oProc import does not know the formats for your variables, but it is able to guess the format based on what the beginning of your dataset looks like. Most of the time, this guess is fine. But if the length of a variable differs from beginning to end of your file, you might end up with some truncated values.; x6 w# A) W( z: u
' J7 X$ g2 _% h; Q9 t9 a! n+ i. S u9 a! y8 C) k8 Z5 r4 N1 h/ U
重点语法-Infile options& Y; C( ?3 s. J+ n$ k1 H3 ^# T
For more complicated file layouts, refer to the infile options described below. ( v) F0 z: {* ?8 F( i1 w8 `% y- C% N O9 [% @
DLM= 6 H3 s! Q, T8 X+ u0 l8 J$ tThe dlm= option can be used to specify the delimiter that separates the variables in your raw data file. For example, dlm=','indicates a comma is the delimiter (e.g., a comma separated file, .csv file). Or, dlm='09'x indicates that tabs are used to separate your variables (e.g., a tab separated file).* ~1 _- n T/ L$ }, V
1 a" Q7 f! z' k8 u! i. ZDSD - U! U! Y7 e. F! x# mThe dsd option has 2 functions. First, it recognizes two consecutive delimiters as a missing value. For example, if your file contained the line 20,30,,50 SAS will treat this as 20 30 50 but with the the dsd option SAS will treat it as 20 30 . 50 , which is probably what you intended. Second, it allows you to include the delimiter within quoted strings. For example, you would want to use the dsd option if you had a comma separated file and your data included values like "George Bush, Jr.". With the dsd option, SAS will recognize that the comma in "George Bush, Jr." is part of the name, and not a separator indicating a new variable.* J; ^! m5 b) x7 H+ A9 p3 o
+ T g5 Q$ i3 q 0 K; r% {* j% S/ ~FIRSTOBS=4 n7 r* v" H) _/ a3 B
This option tells SAS what on what line you want it to start reading your raw data file. If the first record(s) contains header information such as variable names, then set firstobs=n where n is the record number where the data actually begin. For example, if you are reading a comma separated file or a tab separated file that has the variable names on the first line, then use firstobs=2 to tell SAS to begin reading at the second line (so it will ignore the first line with the names of the variables).( [+ a, x/ R6 o: L, n" @0 b; P
7 D. M t$ g% V! ^- {
% D( E* @) p. x$ ?: Q/ o
MISSOVER - Z% ?! r8 _8 Z! lThis option prevents SAS from going to a new input line if it does not find values for all of the variables in the current line of data. For example, you may be reading a space delimited file and that is supposed to have 10 values per line, but one of the line had only 9 values. Without the missover option, SAS will look for the 10th value on the next line of data. If your data is supposed to only have one observation for each line of raw data, then this could cause errors throughout the rest of your data file. If you have a raw data file that has one record per line, this option is a prudent method of trying to keep such errors from cascading through the rest of your data file.' s# |% y* M$ z3 A
* x( A# _6 ^, t& d4 A HOBS=( W+ }# u9 O: O) T2 e1 z* _* L& t4 {
Indicates which line in your raw data file should be treated as the last record to be read by SAS. This is a good option to use for testing your program. For example, you might use obs=100 to just read in the first 100 lines of data while you are testing your program. When you want to read the entire file, you can remove the obs= option entirely.+ r9 B5 A3 r7 S( F
' ^" d6 r1 C. O6 T
A typical infile statement for reading a comma delimited file that contains the variable names in the first line of data would be:$ v. y$ d% E& D- [2 [
' b/ e C; H2 J5 s) R
INFILE "test.txt" DLM=',' DSD MISSOVER FIRSTOBS=2 ; R6 E `0 }3 f" d0 F1 f
$ t0 W8 P1 J8 m/ h+ j
; T; a5 e5 v: u# s" I9 n; d. i4 m读入有缺失值的数据或者读入数值中含有分隔符的数据" j0 G9 I5 \# |; u$ g+ w* ]
DATA cars2;2 j5 P, F& g8 h+ K: @
length make $ 20 ; ) O, C4 S0 e7 NINFILE 'readdsd.txt' DELIMITER=',' DSD ;& J, r2 [' A, s! e
INPUT make mpg weight price;9 t! C& g1 n* L2 Z( p7 y+ Z
RUN; - c* x; v* l# S( s8 X& } / e; I, n/ A+ D& t& ^# a: S! rPROC PRINT DATA=cars2; ; Q! z ]$ {+ ^* [" PRUN; ; o5 X, X0 n% `' g# C& m* Z+ x" ]1 M2 `+ q1 x9 K4 D2 m
" G" ~, ]( D/ U3 V1 D
48,'Bill Clinton',210 ( J* z. u% K% Y# ^! P1 p( r7 Q50,'George Bush, Jr.',180 . @+ G8 [1 V/ ~1 tDATA guys2; 6 ?9 x5 }2 A( Nlength name $ 20 ; |# A" z" `7 m. TINFILE 'readdsd2.txt' DELIMITER=',' DSD ;6 w5 G+ `8 r0 }. ]- s. L
INPUT age name weight ;8 @ \, q8 a+ `
RUN; : e. I2 }6 d( K: J+ J4 y8 f% T4 {: R. n
5 u9 Y7 }, s. oPROC PRINT DATA=guys2;) v. V3 x: ^5 Y( u8 R2 K7 q# t
RUN; p2 Q7 I' d0 R$ l
$ F; d5 S; M- D! e$ J4 U$ ~
最经典例子:从某行开始读入数据9 }; V/ a: j1 j* g* t
DATA cars2; / h- Q( v# b- @& }$ ?( X( ^length nf 8; * S( k7 f) i1 P/ x/ J' G% ?INFILE 'F:\cars1.csv' DELIMITER=',' dsd MISSOVER firstobs=2 ; /* obs=20; would read just the first 20 observations from your file. */4 v8 x8 P3 h6 w* e5 q$ e6 ?
INPUT nf zh hh xb cs IHA fj; & k8 g1 h* y. D$ c' B1 q; HRUN;8 x! ?+ x/ z/ M! ]8 p6 h
: J5 w! Q7 n- b1 v/ R, q6 d. ?% qPROC PRINT DATA=cars2;- G* R4 g2 j# g$ q. g' y5 T$ _ ^
RUN;% W7 A3 _, k( J0 Z, D
+ c" T% i1 e9 [, h# ]
从FTP读入数据; U0 m+ g8 }& D& K) h+ Z. j: r
read raw data via FTP in SAS?2 S- O7 Z0 a0 M$ N! v/ n
SAS has the ability to read raw data directly from FTP servers. Normally, you would use FTP to download the data to your local computer and then use SAS to read the data stored on your local computer. SAS allows you to bypass the FTP step and read the data directly from the other computer via FTP without the intermediate step of downloading the raw data file to your computer. Of course, this assumes that you can reach the computer via the internet at the time you run your SAS program. The program below illustrates how to do this. After the filename in you put ftp to tell SAS to access the data via FTP. After that, you supply the name of the file (in this case 'gpa.txt'. lrecl= is used to specify the width of your data. Be sure to choose a value that is at least as wide as your widest record. cd= is used to specify the directory from where the file is stored. host= is used to specify the name of the site to which you want to FTP. user= is used to provide your userid (or anonymous if connecting via anonymous FTP). pass= is used to supply your password (or your email address if connecting via anonymous FTP).8 r8 a7 [% |( \
1 s8 O t% a: V; a- {% G$ a- Z7 I
6 d1 c; K( l4 h
FILENAME in FTP 'gpa.txt' LRECL=80 * g# Y6 T9 [5 e% p. D CD='/local2/samples/sas/ats/' 1 `) o0 J/ \, [* v HOST='cluster.oac.ucla.edu' ; |- x, \& j( Z$ f. f W USER='joebruin' $ l2 P* x1 U8 {7 f' b$ ]5 {6 t PASS='yourpassword' ; 1 u6 J& |& s& `& rDATA gpa ; " j# k0 j0 Y8 M8 U, Q( `+ w INFILE in ;5 o5 x" k, [* }' `: Q
INPUT gpa hsm hss hse satm satv gender ; 2 g! A& h: N& H8 U3 ]1 ORUN; 1 `) k$ ~, D6 a * t! n$ f. V0 C; P6 ePROC PRINT DATA=gpa(obs=10) ;8 O7 f0 n* c' w* O: L+ B
RUN; ! g6 D0 o- P/ t$ r0 d0 o! [: g
读入多个数据文件0 k3 z" Y/ B2 x; X: k! c. U( ~
5 I, H: b# b& {* ? \
quarter1.dat 4 x! Z8 A# A& P3 q* X1 o! \, N. `+ V( E# J0 T
1 120321 1236 154669 211326 ; d# a9 l* |: g; Z; {+ W1 326264 1326 163354 312665 + L$ \, V* q# Q1 420698 1327 142336 4226857 P- C3 p9 C# k) a
1 211368 1236 156327 655237 & M. T+ i6 X: y/ N: ~) u \9 `! P1 378596 1429 145678 366578 ) U# N) ~6 _" [8 c3 X7 w( j* c7 Yquarter2.dat7 X! S" y6 I& D3 p( ?' x; A
8 M& j+ c" |- W- H+ [# c
2 140362 1436 114641 362415 " e2 r7 x! y/ R/ Q) P3 m2 157956 1327 124869 345215) c% V4 n/ B; |4 j% |4 i1 h
2 215547 1472 165578 412567+ Z; \) `# |1 f6 B, I4 s5 p& {# X
2 204782 1495 150479 3644744 c2 x: w" w; K- C/ A6 L! Z$ a8 ^# I
2 232571 1345 135467 332567' v8 |; W/ a$ C% {0 v
quarter3.dat- i h, Y$ n6 f9 F5 k/ q6 ^
5 l4 G, j, V! B; E) \* h
3 140357 1339 142693 205881 # e C6 x2 C Y3 W& }3 149964 1420 152367 223795 ! F8 b8 D. p9 S# S" n3 159852 1479 160001 254874 5 g2 ?6 |2 s0 O# w0 A3 139957 1527 163567 263088 : i. Q7 Q/ t4 s6 s8 d3 150047 1602 175561 277552# `3 H9 v6 M8 O! ?: `4 U& l
quarter4.dat% s8 }4 n' R; n- H4 \5 X
$ g) Y8 V% d3 m
4 479574 1367 155997 36134 , ]! i& j9 Z( l: l4 496207 1459 140396 35941 ]7 B% e# {% ^- W8 v
4 501156 1598 135489 39640/ ` J, |0 W5 U$ A2 ~
4 532982 1601 143269 386953 w6 T9 G( ^% `: P! ~
4 563222 1625 147889 395564 e" s8 Z7 q9 O x) `5 u
filename year ('d:\quarter1.dat' 'd:\quarter2.dat' 'd:\quarter3.dat' 'd:\quarter4.dat'); 7 s% ^" T' v) |* [6 k$ g( e/ L0 x) S' cdata temp;) V. l- k* Q0 f1 b7 g( ]5 J
infile year; ! n* p+ Z- F0 N9 Tinput quarter sales tax expenses payroll;7 b- U% m2 [8 c6 I( A8 H5 w
run;# w1 N* L6 v G! O7 b4 {7 ^
proc print data = temp;0 F: e' Z1 H H/ z& h9 }% G" l
run; 2 _& G# m/ Z6 \- {- p% h x+ {, B* L i a% y( X. }" G+ p1 s
读取excel 数据集 * w" f& J2 t2 o; \5 w5 n# M, r/ X5 f% v5 y" ~( L% O
Reading an Excel file into SAS! Z. @9 h/ I9 Z4 O
Suppose that you have an Excel spreadsheet called auto.xls. The data for this spreadsheet are shown below. ; w1 f# S' l% U" D, E- c8 x5 p& G { U0 E( D8 n, bMAKE MPG WEIGHT PRICE ' c% Z" e) L- R! a4 @& HAMC Concord 22 2930 4099+ _! W$ h/ }! \7 p$ K
AMC Pacer 17 3350 4749: c/ p$ X/ `7 Z# N6 @. G( }2 h
AMC Spirit 22 2640 37995 l% t! H1 A/ K. w9 ?0 k7 G/ z
Buick Century 20 3250 4816- F% `+ c8 ?* R) f# J3 Y
Buick Electra 15 4080 7827$ U. E5 f2 z- J. `8 Q0 `8 ]1 U, k
Using the Import Wizard is an easy way to import data into SAS. The Import Wizard can be found on the drop down file menu. Although the Import Wizard is easy it can be time consuming if used repeatedly. The very last screen of the Import Wizard gives you the option to save the statements SAS uses to import the data so that they can be used again. The following is an example that uses common options and also shows that the file was imported correctly. - z/ ?8 ~" P6 t" O7 l* E4 b( c* P+ `* P 5 S# r- N6 P( D$ Z: \, LPROC IMPORT OUT= WORK.auto11 U5 d. ~. T% F
DATAFILE= "C:\auto.xls"& T! @7 `/ m3 | C- O$ x$ Z6 ?
DBMS=EXCEL REPLACE; ) s. J5 M: X. A7 I SHEET="auto1";" C2 f5 N% V1 H! W+ ]* j+ a
GETNAMES=YES;+ _3 Q3 j9 u' P9 M) `& A' Q; I
MIXED=YES; 8 \ ?5 V. B) `4 n' B USEDATE=YES; : y6 V4 K1 ]4 r8 X* P7 L SCANTIME=YES; - b1 Y t9 {9 tRUN; - X6 N# `/ U9 @proc print data=auto1;) |# ^( M3 v1 i6 Z
run; ! M7 p1 l( L1 `) h 1 m0 t2 F- ?8 QObs MAKE MPG WEIGHT PRICE7 \% [* t: M2 U7 Q9 r$ T
& P. B% V' h$ [4 M. c1 AMC Concord 22 2930 4099 7 v; _- }3 P" t" g9 C" f/ g5 b4 K2 AMC Pacer 17 3350 4749 9 J( k+ i8 s; q# M: e. ^- C3 Amc Spirit 22 2640 3799# B5 K+ r* c) _! o
4 Buick Century 20 3250 4816 1 b p* K& |4 a5 Buick Electra 15 4080 78277 e: B7 @% y% Z& I0 L
First we use the out= statement to tell SAS where to store the data once they are imported.( e u# {: u" S- O [! C
Next the datafile= statement tells SAS where to find the file we want to import. 4 L& S7 m0 L3 r# rThe dbms= statement is used to identify the type of file being imported. This statement is redundant if the file you want to import already has an appropriate file extension, for example *.xls.& N+ V6 d7 K6 x8 F/ m3 B
The replace statement will overwrite an existing file. 6 a) }+ M- f- c1 \# T7 L' ATo specify which sheet SAS should import use the sheet="sheetname" statement. The default is for SAS to read the first sheet. Note that sheet names can only be 31 characters long. # {' x8 g2 r. u" D% u' ]- B; s" lThe getnames=yes is the default setting and SAS will automatically use the first row of data as variable names. If the first row of your sheet does not contain variable names use the getnames=no., ?# L2 C. L, O2 ^/ S& Y f
SAS uses the first eight rows of data to determine whether the variable should be read as character or numeric. The default setting mixed=no assumes that each variable is either all character or all numeric. If you have a variable with both character and numeric values or a variable with missing values use mixed=yes statement to be sure SAS will read it correctly. , o! D0 m/ \0 L# bConveniently SAS reads date, time and datetime formats. The usedate=yes is the default statement and SAS will read date or time formatted data as a date. When usedate=no SAS will read date and time formatted data with a datetime format. Keep the default statement scantime=yes to read in time formatted data as long as the variable does not also contain a date format. 9 M$ @2 N; a& u$ TExample 1: Making a permanent data file! G) b: l$ U2 S6 @8 D: j0 P9 \
What if you want the SAS data set created from proc import to be permanent? The answer is to use libname statement. Let's say that we have an Excel file called auto.xls in directory "d:\temp" and we want to convert it into a SAS data file (call it myauto) and put it into the directory "c:\dissertation". Here is what we can do. - t, @9 Y! M- p1 H; F+ z* D) { f# Z + s P' p3 B+ flibname dis "c:\dissertation";, k2 j0 `4 a" Y' N3 {; A
proc import datafile="d:\temp\auto.xls" out=dis.myauto replace; 5 c$ Y4 |% c2 ^9 S8 |6 v% B5 M; [run;8 `: ]! W# v7 d7 J9 H# y3 @9 b
Example 2: Reading in a specific sheet( \9 ]. ^. H9 N# Q6 D, g2 y% S( l
Sometimes you may only want to read a particular sheet from an Excel file instead of the entire Excel file. Let's say that we have a two-sheet Excel file called auto2.xls. The example below shows how to use the option sheet=sheetname to read the second sheet called page2 in it. / `9 Q% x* c; I; z l/ N8 g3 a4 L, f. J
proc import datafile="auto2.xls" out=auto1 replace;+ h# E5 f m6 x, G8 M0 B
sheet="page2";& K: ^! x2 z. N8 ~* Z) P+ X" s
run;+ c& M% ~9 f$ w6 M; _' A
Example 3: Reading a file without variable names: n5 S% a1 t. a
What if the variables in your Excel file do not have variable names? The answer here is to use the statement getnames=no in proc import. Here is an example showing how to do this.9 X, v G# }3 g' V3 ]
& ^! I+ Q) V/ E. M
proc import datafile="a:\faq\auto.xls" out=auto replace;) Z$ j9 Q0 U+ |8 [" A
getnames=no; ) d5 P5 o. Q9 i$ H9 H% z$ Frun;3 e" b: ~4 r# _- q+ {3 V& \) w1 c
Writing Excel files out from SAS/ J# d) M# y6 K: b; g5 {$ g
It is very easy to write out an Excel file using proc export in SAS version 8. Consider the following sample data file below.( u o+ s. @+ ?( }
5 x4 F$ d! T6 [- m0 {6 h4 yObs MAKE MPG WEIGHT PRICE - T; g; H/ B7 n; Q1 U' S1 AMC 22 2930 4099) B; N$ T% `8 o# W6 D" }1 e: o( l
2 AMC 17 3350 4749: u) r! U9 E* H7 o0 |
3 AMC 22 2640 3799 : z( m0 S6 @# Z. [* q4 Buick 20 3250 4816! m2 R0 V6 i$ k' s5 r
5 Buick 15 4080 78276 Y; \# q/ z) q5 ^
Here is a sample program that writes out an Excel file called mydata.xls into the directory "c:\dissertation".8 h2 G" W0 U- O; ~5 \2 m- Y
e: X0 a* `6 J# S8 B& i1 g- y
proc export data=mydata outfile='c:\dissertation\mydata.xls' replace; + q! o1 n( ^9 M; Yrun; 5 o* R3 m/ ?) M / `7 ^- E( ?: l. q" I; K# ~SAS读入复杂分隔数据——字符长度不同,字符中间有空格作为间隔符 8 ]6 Z* x2 y9 w5 \+ h, Y1.字符长度不同 V Y: A! [. J, W! s
data web; 1 B4 i6 A% Z+ I, b( ^3 Ylength site $41; ; ~" Y/ \+ V+ Ginput age site $ hits; : u: v; T( Q0 O* n& Rdatalines;% T0 p1 E, b0 a( W1 y& i0 ]
12 http://www.site1.org/default.htm 123456 5 y7 v6 y1 D3 \* w9 t; Z130 http://www.site2.com/index.htm 97654) u( n8 W9 {3 x2 e, g! ~0 Y
254 http://www.site3.edu/department/index.htm 987654, _+ e5 V! ?- |: i1 \# P5 g5 G
;2 S' p6 Z. M6 k" K4 Q S% g0 U
proc print; / i1 ]. p3 N9 }8 B/ Krun;) U3 _* b) k5 z |! E
8 ?1 \+ j( h" X6 E) K& F: [
Obs site age hits ; E: v$ o5 R( `1 T# n$ _( b . ]: P" U+ k3 [# e, {1 http://www.site1.org/default.htm 12 123456$ W; Z3 N3 }8 H6 e% _8 }
2 http://www.site2.com/index.htm 130 976549 g/ H# K5 g) P! _/ q6 V' G
3 http://www.site3.edu/department/index.htm 254 987654 6 l: B& \- ~: J4 p: \6 r+ o& l; `2 ]+ r. h' }/ d. `8 S% @1 i
或者用4 l9 R' {+ y R6 b
data web; [0 e* P: d, w' v
input age site & $41. hits;# k0 i1 L# |) x0 F
datalines; , G4 z" {# G4 {3 N( e. w: o+ D5 m- T12 http://www.site1.org/default.htm 123456& H9 a' h1 F1 G
130 http://www.site2.com/index.htm 97654; l$ J" l* h3 u; q' d
254 http://www.site3.edu/department/index.htm 987654 : A5 G! W% |$ t+ F;. C7 U9 k# x2 A8 ~9 L1 q0 n- w
proc print; ( r. r4 x4 Z2 `. @5 l& n' c' jrun;& F7 ^1 y' [8 f4 x; R
( |& V, w: f q, B: v1 g
Obs age site hits % {0 u% N' @- { z' H' K5 ~ 7 i! ?' X0 d! s1 12 http://www.site1.org/default.htm 123456- {: \9 X5 S V$ I/ ]
2 130 http://www.site2.com/index.htm 97654 * C, z7 o" n5 x& `0 u6 F9 O3 254 http://www.site3.edu/department/index.htm 987654 # x" E4 j, e! k& C: c # G0 o( B& U' f8 E # l0 Q/ ?' I. _2 z- y2 Y2.字符有多个单词,单词之间用空格隔开 H3 Q$ B5 P& L5 S
data fruit;% V6 f/ z/ c3 S- C. a' t' _% p/ h( ?
infile 'C:\messy.txt' delimiter = ' ' dsd; 6 o( @ z5 H. [; b/ o" G0 ~length fruit $22;( N+ H0 S3 ^( {
input zip fruit $ pounds;. ^) `: w2 w$ p2 G
proc print; / i) b1 w7 K4 E3 E) ^5 x( Yrun;+ h' L3 v' T' E3 }) j
( E* g! Y. Z* y% Q: M+ ^Obs fruit zip pounds# Y3 ]2 |/ W5 ]* |
# u+ j- Z# p$ F( @7 K8 [3 H$ I
1 apples, grapes kiwi 10034 123456 + E- B6 Z4 N) j! X! D9 U% T) Q2 oranges 92626 97654! H& ?+ Z, E: ]! s- q4 Z: T) n- z) g
3 pears apple 25414 9876545 W( e) U0 u. a* M' G7 T0 r) g4 c
或者 , g6 b H' ?) ^; E0 c5 ?data fruit;$ s" `5 r% Q w: w z1 f/ W0 i
input zip fruit & $22. pounds; # Y2 ?' Y2 P* Q1 D1 ~+ R7 Gdatalines; - @" |" r. C1 t10034 apples, grapes kiwi 123456 5 s; ^3 R# e, j92626 oranges 976543 L5 I2 ]/ S: k8 O* Y
25414 pears apple 987654+ S: m, h/ J) M/ s( K/ u
; 6 f. v2 B# P6 ?2 `/ G, W uproc print;6 |$ t$ m2 G+ \& W
run; & m) Y. w% `2 C0 h * H* x$ }" ^- ]7 {2 zObs zip fruit pounds# R+ U6 S. D# M" B
+ ]; K0 @) a) ^; }$ g
1 10034 apples, grapes kiwi 123456 . [/ d: N4 \9 l( i2 92626 oranges 97654 4 ?) [$ _" x/ N/ g! M( J/ K( ]3 25414 pears apple 987654$ f+ G8 q" U. d# D- P$ ~: p( ^
, ^1 T. G: o% d) G+ K$ g: u5 t没有格式库的情况下读入数据:% `- v/ v7 y: E" d
read a SAS data file when I don't have its format library" E1 N+ l i8 @# g6 n& L! L7 f5 x% }
If you try to use a SAS data file that has permanent formats but you don't have the format library, you will get errors like this. * h9 b- {, e& ?+ l. y Z ' _9 j8 {( o E" E7 a# pERROR: The format $MAKEF was not found or could not be loaded. 9 Z8 m" Q: `6 O& p" L: G! XERROR: The format FORGNF was not found or could not be loaded. " {" b7 C! K( l: P2 HWithout the format library, SAS will not permit you to do anything with the data file. However, if you use options nofmterr; at the top of your program, SAS will go ahead and process the file despite the fact that it does not have the format library. You will not be able to see the formatted values for your variables, but you will be able to process your data file. Here is an example. 3 w' C0 @) a! W& d. j9 _" V; a T$ T) g R
OPTIONS nofmterr;- U% _& k# l0 t: A2 _0 U5 V
libname in "c:\";+ p3 C! g8 T2 e! I
( A$ ], |& M' g0 y
PROC FREQ DATA=in.auto;4 }5 L1 g- R3 u( U2 Y
TABLES foreign make;% C' X2 ?% N" W
RUN;6 ?# s$ T& b# C& ]' j
高效的保留或者去掉部分变量的方式: ) ]* n& Z& `5 c& D' KThe following program creates exactly the same file, but is a more efficient program because SAS only reads the desired variables. ( p+ Y8 v; M. {7 I/ [7 z( M# D, F- ?- L! U0 {! s
DATA auto2;1 Y ?1 g3 s6 n# r' E' H
SET auto (KEEP = make mpg price);$ b) z* K' }$ v5 @- P' p
RUN;, S8 W9 Z! O2 n) v& w2 Z
The drop data step option works in a similar way. 6 D- \/ r8 N2 D$ Z 3 x2 v) O$ @$ [: c$ {+ ~! P f! oDATA AUTO2;- b! Y8 m5 Z1 t6 ]) X* r3 }
SET auto (DROP = rep78 hdroom trunk weight length* P; K% _% R6 j$ w: e c) q& S1 H
turn displ gratio foreign);. Z) F; L5 R+ K6 g+ e: {
RUN;" \( j6 W* L8 D* T+ r6 E
6 w; Q; r" k6 n/ S: N8 c2 c) a _; s* a, e j# V
比较双份录入的差异是否存在: ( w' h4 ~5 J, Y. h+ K$ ?9 T& F; X: oproc compare base = person1 compare = person2 brief; 3 y' ~1 L. {# S# T+ vby id;. S- `4 r- A+ ~
id id; ( G) V9 O J" W) L; X' }) {run; / }# `& n1 Z$ W8 E0 } ; L: e+ c& \6 j5 m9 V/ K6 @- ]/ q0 l5 T2 M