4 Z" Q X: v# r/ zSAS creates default variable names as VAR1-VARn when variables names are not present in the raw data file. " J# @6 x+ T& ?' b& M 4 O/ d y$ t+ l2.读入制表键分隔的数据: - R2 R' m+ @3 C# n3 Wproc import datafile="cars.txt" out=mydata dbms=tab replace; 3 A' ? ]+ W3 a* Q getnames=no;, K1 E) T0 W: e: U7 i, O* f8 S- j
run;9 @/ W0 T1 `" d4 A0 g( k5 L3 ~0 _
3.根据不同任务将不同的数据集永久保存到对应任务的文件夹下: # G# D! M& f7 A# ]1 T0 L3 Llibname dis "c:\dissertation"; - D$ K Z3 J5 b: |proc import datafile="cars.txt" out=dis.mydata dbms=dlm replace;: X0 x1 s* {6 H. M
delimiter='09'x; ( \& ~' A, {9 A) ~: Z+ G8 V getnames=yes;( R: e" a% }: x- }$ ^5 C2 I
run; % m1 m8 i: P3 @* E2 d& N3.读入空格键分隔的数据: 0 K- j, q: E% M' L; q6 Pproc import datafile="cars_sp.txt" out=mydata dbms=dlm replace; + h/ f0 r9 k8 X5 Z9 {. i# t5 M0 }getnames=no; 8 ~# U9 n& ^8 R& V# }1 k; Srun; d. `, \* X# S5 h& O8 K, t7 ~
4.分隔符的终极例子: $ ~/ {7 r* o5 c4 {9 @! y* ^' aOther kinds of delimiters2 H% \" l: V, L3 J, n5 T) L# n" @2 ?
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., A3 c: I: H* n+ j* ~+ w: {
& Z6 L/ q& W2 ~6 t4 C* f K
22!2930!4099 * w1 K4 x. i: _, c; h17!3350!4749 : Z% W* [5 A3 b2 S22!2640!3799 2 C f' v; n. d, s9 f+ D8 x0 f20!3250!4816% l5 A8 o2 m. A. b1 D" G) o: B5 H$ w
15!4080!7827 , U1 G! o& g6 ]+ B2 vThe example below shows how to read this file by using delimiter='!' on the infile statement.7 \& c( V! S; _( A5 i7 r
6 a. Z3 G+ p v/ f5 f
DATA cars;, S* g7 I) i' d# [. }+ @
INFILE 'readdel1.txt' DELIMITER='!' ; h/ \- p& r$ TINPUT mpg weight price; 6 N- c& C3 F9 x8 h' S; A l3 h+ dRUN; # Z4 ~' W- b% \) a/ S" h% i/ R u& G
PROC PRINT DATA=cars;& k# P4 w5 C5 ]- i/ z' c
RUN;2 s0 k+ s" y) v2 A
As you can see in the output below, the data was read properly. $ _* I& }" w, a ; V: M, t: I% _# h- Q. s/ rOBS MPG WEIGHT PRICE I: e/ t* q. v9 |* R" f8 P7 }' T# t/ F8 {9 F0 g# n
1 22 2930 4099 6 {* N$ V8 Y, O1 G9 O2 17 3350 4749( p8 N* Y8 h3 V% D
3 22 2640 3799" b- B5 {6 g; k) |, }0 U+ A
4 20 3250 4816 0 K/ S; x; m" }' N$ X/ v; p5 15 4080 7827 % U6 L' d5 |6 A bIt is possible to use multiple delimiters. The example file below uses either exclamation points or plus signs as delimiters. ! p. Y! Z3 J, X6 ~! _+ l6 k8 O: a/ |1 ~1 I
22!2930!4099 8 e1 `& h3 R1 f3 l- a17+3350+4749 . s5 o N4 K: i) c6 u1 M" X( D3 n! {22!2640!3799 - Y" y8 w. K8 W. V3 t' v7 I20+3250+48166 i: B( U& f1 |7 q6 t$ G
15+4080!7827! y4 ?: G5 C+ O9 `
By using delimiter='!+' on the infile statement, SAS will recognize both of these as valid delimiters. % G+ R: z8 |; U. C, W) E* l; X$ W% o+ T6 n
DATA cars; / z2 T4 B7 i0 q" w% ^3 t4 R) [INFILE 'readdel2.txt' DELIMITER='!+' ; ( v9 L' ~4 d9 F+ t! t5 C' M. K7 f8 YINPUT mpg weight price; 7 w0 z& X. n& n# ARUN;4 a: L D- R+ ]" ]
' T' ?0 q+ y, l! p+ Z. A! jPROC PRINT DATA=cars;! `4 h3 q$ Z& j8 b8 G5 I
RUN;, y/ }2 V+ ?* S; [) y
As you can see in the output below, the data was read properly. 4 y; L3 ^! D0 i0 u6 l. P; b8 a 8 c5 Z# m/ F U1 y2 dOBS MPG WEIGHT PRICE1 ?# C0 H: r& B8 I* A- l
4 g5 P* V$ q7 v# F; w# a7 p/ S% U, c - v4 x7 I+ e9 a& Rimport缺陷及注意事项:( C" g& {0 |/ l4 z8 C: @+ F
Proc 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. " a. B0 L+ ]' k; ? C: u; z; B/ G5 G" k, T; c' ~
2 C7 w' i! W! i. y0 _
重点语法-Infile options+ d3 T2 @8 N( C) ?1 }9 s8 M# w+ G% y7 g
For more complicated file layouts, refer to the infile options described below. 4 S% ^' u% I8 X, R* b2 a" _* \. t" m5 y 9 e. y4 U; B+ i" T% k6 |DLM= , k- d, K( F5 Q" W% _The 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). : t0 M6 c) F4 `- k) f* d $ y2 r' e( i0 l) [: |DSD+ n$ r- I& g* Q. z/ n8 _) W X9 b
The 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.5 p' q- v& _4 m4 J0 P5 V. _5 D
7 S1 i1 v, A- x' o9 J7 V& w , }% N. A( ]: N/ N% ^. yFIRSTOBS= * I" H( b2 w2 I: x: {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). 7 |3 l/ J p) H2 S( o 3 H* ]* \0 y+ ~+ }6 P- f) E0 K" R7 ]* E9 C; J; u
MISSOVER * e$ b6 b# ~$ h/ R) [+ F* A7 OThis 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. 1 E$ O0 z+ P @: X' u; ^ 3 A7 o! U! t, qOBS= 5 K# T, _, }1 Z9 d) z- Z( J7 d* PIndicates 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. 7 U$ r1 l; a6 [$ k, L $ f+ D5 U2 I3 Y0 C" V5 ~: TA typical infile statement for reading a comma delimited file that contains the variable names in the first line of data would be: E% u$ z1 G* b, f. N# R Y8 ? J% |- n) J9 q+ L' c
INFILE "test.txt" DLM=',' DSD MISSOVER FIRSTOBS=2 ;$ m& O8 \+ g2 g1 X4 W
3 @2 Q; a" y( l" U
8 i d- _6 C/ J0 e3 |: X读入有缺失值的数据或者读入数值中含有分隔符的数据/ M, t V; [+ N9 N: v, f! O
DATA cars2; 4 @! ]' c: M7 m$ Nlength make $ 20 ; - }) _. A1 y6 T' T# C" a" K7 QINFILE 'readdsd.txt' DELIMITER=',' DSD ;! {3 g# j4 Z u
INPUT make mpg weight price;& u2 m, {1 F1 u Q/ Z
RUN;6 c4 L- {& R. x1 t" ~) T( T
2 q6 o9 _$ b) d' z: u9 K$ e
PROC PRINT DATA=cars2; / ?+ s% L( C) i' T) ]RUN;* u& ^4 R# a4 ^# G, u5 X/ ]
p+ ~3 P, Z7 O& d* }+ a; g; U Z- V$ w5 I4 t$ h
48,'Bill Clinton',210 9 U: s$ e( b1 N( g: V( w50,'George Bush, Jr.',180 1 K3 v$ c$ T- v W( BDATA guys2; $ k! m2 |: b- N nlength name $ 20 ;& Z0 T) ~$ N4 c0 M: }% t
INFILE 'readdsd2.txt' DELIMITER=',' DSD ; ; A* F7 }; F3 M" X0 Z. R* l5 H; RINPUT age name weight ; & K6 s+ {, y8 l% ~! b, W, ?2 nRUN; # O) y6 I8 c$ [! h. d! C/ f9 D" ^$ n1 d/ z
, x# V/ W4 Z( q/ |. O( _+ z/ LPROC PRINT DATA=guys2; + c% w- O( N) H% y i T( RRUN;8 K; z4 r ]* p7 {
* [$ S) `& ?2 K/ b5 I* n" l" ^
最经典例子:从某行开始读入数据 ; T3 O! R) f: zDATA cars2; 2 s0 k/ h0 g# t3 x( Vlength nf 8;. o9 [5 A* d5 U) ?
INFILE 'F:\cars1.csv' DELIMITER=',' dsd MISSOVER firstobs=2 ; /* obs=20; would read just the first 20 observations from your file. */ # Q9 d* b2 O( AINPUT nf zh hh xb cs IHA fj;5 p2 S& M$ w; w8 }# u4 C
RUN; 1 Y& L5 s# ?+ t. U! P; F% j0 \( [$ @" T) X
PROC PRINT DATA=cars2; 5 u, z6 r1 g; E, q4 I# ^RUN; ! }9 f( Y1 @- g! u. F& Q& i1 w( I s# H: E, P8 s+ w/ P
从FTP读入数据: Y$ w2 W9 K _) q* w h0 o
read raw data via FTP in SAS? $ h" z/ D8 u2 k8 FSAS 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). $ F# a/ J- A; [; L# ^9 v* L. c1 Z& _$ c. M
2 n- w, j u: k- \% X
FILENAME in FTP 'gpa.txt' LRECL=80- ]0 B1 ^4 Q+ c0 j p
CD='/local2/samples/sas/ats/' 3 m" v7 Y$ I+ e) x HOST='cluster.oac.ucla.edu'% y% q& L( @) b- R+ r1 X
USER='joebruin' + y: Y* b) @3 W6 v2 H6 ? PASS='yourpassword' ; ! k9 i9 v2 t5 H5 u! cDATA gpa ;" b. f: G/ g+ Q! t; f' p' _
INFILE in ;1 D L9 o# m/ V- s
INPUT gpa hsm hss hse satm satv gender ;, v5 R2 K l; l
RUN; 6 Q% Z) ]( x5 A" A0 }/ a2 R " e+ y0 K+ \) f; k8 S4 jPROC PRINT DATA=gpa(obs=10) ; 5 j! x3 u$ U) f) D2 iRUN; : q! p3 |. q# O0 w ( X, O% u+ w8 Q+ s9 B: y. x读入多个数据文件 9 l0 x0 J8 @: H% P2 P6 J: x4 r; \$ `& \# C
quarter1.dat % d; u' u! ` d! ^ $ `# a/ t* x7 x! }1 120321 1236 154669 2113266 }& X. V& ^8 R7 \8 G+ w
1 326264 1326 163354 312665* {" H: c7 x& m. V( o7 |
1 420698 1327 142336 422685! [8 K4 s& V; m" T& U
1 211368 1236 156327 655237 : o+ s8 I. \- o' i0 f1 378596 1429 145678 366578: W( ~ }1 k& r$ p' b. T0 _( W
quarter2.dat ! H: D, p% b% D, q! o. i# | w/ C' {1 e
2 140362 1436 114641 3624159 Z- b( o; o; W! l9 |0 I) S
2 157956 1327 124869 345215) J: Z* s. d; e% i5 p
2 215547 1472 165578 412567 + Q H1 W2 e6 y- g* q2 `2 204782 1495 150479 364474% z& I7 B& `5 v* B4 v" y; l% u
2 232571 1345 135467 332567 $ Q" a7 I |& p& e# \quarter3.dat - {3 |4 N( B; i X) d* e7 Q " [& a, Y& v1 G* |7 ?. E- m3 140357 1339 142693 2058813 b1 |- C( L4 X% U
3 149964 1420 152367 223795 ! t$ j- {6 h0 M5 f5 g J3 159852 1479 160001 254874 5 d0 H2 \; p! C7 e) O3 139957 1527 163567 2630882 G# R. S) l9 W* ~4 g
3 150047 1602 175561 2775526 p8 } J% l! `* L( D6 P# W
quarter4.dat1 B/ s* x9 I4 i8 P! o4 R) e
8 z8 x6 h$ `& e4 r0 S: E" ]
4 479574 1367 155997 36134 ; |. B) t k: t; S# b, o4 496207 1459 140396 35941 ; A0 n A" g: O/ m/ c& H4 501156 1598 135489 39640) ?) X6 f" s) H7 J- Z3 z; w, X+ k6 ^; F
4 532982 1601 143269 38695 ; v" C2 E2 M5 e4 563222 1625 147889 39556 1 f' g3 O3 {& P6 s$ K4 Dfilename year ('d:\quarter1.dat' 'd:\quarter2.dat' 'd:\quarter3.dat' 'd:\quarter4.dat'); + r/ |4 c2 x- N: q1 @data temp;, o' G5 `/ G0 R: y2 c( f8 `% M
infile year; 7 [7 i' o/ X5 f7 l& @input quarter sales tax expenses payroll; r% N5 ?$ {- O! G' E
run;2 Y# Y/ s! J' s: k6 s9 w/ _
proc print data = temp; ' F' z* g) r, Z# q7 e+ a+ Trun; 4 F4 C1 J: |8 W' n , x/ o7 X3 l) m: [- V! B读取excel 数据集 5 H+ u- p( U0 {/ E" x6 M5 Y# T& y( [. M+ I
Reading an Excel file into SAS / y1 W% u5 ?9 I# `5 V# ?8 T" E* zSuppose that you have an Excel spreadsheet called auto.xls. The data for this spreadsheet are shown below. R$ U4 M, E) q: Z' V: h0 r
; T0 @. g! V1 B' gMAKE MPG WEIGHT PRICE1 c# Z, B$ ?" {6 \/ r
AMC Concord 22 2930 4099( y: }& G+ w" y2 P# j/ X
AMC Pacer 17 3350 4749. c- W5 q: D6 H: v! N6 q7 g) t
AMC Spirit 22 2640 3799: M9 F$ T, q9 @7 H8 | E- {
Buick Century 20 3250 48168 |5 j' E8 L1 e( h1 h" V& I
Buick Electra 15 4080 78274 T: ^' t* p, `7 i s$ m
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. " s m7 l) i. U) R- t' @. T5 a$ T/ l1 A
PROC IMPORT OUT= WORK.auto14 ~/ A( }1 J; c$ A3 X( J u
DATAFILE= "C:\auto.xls" $ Y k: N F; y DBMS=EXCEL REPLACE;- A; Q2 o# D5 j6 X$ G
SHEET="auto1";9 Y" y2 }9 B3 g
GETNAMES=YES;7 j @+ q; A2 r3 a2 x7 F2 h
MIXED=YES; # U: [! {' F/ ]: E7 r USEDATE=YES; + H( R; B5 ^. {1 Q SCANTIME=YES;' ^! g% M$ y/ R3 n0 j# S
RUN;+ }$ O" A* U4 b6 @. {* c, y0 @
proc print data=auto1;' w6 j* ^! x* Y; P
run;' c" i2 f) b4 E; m. y: a
+ s% t) i* R; J8 b) D
Obs MAKE MPG WEIGHT PRICE- s4 `! C3 F- f, R, J3 K
' J" Q' T% c' x' z& `; ?- `: [+ B1 AMC Concord 22 2930 4099 / U+ @: c, u6 r4 g3 U& q7 a. P0 |- b2 AMC Pacer 17 3350 4749 ! _' d4 h4 x1 [4 e' H1 I3 Amc Spirit 22 2640 3799 ' h8 s+ y0 e t% {4 Buick Century 20 3250 4816 ! B+ x% T7 j0 h* N5 Buick Electra 15 4080 7827 8 I2 p% b" ]6 t. c, j8 Z# y+ A7 zFirst we use the out= statement to tell SAS where to store the data once they are imported. : u0 j8 N1 ]* sNext the datafile= statement tells SAS where to find the file we want to import.; [5 T* i: J: F0 Y: Q4 Z: C
The 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. ( p6 n' |1 `& ?/ HThe replace statement will overwrite an existing file.9 h" H; v, f1 R+ N! A
To 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.' I( F, L6 L+ d0 w
The 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. ' N& G/ O) N |, k% ^' y$ K$ uSAS 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. 6 a z4 A {3 h- P4 a$ D% O( iConveniently 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 ]+ i2 @1 C* O/ U9 M" i; l
Example 1: Making a permanent data file 9 O( ~' i% s0 C. Y8 W. {4 gWhat 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. ' o' z+ t4 Y2 w* D3 ^& t) A+ h7 g3 f5 a i& V; X- C! j! ^- A
libname dis "c:\dissertation"; `+ I! k1 y$ w+ i$ e) i5 l
proc import datafile="d:\temp\auto.xls" out=dis.myauto replace;# Y/ Y+ @0 Y, c( \9 ?: y
run; 7 p6 L" Q ?% ^2 Z. e" ZExample 2: Reading in a specific sheet, k; c s" b& s, S5 J
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.1 x; _* e+ b; K( t. R4 L+ b. `
# ~: N7 ?' v5 Z4 X/ J1 a6 _proc import datafile="auto2.xls" out=auto1 replace; & S( ?' R4 Q" d# s! rsheet="page2"; ) `; L) X" U, [, Mrun; ' V8 t4 B( G; h* a0 NExample 3: Reading a file without variable names3 @ V: N6 p4 J+ P; v4 d2 y
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.2 t3 B7 ]8 S0 E/ J' o% u# P
. o/ H l# {5 j, v# w
proc import datafile="a:\faq\auto.xls" out=auto replace;- [5 i- O: e; U4 G( ~4 x* t
getnames=no; u: } x1 _" y4 Q0 nrun; 1 Z7 }" a N$ Q$ Y. SWriting Excel files out from SAS1 L( b+ Z M) g7 \4 A# 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./ `4 W& \, q8 i: C. ?8 w# I6 s
, W8 j' ?' o) d1 Z6 b6 p4 O2 q I/ f
Obs MAKE MPG WEIGHT PRICE/ k7 f) V% o' m. k& A0 u# d
1 AMC 22 2930 4099% `+ c4 r! E: s0 u+ i" Z# G
2 AMC 17 3350 4749 ( j4 k9 k1 y1 e6 _7 f3 AMC 22 2640 3799 , {2 a8 c# f! b; d. b4 Buick 20 3250 4816 g! B9 g/ t8 C" Y. ^
5 Buick 15 4080 7827 , {/ M6 ~8 f k' _; Y' }Here is a sample program that writes out an Excel file called mydata.xls into the directory "c:\dissertation".' ^; M5 Q4 s9 y* K; {
% M* U4 ?* t7 r4 V5 \- z
proc export data=mydata outfile='c:\dissertation\mydata.xls' replace; 1 j( N) {9 R% P5 ^run;1 @, y* x# \# ~0 a" C
# d- i( r0 S/ ^# k' n# v$ wSAS读入复杂分隔数据——字符长度不同,字符中间有空格作为间隔符 1 ~& H) w- K p. C, J ^1.字符长度不同1 Y: |3 v6 [) T% F+ p
data web;- A) k; y4 e. W
length site $41; - |; J4 Z( j& N) W* O- b! Ginput age site $ hits;) e# [) p1 }- p+ q6 K# O
datalines; 5 p, P" W* D4 B# @8 B8 y12 http://www.site1.org/default.htm 123456 1 Z$ m% Z( u0 k4 J7 j* g; g5 @130 http://www.site2.com/index.htm 97654 @. A3 S; _8 x( T- J% e# {3 w$ @' Y254 http://www.site3.edu/department/index.htm 9876540 ^/ [" ^" n g/ _/ [
;% k+ L/ F1 e" m. g: K0 o
proc print; / S6 f6 n9 B) }1 \. {6 r1 ^run; + k: G% R9 T, b. b0 f' l& g/ \8 E& E+ q! r. @( R% r5 r
Obs site age hits5 q6 Z1 W3 w+ V9 b; X7 a: h
8 n' i1 g* k8 p* U ) Y. a/ D+ d) R: L2.字符有多个单词,单词之间用空格隔开 4 _, D+ f( `* U3 @' C* D$ I( `data fruit; 3 f# Q4 G6 A( v# {- `infile 'C:\messy.txt' delimiter = ' ' dsd;4 ]2 S# M+ }# e8 V: q5 V
length fruit $22;' _; c! U3 p! d/ B7 }' h4 Z
input zip fruit $ pounds; 3 N, s3 S6 Z" g9 k% \proc print; 8 ~2 y. N7 c& f$ i) E7 q; Lrun;+ }& N; f" W/ r- J3 W8 y% ~
; T g7 ^! n5 `' B0 s" T2 QObs fruit zip pounds 5 R( p) N6 I+ H; z9 A& T# q - O: D- f0 J& b. h5 ~1 apples, grapes kiwi 10034 123456 1 R$ Q1 z2 b$ h4 [+ a5 r6 `7 P8 R# P I, J2 oranges 92626 976545 n+ s! _' @+ Q3 p
3 pears apple 25414 9876540 q/ P! w C8 x4 e2 Y8 C* a; T
或者 " A3 o. i. L+ V6 b7 s! B3 Wdata fruit;' }6 x7 ?0 _5 b/ O, }3 @2 ]
input zip fruit & $22. pounds; 2 n# x/ E( o8 ldatalines; + O- W5 ?; u1 K3 j6 ` k10034 apples, grapes kiwi 123456 * ^& x8 G! k* {92626 oranges 97654 : P3 E" z% A. d" N# t' d% f25414 pears apple 987654 ; L' Q' I1 d* y;, X6 V- K8 k Z e0 `
proc print; ( u$ j1 T3 @ e C( _8 Srun; 3 B% ^7 B* p3 r- m4 y6 e# ?% v) E/ S3 J' v) p9 }5 K
Obs zip fruit pounds 1 x: x. \, U$ N" \& c3 X+ _, A& q; T5 j1 \
1 10034 apples, grapes kiwi 123456& ?8 e' G1 V O) T- I' U
2 92626 oranges 97654& D% I6 d/ E: U6 C% L# g- e
3 25414 pears apple 987654 9 u6 L. O K o2 v6 Q 3 x1 ~# N' `- b没有格式库的情况下读入数据: # o, Y; L3 m, ^% yread a SAS data file when I don't have its format library/ ~! N' P9 |' I n
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. - x, W! |: E% Y; P4 p# ~ " d0 L& j7 K% s9 o8 [ERROR: The format $MAKEF was not found or could not be loaded. 0 U3 F; R5 j6 T; Z$ M! |ERROR: The format FORGNF was not found or could not be loaded.2 Z5 @7 s5 @# M1 H+ `# ?: `
Without 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. , O4 l. M4 E+ Z: | n1 A5 b: }9 ?) n. b% a! J. e+ w
OPTIONS nofmterr;* @, e' f% J' \: V% W
libname in "c:\";+ z. ]" g+ c# }) S
2 A$ @' l4 |+ Z* H& i4 [! x( \PROC FREQ DATA=in.auto; * I# {& k# C0 i+ @0 a: |9 q TABLES foreign make; $ T8 Z2 \; r2 E( ORUN;2 C, F( H3 S7 _! U* K
高效的保留或者去掉部分变量的方式:& ^7 y4 f& y7 @* {
The following program creates exactly the same file, but is a more efficient program because SAS only reads the desired variables.# t, B8 m; {4 S& Y4 G, n% |$ R) e# d1 W
$ X2 H8 r" H; zDATA auto2;' \5 Z* \' W: W6 J$ T g0 z
SET auto (KEEP = make mpg price);0 O; l' ]9 k7 \" Z$ u5 @7 ?
RUN;' t5 K; U' R6 {
The drop data step option works in a similar way. 9 a! v5 C6 Y% T9 o7 F( K# v7 T, R2 O, L* s
DATA AUTO2;$ ]; Q3 z% Z: p
SET auto (DROP = rep78 hdroom trunk weight length 0 ?/ M. t& x; i5 U3 |2 [5 g turn displ gratio foreign); 2 D5 z+ o; R) w' l; | D0 y$ @RUN;3 d0 O3 V% g+ \5 F. t6 X" \
- Y, E0 e+ q% x+ M
0 ?& J' n2 @( E) {3 P比较双份录入的差异是否存在:0 O5 A8 r+ L* J
proc compare base = person1 compare = person2 brief;0 p1 x$ s" y- V2 z3 |# M$ P
by id;$ F" X1 j: z) W: I' w
id id; ! F# g' P$ l; `1 h5 b% R5 yrun;) u8 `! Z+ B2 F" a( O& Z. P9 V5 G