通过示例学SAS(13)--高级输入技巧

1.处理缺失值

考察如下数据

1 2 3
4 5
6 7 8
9 10 11

第二行有一个缺失值。运行如下程序,有什么结果呢

data missing;
    infile 'c:"books"learning"missing.txt';
    input x y z;
run;

Listing of MISSING
Obs x y z
1    1 2 3
2    4 5 6
3   9 10 11

更正此问题

data missing;
    infile 'c:"books"learning"missing.txt' missover;
    input x y z;
run;

结果为

Listing of MISSING
Obs x y z
1    1 2 3
2    4 5 .
3    6 7 8
4    9 10 11

2.检测读到文件末尾

data _null_;
file print;
infile 'c:"books"learning"month.txt' end=Last;
input @1 Month $3.
       @5 MonthTotal 4.;
YearTotal + MonthTotal;
if last then
   put "Total for the year is" YearTotal dollar8.;
run;

3.读入部分数据

确定obs=n作为输入选项,SAS将会读入n行数据后停止。指定firstobs=m将从第m行开始读入数据。两者结合,能够读入任何连续的数据行。

4.条件读入数据

假设有一份数据,含两年的访问数据,问题在于这两年的数据并不一致,访问的问题不一样。如何分年读入数据呢。下面是数据标本。

001ABED 2005
002AABCD2006
005AADD 2005
007BBCDE2006
009ABABA2006
010DEEB 2005

读入数据的代码

data survey;
infile 'c:"books"learning"survey56.txt' pad;
input @9 Year $4. @;
if Year = '2005' then
input @1 Number
@4 Q1
@5 Q2
@6 Q3
@7 Q4;
else if Year = '2006' then
input @1 Number
@4 Q1
@5 Q2
@6 Q2B
@7 Q3
@8 Q4;
run;

注意其中的@,表示以多行input读入一行数据。再如

data females;
infile 'c:"books"learning"bank.txt' pad;
input @14 Gender $1. @;
if Gender ne 'F' then delete;
input @1 Subj $3.
       @4 DOB mmddyy10.
       @15 Balance 7.;
run;

如果是在一行数据里有多条记录,也就是说一行并不表示一条记录。如何读入呢?

data pairs;
input X Y @@;
datalines;
1 2 3 4 5 7 8 9 11 14 13 18 21 27
30 40
;

此处@@表示一行数据读入多条记录 。

posted on 2008-08-25 16:10  zgw21cn  阅读(1040)  评论(0)    收藏  举报

导航