QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3445|回复: 1
打印 上一主题 下一主题

提问:请问那本书有关于数据导入的详细介绍

[复制链接]
字体大小: 正常 放大
jinsen78        

5

主题

4

听众

180

积分

  • TA的每日心情
    开心
    2014-8-21 16:52
  • 签到天数: 48 天

    [LV.5]常住居民I

    自我介绍
    我叫森哥

    群组数学建模培训课堂1

    群组数学建模

    群组数学建摸协会

    群组MCM优秀论文解析专题

    群组SAS学习圈

    跳转到指定楼层
    1#
    发表于 2012-5-1 02:07 |只看该作者 |正序浏览
    |招呼Ta 关注Ta
    新手不怎么懂导入数据,有哪位高手赐教一下?
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信

    857

    主题

    21

    听众

    1598

    积分

  • TA的每日心情
    开心
    2012-7-27 21:03
  • 签到天数: 99 天

    [LV.6]常住居民II

    群组第二届数模基础实训

    群组Matlab讨论组

    一、将数据录入SAS——DATA Step / Viewtable' {7 K/ T& e0 w
        1.Internal raw data- Datalines or Cards 命令;
    : }. X5 }1 I+ ?* O3 q8 P7 }    2.External Raw data files- Infile 命令 + Input 命令;
    5 k5 }9 e& a$ d1 @5 A2 r8 d  g3 g$ `- k. E- G8 {
    二、将数据文件读入SAS ——DATA Step / PROC IMPORT2 v' A! l" G7 h5 O1 [
       1.将SAS文件读入SAS——
    6 n& D3 m- N! }" T8 C9 M   data sasuser.saslin;
    . b- C3 H- d6 G     set "F:\sas1.sas7bdat";  I- j8 s! g5 r. e  o. N
       run;
    0 {3 _, ]; S8 M- p! F: [   proc contents data=sasuser.saslin;- x. ?3 B$ ~0 H/ P& ~. f" C, j
       run;, ~* g6 Y$ b) h
    & |, \- u3 C; _
    : C9 Z0 ?& Z1 \7 C
       2.将其他形式文件导入成SAS ——PROC IMPORT / 直接读入其他形式文件
    # P# B/ }0 h& F) f( j+ m* w/ @' Q5 a   proc import datafile = "c:\data\hsb2.sav" out= work.hsb2;
      Q: `# ~- Z( S' S) o: j: W   run;$ ]- c8 [+ S* E' x* I4 X2 k
       proc contents data=hsb2;
    ( Y& [% \3 b- i- S0 p   run;5 Z# U0 |- i3 ]/ `. |
       SAS导入数据:SAS recognizes the file type to be imported by file extension.
    . D4 ], ?3 E, |: l) z
    ' |# ?6 ?5 x5 h- v' V/ i* m- {( b  J( _' e: u+ ?8 b7 s
                          对数据长度的限制! S" O7 x# T5 G0 V! @9 O1 n/ V- j9 O
        在一些操作环境,SAS假定外部文件的纪录对最长为256(一行数据包括空格等所有字符在内的长度),如果预计读入的纪录长度超过256,可在Infile语句中使用LRECL=n 这个命令。2 T+ M( ?$ @4 `% I; U6 M
    读入以空格作为分隔符的原始数据
    ) o8 \: ]0 T; {. y. E    如果原始数据的不同变量之间是以至少一个空格作为分隔符的,那可以直接采用List方法将这些数据读入SAS。
    0 g- ?3 b! A6 F5 @9 IList Input读数据非常方便,但也有很多局限性:
    # C) t; [+ L8 q9 I* ?# [(1)       不能跳过数据;; ?% i+ b2 \' _7 l1 F6 O  U
    (2)       所有的缺失值必须以点代替
    " i+ Z* _! y/ K(3)       字符型数据必须是不包含空格的,且长度不能超过8;% V/ {+ V+ A# x' k/ T
    (4)       不能直接读入日期型等特殊类型的数据。0 \- X$ ]% k, u) ^$ P
    程序举例:
    ( Q/ z4 p+ e$ [1 @% `INPUT Name $ Age Height;
    / u3 f4 B# i  Z. w6 |0 G; A) ?9 @) Z5 Q
                          读入按列组织的数据
    ( Q9 u$ B4 _0 Y* e6 q% k有些原始数据的变量之间没有空格或其他分隔符,因此这样的文件不能以List形式对入SAS。但若不同变量值的都在每条记录的固定位置处,则可以按照Column 形式读入数据。Colunm读数据方法要求所有的数据均为字符型或者标准的数值型(数值中仅包括数字,小数点,正负号,或者是E,不包括逗号或日期型数据)。) {& c. K7 ~3 D; ]! H9 y: q# ^8 ]
    相对于List方法,Column读数据方法有如下优点:
    2 I. ]  ^# S2 T* c3 d' T(1)       变量值之间无需用空格分开;
    ' z  n- W9 \& g$ S+ x0 a0 w(2)       可以空格表示缺失值;
    8 `) h  w" T* M5 S8 e( E(3)       字符型数据中可包括空格;2 t/ |% c; d+ e  L9 L: L
    (4)       可跳过数据。
    . ]7 x9 ]3 \4 p9 ^程序举例:
    4 ^. y0 x9 L0 A: q/ MINPUT Name $ 1-10 Age 11-13 Height 14-18;, t/ ~: O+ ^- {$ l. l% |9 h/ g
    使用格式命令读入非标准格式的数据
    7 W6 F. `) Y( p字符型数据: $informat w.
    # w2 q7 V" o# V+ B1 b. \) d8 g数值型数据:   informat w.d
    8 e) m; C1 q9 }日期型数据:   Datew.
    8 h" E% q' E! k( a! D! I(1)字符型:
    5 q/ M) i" {0 I+ a9 g8 q$CHARw. :不删除前后空格,读入字符数据;+ _& T9 B: {* c' R) c, Z
    $HEXw. :将16进制的数据转化成字符数据;' w& c- D( |, p( k1 h
    $w.      :删除前面空格,读入字符数据;! S( A- \7 h+ \, n# F( t+ y
    (2)日期,时间或日期时间型数据
    & j9 S- `8 e: `6 e8 h* fDATEw.           :以ddmmmyy或ddmmmyyyy形式读入日期;) T7 Z3 k2 X5 I7 Q
    DATETIMEw. :以ddmmmyy hh:mm:ss.ss 形式读入日期时间;+ f+ m! b* L& }  K1 j8 L4 f7 Z
    DDMMYYw.     :以ddmmyy或ddmmyyyy读入日期;
    % F: f" T# a& B1 O  ^& @JULIANw.        :以yyddd或yyyyddd读入Julia日期;3 Z2 `8 {: T$ |% g7 Q
    MMDDYYw.     :以mmddyy或mmddyyyy形式读入日期;- c9 A) x- L3 o6 V' g; r7 N/ u1 ?( ]
    TIMEw.             :以hh:mm:ss.ss形式读入时间;
    5 Z/ Q6 W$ |" k3 Y% t* I, U(3)数值型数据7 [6 p+ _- B- b
    COMMAw.d       :读入数值型数据,将其中的逗号,$ 删除,并将括号转化为负号( v/ _6 z1 ]: B
    HEXw.                :将16进制数据转化成浮点型数据
    5 J9 S4 I) ]0 V% {) l" \IBw.d                  :读入整数二进制数据;6 X: x  D' H6 k% w7 R8 g8 Q
    PERCENTw.     :将百分数转化为普通数据;
    ' ]3 L9 H7 q0 f4 s9 mw.d                      :读入标准的数值型数据。
    ) W, s% D" x/ ?7 F6 `, ]1 q! S3 B3 C% C) ]3 R9 N
    INPUT Name $16. Age 3. +1 Type $1. +1 Date MMDDYY10.
    . L9 v; ~6 w* T( K8 }8 N& N         (Score1 Score2 Score3 Score4 Score5) (4.1);
    . q" f- X  q0 B                         多种输入格式综合
    0 \  U  d2 e' X& I" A: d; G读入位置控制——列指针
    " ~. v8 P8 \8 U; R- y+n –n :控制列指针从当前位置向前或向后移动n个字符;% R7 R7 E  W, g& ^6 C
    @n   :控制列指针指向
    / H; }; N  S! F9 V0 g举例:3 x3 `# _& M. a0 T( C! o* W5 {
    INPUT ParkName $ 1-22 State $ Year @40 Acreage COMMA9.;# A8 a% ^, }$ g2 E) [

    ' Y; W& i, ~; M" T8 t/ g                         读入杂乱数据
    * k3 y) ^/ |: N( `在不确定从哪一列开始读入数据,但知道读入的数据均位于某一特定字符或字符串之后时,可采用@’character’列指针。
    1 S, |: S0 v7 Q0 |( D( j如:有字符串如下,需读入Breed:后面的字符串
    0 i+ h2 n- `9 }) i+ qMy dog Sam Breed: Rottweiler Vet Bills: $478. Y8 c- q  k& m' c# b1 `

    ; |' M5 Z; O/ y& q! d(1)SAS 语句:Input @’Breed: ’ DogBreed $;     读入内容: Rottweil
    # ~2 A" e9 V+ a% {! M7 J6 {9 L5 K; w读入Breed:后面的字符串,赋给DogBreed,读入时到空格时,自动结束。4 ^9 N) b/ Z0 h* Z! s2 Y, e4 l
    (2)SAS 语句:Input @’Breed:’ DogBreed $20.;   读入内容:Rottweiler Vet Bill$ U0 F; y: k% {. _: g2 T
    读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20。4 H- J" B+ W$ }, |$ {/ ]
    (3)SAS语句:Input @’Breed:’ DogBreed 20.;   读入内容:Rottweiler4 M) e3 X8 V: j; {' H- [  C. U
          读入Breed: 后面的字符串,赋给DogBreed,读入字符串的长度为20,但遇到空格时不再继续读数据。- u  Y: k5 m9 }' r: c  s( T& s
    从原始数据中读入多行数据作为SAS的一条观测% N* R3 ]9 F$ c  c% \. N8 \
    使用行指针:* [5 b  [4 ?8 t- v* L6 t
    ‘ / ’—— 到下一行读数据
    . y$ e3 u4 |; b0 i$ M  D‘#n ’——到第n 行读数据
    # n9 A9 F- Y: G. C. K# n; H1 r4 `- YINPUT City $ State $ / NormalHigh NormalLow #3 RecordHigh RecordLow;
    0 b  Z( e2 T3 O9 c/ n' q从一行原始数据中读入多个观测5 [1 Y: P% P6 R1 ]
    在Input语句末尾使用@@标示,告诉SAS继续读入本行后面的数据。3 i! T. Z: e2 H& V4 y. l
    INPUT City $ State $ NormalRain MeanDaysRain @@;$ S; C3 r) _+ O: l5 h2 ]
    有选择的读入原始数据- k$ h# R2 ^  V; b( T5 V9 n) D( A/ `+ Y
    SAS让用户无需读入所有的原始数据,然后再判断是否是用户所需要的数据。用户仅需先读入足够的变量,以判断该条观测是否为自己所需,然后在INPUT语句后以@结尾,以使SAS读数据的指针停在此处,保留此行数据。然后用户使用IF语句判断读入的观测是否为所需数据,若是,则使用第二个INPUT语句继续读入其余数据。/ B2 J7 c; ~+ ?1 z! c
    INPUT Type $ @;# C. n* h" t" C, ?: {
       IF Type = ’surface’ THEN DELETE;$ u( T" I/ v3 v1 F9 R& @% G; Z
       INPUT Name $ 9-38 AMTraffic PMTraffic;7 t+ W9 g% ?9 J
    @ & @@
    2 {8 a; n' @* I% x; g  V5 t(1)    均为锁定数据行的标示;! j# e* ?) A, w3 |+ D# a
    (2)    @标示在SAS进入下个循环之前就释放锁定的数据行,而@@标示在继续锁定数据行
    ! W6 k3 _" ?% r% y! U& h在INFILE语句中控制输入的选项: |! P. @. }! \6 \6 o5 j% B
    (1)FIRSTOBS=n : 从n条观测开始读入数据
    ! y, w6 I8 J0 A3 F% M/ M  B- K" t. c(2)OBS=n 读入n条观测
    : }+ G* q1 ?" v" z8 ~$ T- v(3)当读进内存的观测长度小于INPUT语句设定的长度时
    4 \; t) m* u4 E: n/ v当SAS指针到达一条记录的末尾,而在INPUT语句中尚有未读入的变量时,SAS默认继续读入下一行数据。2 p& a7 A& z) r) f
    MISSOVER:不读入下一行数据,而将未赋值的变量以缺失值填充。
    & P. j4 o& i0 K" z+ P; J( FTRUNCOVER:当使用column或格式化读入方式时,某些数据行长度小于其他数据行长度时,使用TRUNCOVER选项,可防止SAS读入下一行数据。
    * H5 c1 ]7 C3 Z使用DATA步读入分隔符文件
    " K5 u* z0 y' l! p" X0 s" v3 W在INFILE语句中使用DLM= 选项或者DSD选项可以读入以特定符号作为分隔符的原始文件。. f% V8 f6 @0 M2 r
    (1)The DLM= option (i.e. DLM=’&’)3 q' _& C: \% n( S7 r8 K
    如果是以Tab作为分隔符,则使用DLM=’09’X命令& n& {# s: C1 y8 H
    (2)The DSD option:主要有三个功能
    # q3 G7 q" j' w+ P忽略单引号内的分隔符;. k8 x+ P( M0 S6 ]  i
    不将引号作为数据读入SAS;5 p. b  r8 w! g) l/ x1 N5 ^% z
    将一行内连续两个单引号作为一个缺失值处理。
    5 h$ L* K( z& X使用IMPORT程序步读入分隔符文件
    # ~; s3 U2 [5 C* k+ G% _5 UIMPORT 程序的功能& e  d) l# F1 @1 B4 F0 R) z' y
    (1)       自动扫描数据文件,并确定变量的类型(数值型或字符型);+ H9 W# o8 V" X" p) ^% ?
    (2)       为字符型变量,自动设定变量的长度;
    & l" e; h9 T/ {, W! y" B0 x  R(3)       识别一些日期型数据;
    8 \- b. ~( i- w0 }1 A, O; J5 i(4)       将两个连续的分隔符作为一个缺失值读入SAS
    % p4 ~3 ]: l2 h* b(5)       读入引号内数据
    . `) p7 x& D) v9 a! H4 O(6)       自动将原始数据中不存在的变量赋缺失值;5 x; h, ?* t' R
    PROC IMPORT DATAFILE=’filename’ OUT=data-set;
      C" U1 J. O0 x' kSAS根据读入文件的扩展名确定文件的类型。若读入文件没有正确的扩展名,或者是DLM文件,用户必须在IMPORT程序步中使用DBMS=option 选项。当读入数据集的名称已经存在于SAS库中,可用REPLACE选项将原数据覆盖。
    1 g% a9 d/ u  o9 U3 YPROC IMPORT DATAFILE=’filename’ OUT=data-set DBMS=identifier REPLACE;( b$ P+ [: I7 B9 n' V
    在默认情况下,IMPORT程序步将第一行数据作为变量的名称。若第一行数据并非变量名,可在IMPORT语句后使用GETNAMES=NO语句。
    6 q' L$ Y* D& n2 U1 u8 y若IMPORT程序读入的是分隔符文件,默认分隔符为空格。若不是,则需使用DILIMITER=statement语句指定分隔符。: o& ~- d3 y: m9 h
    PROC IMPORT DATAFILE=’filename’ OUT=data-set
    ) N7 E( a* \  K6 O' b! P                            DBMS=DLM REPLACE;5 O, Q6 f1 P# Z9 G/ E9 s
           GETNAMES=NO;5 O5 E1 o! `% y5 V6 K# v% T
           DELIMITER=’delimiter-character’;
    ! n: [) a6 f0 SRUN;
    # W6 _/ ^" r2 `2 J4 r& C% B1 p; F9 C使用IMPORT程序步读入PC文件1 b( V, s7 \/ I1 R
    PROC IMPORT DATAFILE=’filename’ OUT=data-set7 o. T; Y2 Y( f: E; Y+ E4 ?3 h' ~! l
           DBMS=identifier REPLACE;
      Y$ X5 o+ w: W7 L% g列示SAS数据集的内容/ ^) j& W( i+ N8 L8 o  |
    PROC CONTENTS DATA=data-set;: w5 v# Q1 h  e' B3 o5 Q' w/ h
    CONTENTS程序步的功能是显示SAS对数据集的具体描述,主要内容有:
    * X3 f$ n! J% F3 M4 q' E. C* `(1)       数据集描述
    : K" d9 M7 P6 |               数据集的名称;# D9 P: ~& t7 x; p; \
                   观测的数量;
    6 B/ x8 C- P$ \1 m  `: q0 p2 u               变量的数量;
    5 h% |: F. Y7 M6 q7 ^  h6 e2 ]6 ?               创建日期8 G4 U1 ~! s9 t* v
    (2)       变量描述
    / K6 t- ?9 t. j: I- K! R6 N               变量类型;: z( l/ y# ^* ^2 \( s
                   变量长度;
    : C& P# @- {$ C3 w% M: Z               变量的输出格式;# z, V4 ]! w' f- Q
                   变更的输入格式;
    + ~: [* W. {! o5 Z( L              变量标识。% ]7 I0 y, Z/ M1 e

    / \* _  q( ^  c% |$ |- v实例:3 G3 K3 T8 G1 l& q8 P: h
    1.读入逗号分隔数据:cars_novname.csv
    ; T3 j. E- C4 y+ s7 ~9 F2 Q$ x/ p/ v/ N. }/ |/ s
    Acura,MDX,SUV,Asia,All,"$36,945 ","$33,337 ",3.5,6,265,17,23,4451,106,1890 {2 i6 @7 p, N
    Acura,RSX Type S 2dr,Sedan,Asia,Front,"$23,820 ","$21,761 ",2,4,200,24,31,2778,101,172: {) u9 @% W9 X' ^7 G
    Acura,TSX 4dr,Sedan,Asia,Front,"$26,990 ","$24,647 ",2.4,4,200,22,29,3230,105,183
    % q1 d1 a9 x( }* f! nAcura,TL 4dr,Sedan,Asia,Front,"$33,195 ","$30,299 ",3.2,6,270,20,28,3575,108,186
    9 [  o3 z1 l. t1 v0 KAcura,3.5 RL 4dr,Sedan,Asia,Front,"$43,755 ","$39,014 ",3.5,6,225,18,24,3880,115,197
    5 K& F: X; P  j5 g- {
    3 v' u' P. X( P( j  Kproc import datafile="cars_novname.csv" out=mydata dbms=csv replace;
    4 e* b+ q  M2 J0 `+ n' D& D    getnames=no;
    2 E3 U5 u9 U4 D6 e* O0 o0 g; W! ]- M! |run;) M+ I& a, T0 U/ M* ]
    . ~: l. f, F# p# h
    proc contents data=mydata;* P4 k) h$ v$ a" \
    run;
    " s5 S/ i( g% f/ q9 j3 p" U0 L
    . E, L1 \# u7 D4 v( S4 a7 a$ aSAS creates default variable names as VAR1-VARn when variables names are not present in the raw data file.2 \4 s) B1 x* C  i4 T5 c1 r
    $ t* h. b7 D0 B- K9 S6 X
    2.读入制表键分隔的数据:8 Z4 u: g) o% ?6 w
    proc import datafile="cars.txt" out=mydata dbms=tab replace;7 l! E% }' P; K. u3 R
       getnames=no;6 M+ s4 M9 A7 S2 s5 J9 r
    run;
    5 G9 Q/ g3 ?: B; z5 }! J. s3.根据不同任务将不同的数据集永久保存到对应任务的文件夹下:
    : ^, A; o; c( l8 p: |. vlibname dis "c:\dissertation";% z- A' p5 Q4 J+ T  @. ?5 |$ z
    proc import datafile="cars.txt" out=dis.mydata dbms=dlm replace;
    5 g8 T7 w, o) l, y  t3 z. d   delimiter='09'x;
    ; v5 s' c/ P4 x" p   getnames=yes;% O( l" \$ N. s7 V2 }  Y6 `; ]
    run;
    # s& I- X$ }/ B) h& @3.读入空格键分隔的数据:
    : @' d9 e2 K' P3 W( w. r1 Vproc import datafile="cars_sp.txt" out=mydata dbms=dlm replace;
    , B, A4 A/ W( E9 ~$ g  Cgetnames=no;
    0 r' d* p9 }% E+ M: Zrun;
    ! j8 S6 W; N* z" v0 @! y" `4.分隔符的终极例子:. |3 P% W9 h- T/ y* j9 {) O9 T* v3 }
    Other kinds of delimiters" v' o4 G) v* h, J- L2 K
    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.! g) P2 c7 |! @8 U

    / e0 x# m5 X& _; z. B; y2 M- b22!2930!4099
    0 n: R( ^7 W9 Z$ g17!3350!4749
    6 [: x) j. k0 s$ K( b' q  X  k22!2640!37997 r  Z( Q7 W5 o( Z  @! f
    20!3250!4816  O; ^& ~: F; Z/ x
    15!4080!7827
    6 K8 U" }, I( J* `2 ^( pThe example below shows how to read this file by using delimiter='!' on the infile statement.4 h7 X/ S1 h; e+ y: p" Y) _
    0 |/ R) J3 @2 _& ?: W9 e0 D
    DATA cars;
    4 y0 o* O& Y2 K  A) l9 ^1 WINFILE 'readdel1.txt' DELIMITER='!' ;* V4 w- k' \% x& q+ Y
    INPUT mpg weight price;0 ^0 d; {& v# q7 o1 [! @% [) p
    RUN;( E& D, {6 D- q( a$ ~7 {

    5 E. {% R, t+ M  v; J' CPROC PRINT DATA=cars;& q* b. ~- `( F% a  o# d
    RUN;& f- d3 L. C; e) }. c8 G& b5 k
    As you can see in the output below, the data was read properly./ s! i) {! b$ f* {: H
    ; ^. G0 D4 G9 V% q9 K" D- S5 z
    OBS    MPG    WEIGHT    PRICE
    ! {' M9 L$ b7 T. z
    3 {1 o2 B2 s: d+ H2 O" Y+ c1      22     2930      4099
    ) ?& h8 z7 F9 I- T2      17     3350      4749
    # t6 B! P) K7 {+ B/ f3      22     2640      3799
    " q9 o. ?, w( e/ d; m4      20     3250      4816
    & K5 a$ v6 Z0 j8 [5 h9 W5      15     4080      7827
    2 o: B& E4 g8 e) o/ YIt is possible to use multiple delimiters. The example file below uses either exclamation points or plus signs as delimiters.
    3 Y  e- H6 W( J$ m5 s- e( v  Q  J# p8 @. X* w) f: l# ~
    22!2930!4099
    1 y$ U6 i: G& ]: ~3 P$ j( |, o1 `17+3350+4749
    7 Z! J  B$ k* @% k/ V1 p: W( l: I22!2640!3799
    , s5 R( E2 {0 L  @/ C+ `0 K. Q20+3250+4816
    + G& }8 q( Y& M& {2 _15+4080!7827
    - H, I; K4 S9 ]8 l- w; d9 sBy using delimiter='!+' on the infile statement, SAS will recognize both of these as valid delimiters.
    ( A1 O) x5 n) O+ \) W! J4 k2 H4 d- t) i) z) w. ]2 `+ ?, Q
    DATA cars;! Z, q& C+ L$ V8 i, J4 D+ t
    INFILE 'readdel2.txt' DELIMITER='!+' ;( p2 R& S  J7 c( O6 [# K1 {0 F
    INPUT mpg weight price;! v4 _% b. v5 L
    RUN;
    2 R# m& F. W- R2 R+ r4 P' u2 r! S$ t5 k9 K  j2 R+ P5 |. ]2 i
    PROC PRINT DATA=cars;7 C( h4 w7 P" }
    RUN;
    4 [& k8 j% a$ j* JAs you can see in the output below, the data was read properly.3 j. e/ }. b0 o- J3 D2 T% _

    : r2 C; b( f2 F4 k. dOBS    MPG    WEIGHT    PRICE
    # E' ~; M/ a5 s# d  u# z
    ) v, S0 g1 p; n4 s2 ]) ^1      22     2930      4099
    3 b% t0 t* a% k$ \. J3 m  m4 h" j2      17     3350      4749
      [$ h4 M/ W' \$ R7 C2 P3      22     2640      37990 q; K1 ~- G! x# h3 t! r, Y3 r
    4      20     3250      4816  i6 G7 Q7 Q2 }% h( M0 K% G
    5      15     4080      7827
      P9 s  ?3 J; R: V8 F3 E  Q: s6 x; `: V: I
    ! [3 V5 E, v' _* r
    import缺陷及注意事项:# ^% @+ m$ g! C0 V3 J0 j
    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.5 d0 Z: ]5 i6 k& x

    " Q1 z  k% P4 \# ~5 g# a
    + [) v- y: u4 Y# V4 z, b重点语法-Infile options/ q' t* p( K, N
    For more complicated file layouts, refer to the infile options described below.* G* _, I, d" x: n3 z* y
    5 j* V9 b* g$ o6 J' }( U. b
    DLM=2 S+ Z5 R4 |) A0 V
    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).- z, l4 f  @: I! D! e2 ?% o* Q

    + L: q, d+ X. z, W* m9 J" HDSD
    # ^) Y/ f7 }# M) v+ v- B9 d( v/ RThe 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.
    + q, X8 z3 L* j3 f4 s+ E7 i  U0 w' k% a9 ^
    , @5 v, J. ?1 {0 c+ R0 q  K
    FIRSTOBS=' N' i( h& U. y: M9 G
    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).. M4 d( s  o7 G- J* [
    * B, |6 u$ l. n

    6 f6 g) j2 m  Z% HMISSOVER' o/ d; r0 {( Q& j
    This 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' R. r6 ?$ U5 x: I  f0 U
    , R9 {1 Y5 t: sOBS=4 `# B6 |$ a2 Z+ O! {9 `2 k1 j
    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.( L# v9 ?9 j4 H/ F$ K
    5 c+ f- D& v% L8 Y0 m! `
    A typical infile statement for reading a comma delimited file that contains the variable names in the first line of data would be:
    6 ]8 }5 s1 Y: l2 Y; y8 Y: [8 u$ ?& e- E& }; e6 w4 x
    INFILE "test.txt" DLM=',' DSD MISSOVER FIRSTOBS=2 ;
    * t. V2 E6 I- I- D8 u8 q, r( w
    / L/ ^! K  \! @. g' @( }+ M0 G" y' D) L  h. R" f
    读入有缺失值的数据或者读入数值中含有分隔符的数据! Q2 X! e, G+ p
    DATA cars2;# E* T( ]6 C- X9 T9 u; r. j) g* z
    length make $ 20 ;
    , _$ ~. k- W4 l* eINFILE 'readdsd.txt' DELIMITER=',' DSD ;# p6 z1 b% `; Q1 }
    INPUT make mpg weight price;
    , G; }3 }* V$ ^0 LRUN;
    : R1 j, T. Q7 i6 T- m) o) F$ G
    7 W( n. U; u# N! ]& ?PROC PRINT DATA=cars2;' `% [$ D3 H1 J6 N5 P0 }/ b
    RUN;, U8 |7 D% X# n; ^

    & y1 n: p  u+ E! @5 _
    ' R: Z* r+ X% F$ f, M3 q7 K* {48,'Bill Clinton',210! L/ r) i1 P, c8 v0 @* ]. ]
    50,'George Bush, Jr.',180: c6 z, q2 d' i# A& p% i/ u
    DATA guys2;' D5 q2 V5 C; B( Z; ~
    length name $ 20 ;7 |3 A7 `: H" h) f* D
    INFILE 'readdsd2.txt' DELIMITER=',' DSD ;
    # |4 ]! J0 e8 q1 P$ S) T1 KINPUT age name weight ;* U4 {* w$ Y) Q6 b3 O. d, M
    RUN;
    ; b; K4 b: J$ V& K9 c
    & L# ^! y7 {- f, M$ \1 A$ V/ X' G& A8 I! P( ^5 y! }' [
    PROC PRINT DATA=guys2;
    # U- C* A7 x4 ^/ h0 E" p5 dRUN;# b9 U% f# A9 V3 A
    ' X6 ~/ i& n6 C% q- y! q  c; {, Z1 b
    最经典例子:从某行开始读入数据
    0 U1 K: {, B4 p0 mDATA cars2;2 U7 V2 c* s. J: c- G9 @
    length nf 8;5 D; _) x$ E' v6 ?: y+ t
    INFILE 'F:\cars1.csv' DELIMITER=',' dsd MISSOVER firstobs=2 ; /* obs=20; would read just the first 20 observations from your file.   */* V+ P) {- o. o2 t: h
    INPUT nf zh hh xb cs IHA fj;
    2 N- U5 m- B6 |/ z9 n( p; gRUN;
    $ O0 i; @/ d7 b0 b
    9 C+ v* G  H6 x5 }7 xPROC PRINT DATA=cars2;) [- V: E4 d( S
    RUN;
    . e+ H( j  G! ^3 r, m+ J+ I8 A8 `: n9 ~+ E# n
    从FTP读入数据+ O: n/ G) J; Y
    read raw data via FTP in SAS?1 E# S6 ?/ T' Q1 s* E1 s
    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).; w# ]+ D! e3 F- K, I# u3 L
    * q5 W9 K$ t& {3 _, W7 o+ J
    0 Q  C# A4 G; O0 h. h
    FILENAME in FTP 'gpa.txt' LRECL=80. Q6 I1 N  R) Q3 |/ l; Q! z' V" P
                    CD='/local2/samples/sas/ats/'. {; \/ O6 O9 J
                    HOST='cluster.oac.ucla.edu'
    7 \! q0 P9 a( i$ T                USER='joebruin'# z4 ^8 j' U* z
                    PASS='yourpassword' ;
    : @3 e0 Q$ G# ~  PDATA gpa ;
    6 a7 ]: _7 g2 s6 J/ d7 b) O& m   INFILE in ;" O# j( B4 O' x0 o
       INPUT gpa hsm hss hse satm satv gender ;
    % q9 ?. Z' z, tRUN;
    8 D2 X7 Y2 A- r' t% j( h% I/ |) f
    6 X6 i, x$ j: i* n6 K; p3 M' RPROC PRINT DATA=gpa(obs=10) ;
    , [# R6 M+ E5 ?  {RUN;
    ( @8 j" _% F; v0 f' t: U0 S/ G$ k+ @. T
    读入多个数据文件3 i# d! d0 L7 B& c( {' U

    ; Z1 l& v1 V* I+ z; I4 _8 wquarter1.dat
    9 w% }# G$ I0 P7 n. U+ F' W' b  b. d! H
    1 120321 1236 154669 211326! t) m* _# n" f4 A+ I
    1 326264 1326 163354 3126654 Q$ I  b1 v0 {5 L8 v  V
    1 420698 1327 142336 422685
    " P1 ?8 \4 u+ {8 g1 Y# @- F1 211368 1236 156327 655237( z) C' R+ j7 v% W* \# \
    1 378596 1429 145678 366578
    / ^2 p# V* p6 Q: equarter2.dat
    ! ~: C/ W4 y& E+ z3 D$ Z' B) t( w3 |. V0 S3 V7 K2 ^7 |
    2 140362 1436 114641 362415
    # M$ i% h+ c: w) c' l' j* s$ S2 157956 1327 124869 345215  [6 p  j! s! Y
    2 215547 1472 165578 412567
    $ U8 b+ d& H) Z& x2 204782 1495 150479 364474
    7 r9 p3 m9 V" k0 [2 232571 1345 135467 332567
    * [2 I, x! @) o3 G/ \quarter3.dat) q, k  H% [! L# A5 O. d( J# w
    2 J: Q" j. q( b# ^7 ]2 N' c
    3 140357 1339 142693 205881$ m( Y; o$ k) x  K/ L$ f( d
    3 149964 1420 152367 223795
    7 H" R) q; ]  s% [( D9 ^* E3 159852 1479 160001 2548748 h6 V. ^1 h6 p: k9 W
    3 139957 1527 163567 263088* p/ m- x" Z- G& f( S$ t9 ]( R
    3 150047 1602 175561 277552+ z( `! m3 ?8 v  p3 c
    quarter4.dat* R3 h5 u+ ?! V. a  h! z! `

    ' Q% b% l% e" M4 479574 1367 155997 36134" f/ Z1 `) }0 E: ^! @# H
    4 496207 1459 140396 35941
    ! m. e5 d$ A/ e% G2 X% ^4 501156 1598 135489 39640
    . f& s/ o7 Z. ?) q/ O% ]4 532982 1601 143269 38695
    ( c1 L4 J4 w2 X2 ~! t* m% ?4 563222 1625 147889 39556  C1 f' \  l& |" W+ ^  `- W  ^1 c, Q
    filename year ('d:\quarter1.dat' 'd:\quarter2.dat' 'd:\quarter3.dat' 'd:\quarter4.dat');
    % h" U- P* e1 s; I7 h' _8 B: q6 j& ~data temp;3 y4 z# F# ~* A
    infile year;
    ) W4 B" i. q; Q  p; E8 i$ D6 ^input quarter sales tax expenses payroll;  c& t. d* o( d& P# Z! G4 a
    run;
    9 \' Z- p7 P2 r' r5 j4 O3 @7 {+ ?proc print data = temp;
    6 Z" t! x' p* W0 ?  a" k& O7 E1 Q! |run;4 a. V+ J8 V& @/ p! u6 \/ Q: n/ x
    # Y: B" T: `$ f; W* b$ X9 E2 H
    读取excel 数据集6 i" ~/ o" w! H5 k8 k
    - ~* i  B' b3 b4 ^# m
    Reading an Excel file into SAS4 X( ~( Z; `" W2 a, e
    Suppose that you have an Excel spreadsheet called auto.xls. The data for this spreadsheet are shown below.- F$ t$ c& x- I  y1 v+ T

    + k; k& `- r: s1 H/ ?; c" JMAKE           MPG WEIGHT PRICE
    # B: m2 D! g* ^1 V. e7 CAMC Concord    22   2930 4099& L2 u! L3 G& j2 [
    AMC Pacer      17   3350 47498 ~% p  z) ~* N# ?7 u$ A
    AMC Spirit     22   2640 37995 b5 G1 U- g* v  W
    Buick Century 20   3250 4816  h( I" {( M. M1 E. N* L' [+ l
    Buick Electra 15   4080 7827
    1 w6 `% p2 w. u5 T0 kUsing 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.- j1 i) z3 \, A3 R* ?  j1 X

    - Y) v* P3 O% `/ d3 a6 {PROC IMPORT OUT= WORK.auto1
    3 h4 f5 Y& q8 ?            DATAFILE= "C:\auto.xls"
    % w  U( \! x% Y" S2 S5 M; t0 c            DBMS=EXCEL REPLACE;, R3 J  B% ?- ^1 M2 S" l  a: ?  m
         SHEET="auto1";- x- A  L4 i9 o, a! _& g/ g7 a
         GETNAMES=YES;
    " r1 g- E2 w- {+ x3 ?. M     MIXED=YES;
    3 P8 D+ D5 g6 T- Z# `1 k     USEDATE=YES;
    + t1 X( q' o7 s, \     SCANTIME=YES;: z; O% j2 |% @3 w. ]; c
    RUN;
    - I& r5 |7 s* v8 ~. y4 g# j" Rproc print data=auto1;/ V/ j. A) c; v
    run;8 c5 C( a; v' [  B! ~. F; _

    2 u' R5 `$ {; u3 \7 H% n  W) aObs    MAKE             MPG    WEIGHT    PRICE  T2 I. p, j# M- Q: V* v
    8 A8 D" ^" c% U/ `+ U* \9 C
    1    AMC Concord       22     2930      4099
    / {  n2 M1 [& A' @2    AMC Pacer         17     3350      4749
    " \6 c  x! [; Q+ c$ S5 ?3    Amc Spirit        22     2640      3799
    - d* n) w0 H' L# q1 Q' d4    Buick Century     20     3250      4816) \; R! G( M5 H3 A$ _
    5    Buick Electra     15     4080      7827
      N1 J, M3 G: T' `First we use the out= statement to tell SAS where to store the data once they are imported.
    8 ?) V; W8 ]% l/ }4 o; vNext the datafile= statement tells SAS where to find the file we want to import.
    0 G. F% l7 E" R2 d. A6 ?7 ZThe 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.8 `6 W, O7 f9 b# Q/ g+ M; k5 K( H9 }
    The replace statement will overwrite an existing file.
    5 m- U5 f$ o* u1 h6 B1 [% [: sTo 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.
    ' V2 L0 t$ H* q# Q- U8 A, VThe 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.
    8 l: i) {% h6 d7 Y" e+ TSAS 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.
    2 k! r( D8 m  A, x' t, r- w+ pConveniently 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.
    2 ?( i  {6 h  \  oExample 1: Making a permanent data file1 y. m- S4 Y6 {9 c& |' ]/ f0 t3 B
    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.
    ' @* Y5 k& w- [2 d2 g' P
    ' t, d7 i! T- |6 |) {# tlibname dis "c:\dissertation";8 S7 |+ C0 v1 ~# H( w
    proc import datafile="d:\temp\auto.xls" out=dis.myauto replace;
    + n# y4 G, j" T' ]run;! z' N' [& y7 y2 u* z+ I1 Y5 ]  j
    Example 2: Reading in a specific sheet
    3 N' L$ S0 ]( ]9 {) ^# JSometimes 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.: x" G  [6 _; `2 ^

    ! d8 n6 `, l  ]0 [/ hproc import datafile="auto2.xls" out=auto1 replace;* G0 N) O' s; q' H* d6 _
    sheet="page2";
    - o- P1 d* @- [0 F4 s4 rrun;
    * s! x% G9 v( @) F9 zExample 3: Reading a file without variable names
    4 S" o" x+ n7 c9 r4 D; }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 L  Z' P/ \# f2 ~3 `" u/ w! ~, ^! z8 o
    proc import datafile="a:\faq\auto.xls" out=auto replace;
    , a4 o+ e- ~8 h- Y0 [# S( b5 Agetnames=no;1 @& W7 a1 b  F5 Z! T
    run;
    % z3 @$ p: w& mWriting Excel files out from SAS
    ) O/ e( C" x& J' g6 L2 V9 t2 `: SIt is very easy to write out an Excel file using proc export in SAS version 8. Consider the following sample data file below.. R9 \! v4 s( V/ d
    8 s, n9 `' l0 u8 c& ^8 z
    Obs    MAKE               MPG          WEIGHT           PRICE, E; A$ P- [! V* y, k* H5 }
    1     AMC                 22            2930            4099! k- p1 _# V( V
    2     AMC                 17            3350            47495 b* U8 U, [; d7 e( R  f+ z
    3     AMC                 22            2640            3799$ [% b  N4 p8 h" N: z1 p
    4     Buick               20            3250            4816
    ; {. c- C% c4 ], _, v2 [* D; ^4 ~5     Buick               15            4080            7827, H- H: f8 t- i8 v! D2 c4 T: b3 ^
    Here is a sample program that writes out an Excel file called mydata.xls into the directory "c:\dissertation".
    # q) l- `  ^5 v, j/ w
    ' p& v# o4 J* N" Qproc export data=mydata outfile='c:\dissertation\mydata.xls' replace;
    $ W( O1 Q% [8 {" y- A  Erun;! H' W2 F5 f4 h
    0 U! c7 K7 }4 Y# t/ J) L, D. x" l
    SAS读入复杂分隔数据——字符长度不同,字符中间有空格作为间隔符4 G; N4 p* `  |" r* @
    1.字符长度不同
      C9 ]( E2 o0 {data web;+ M; V: a) c$ g* b7 w8 @* R
    length site $41;
    & v" _. q' t8 q$ z5 ~input age site $ hits;6 y* d( M: C! D1 D
    datalines;. _. F6 G: D: j( K  f5 g) `' g, W
    12 http://www.site1.org/default.htm 1234565 P/ o6 J  ]3 D2 W. b& W
    130 http://www.site2.com/index.htm 97654! m" c; b3 A, O9 ~+ t  b- _* Z
    254 http://www.site3.edu/department/index.htm 987654
    / s5 V3 L. r7 P9 d) j;
    $ m6 I  g' T# R4 C$ l2 z# y6 G* ^proc print;, y7 f" J6 M: k2 {; ~5 T% v
    run;: ?9 c; m% C( X$ A7 g

    " ]  n8 T$ h9 C8 r4 G' W$ N; RObs                      site                       age     hits
    5 W0 N* E5 \. b  \; m
    ! `  E& x9 q# e4 M# ^. g1     http://www.site1.org/default.htm              12    123456
    : n: X0 k; P; |/ f* {, Q2     http://www.site2.com/index.htm               130     97654. U$ J# H( i9 f3 L/ i
    3     http://www.site3.edu/department/index.htm    254    987654
    / V9 Y! v) I- ?0 E- G! {/ C+ G+ ]4 e# D" q3 A8 f
    或者用- A+ h/ h2 q) w$ Y- h; R7 A
    data web;
    - ~/ h5 Q- ]9 c& V% m' linput age site & $41. hits;" t; _/ `5 M3 o1 z
    datalines;8 m5 n9 k% X; C
    12 http://www.site1.org/default.htm   123456$ s* x8 `. p* P3 ~( q; A
    130 http://www.site2.com/index.htm 97654
    ' B2 X$ X! H: c, P5 {0 D. {$ U: i254 http://www.site3.edu/department/index.htm    987654
    + j2 n' m2 y4 P+ R! r4 c;
    , {6 D3 E( s) t+ _  [& K. |. mproc print;
    9 D9 v8 f8 a" L* X3 l# urun;
    6 T' n' a- u9 t% E; P# l+ h% z$ b% L- d4 p! j7 Q" V% q
    Obs    age                      site                        hits+ a/ o; c- R) S
    1 C/ g! u7 n/ b! ]! E
    1      12    http://www.site1.org/default.htm             123456
    ! ~/ L: J: P9 n0 G2     130    http://www.site2.com/index.htm                97654
    8 a6 ?# Q( o, L4 X) |8 c3     254    http://www.site3.edu/department/index.htm    987654
    : T5 T9 {: C1 x4 K: r! C4 E' f% g' Z
    ( Q/ \- Y$ p7 z% L3 w* p5 g, U; m
    2 |: ^5 a: I' d9 m; r% k, x2.字符有多个单词,单词之间用空格隔开
    ) W* e9 _8 R- w4 V( k; c  ndata fruit;
    3 q1 X# p3 B8 I  pinfile 'C:\messy.txt' delimiter = ' ' dsd;
    ( Q9 V$ m& M+ S: N& ulength fruit $22;# P! S% M4 L9 J& W" @- V! S
    input zip fruit $ pounds;# ]/ [7 d5 w( Z& X
    proc print;% W& o$ ~, e- ~: ~" U
    run;2 b2 @4 o1 ~# r9 v$ P0 p# Z

    + q# `/ n( k! h- H+ |Obs    fruit                   zip     pounds6 D! v) b: S0 _; n

    , H* A# m0 `$ b5 s+ t& \1     apples, grapes kiwi    10034    123456# F& P5 z$ O4 U1 V2 W
    2     oranges                92626     976549 G/ ?7 o/ }/ y/ {: }# {
    3     pears apple            25414    9876542 q3 K5 ~2 }0 z! z
    或者
    7 `& A( y; D$ q5 l1 H3 B& @data fruit;
    : J5 N9 W( W. U7 P' winput zip fruit & $22. pounds;' K2 H/ x9 m0 x0 Z7 F4 a/ Z
    datalines;
    : e/ U# d2 D4 b: i" W& c! ?/ X5 J10034 apples, grapes kiwi 123456
    " `9 [% R0 b- J/ H% m* X" T$ V92626 oranges 97654
    0 [3 K$ h$ r/ p$ Z3 I) w0 U7 ?) z25414 pears apple      987654: q3 O0 {0 v0 r
    ;0 {! r) z6 m6 L# c6 L
    proc print;: x; a3 n7 |9 |# v% S
    run;
    ! {* v6 Q+ g2 q! {- l
    & i9 {0 t# L" Y! N7 C5 EObs     zip     fruit                  pounds
      C% i+ ]' s8 }# b4 j
    ! q: L8 _, _/ k) K1     10034    apples, grapes kiwi    123456/ W' M( Y% p6 i1 |6 [8 n
    2     92626    oranges                 976542 p; `# j# O3 J3 Z
    3     25414    pears apple            9876540 d5 H2 q/ S- F; V; y$ G* J
    ( W2 [& ~9 W! k  c, b
    没有格式库的情况下读入数据:
    ) R" W% _0 N9 F: L" qread a SAS data file when I don't have its format library
    + j$ _2 w# B% |' N1 O! L- n$ LIf 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.
      m, Q9 Q$ Z* O" i+ u  R
    5 |8 t9 s! ]4 @1 H/ `ERROR: The format $MAKEF was not found or could not be loaded.
    0 c6 D9 |3 W# M# Z! v& ]' F4 E% aERROR: The format FORGNF was not found or could not be loaded.
    4 a0 G+ M1 E# I1 {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.. U% |! k* G; r2 w9 ^. h

    . A  f3 M/ D) c: ]/ r$ v2 Q( V6 kOPTIONS nofmterr;
    ( ?# t0 f. y- b4 klibname in "c:\";2 S3 U- M1 p) H; ^7 W1 `

    3 F: g! L/ |. nPROC FREQ DATA=in.auto;
    4 k/ G9 [2 I! _, C7 K: q7 x( E   TABLES foreign make;
    : n. m) M  D$ {/ D& V5 N: Y4 sRUN;
    / n( z  R, l! a* F' C高效的保留或者去掉部分变量的方式:
    2 U: `6 g* `. D2 g4 yThe following program creates exactly the same file, but is a more efficient program because SAS only reads the desired variables.
    $ a3 d2 c8 y6 @+ B2 U: \' Z2 l# z2 v
    DATA auto2;
    4 d5 F0 J  F' c   SET auto (KEEP = make mpg price);  |+ q' _! a: ~+ d
    RUN;
    $ X" ~' V: w+ U" e7 U1 Y# c6 iThe drop data step option works in a similar way.- k# i  p8 [7 H% ]4 v5 v

    1 [! }7 l& a1 fDATA AUTO2;
    2 e# S' Q4 ?* H  h   SET auto (DROP = rep78 hdroom trunk weight length9 x1 [- J) P. \
                        turn displ gratio foreign);
    1 L+ K7 Z6 P  N. j2 dRUN;
    ; [5 p5 l% G, V, D; M3 Q. C( j$ d. h' x/ j

    5 t4 I, C3 g" F/ v# [! l比较双份录入的差异是否存在:
    6 U( \& g7 n1 \proc compare base = person1 compare = person2 brief;9 _9 ~2 N4 W$ O( o7 j* y) q) D
    by id;
    % v/ M: l4 n4 F8 w7 Y( lid id;3 A5 F& V$ P* M' g5 u% `$ `
    run;' w3 {$ e& M: c3 B
    ) V/ n1 A2 H( K8 r8 V; g
    * a/ w/ j1 E4 ^2 Q
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2025-11-2 00:43 , Processed in 2.701079 second(s), 57 queries .

    回顶部