这里简单的列举下其他option的功能,可以根据需要同时用多个option。
下面再来看另外读一个简单的数据。
data numbers;
infile datalines ;
input testnum 5.;
datalines;
1
22
333
4444
55555
;
run;
infile datalines ;
input testnum 5.;
datalines;
1
22
333
4444
55555
;
run;
大家知道infile是常常用来读外部文件的。现有同一.txt数据文件,在硬盘中位置:F:\saschm\samplefileforB\out_data2a.txt。
1
22
333
4444
55555
22
333
4444
55555
用下面代码读取时,发现失败的。
filename outdata1 ‘F:\saschm\samplefileforB\out_data2b.txt’;
data numbers;
infile outdata1 ;
input testnum 5.;
run;
data numbers;
infile outdata1 ;
input testnum 5.;
run;
得到结果为下面,说明读入是不符合要求的。
Obs testnum
1 22
2 4444
3 55555
2 4444
3 55555
为什么会发生这种现象,首先说明一个问题SAS读入外部文件和读入放到editor里面的数据是有区别的。
【这里有比较详细的讨论:谢谢smartie,viciatwo。http://sasor.feoh.net/modules.php?name=Forums&file=viewtopic&t=659&highlight=infile&sid=9b94167994a4dfc7bc42d67f6c6cd5f8】
但是如果外部数据如下,在硬盘中位置:F:\saschm\samplefileforB\out_data2b.txt,【注意后面用空格的,每行字符大于或等于5都可以的。】
1
22
333
4444
55555
22
333
4444
55555
用下面代码,就可以符合要求的结果。
filename outdata1 ‘F:\saschm\samplefileforB\out_data2b.txt’;
data numbers;
infile outdata1 ;
input testnum 5.;
run;
data numbers;
infile outdata1 ;
input testnum 5.;
run;
在实际中数据量巨大时,对于out_data2a.txt这样的外部数据,手动添加空格的方法显然不是明智的选择。当然SAS本身也提供了一个有用的option:pad 来解决这种问题,PAD选项就是这样专门用来垫当空格的。
filename outdata1 ‘F:\saschm\samplefileforB\out_data2a.txt’;
data numbers;
infile outdata1 pad;
input testnum 5.;
run;
data numbers;
infile outdata1 pad;
input testnum 5.;
run;
当然还有一种解决方法,就是设置option:truncover。
filename outdata1 ‘F:\saschm\samplefileforB\out_data2a.txt’;
data numbers;
infile outdata1 truncover;
input testnum 5.;
run;
data numbers;
infile outdata1 truncover;
input testnum 5.;
run;
可能SAS对外部数据的行数(即回车键)没有辨别能力,导致读入内部和外部源数据时出现差异,当然这些问题SAS也会给出了相应的解决方案的,那就是设置option:truncover。关于选项truncover,下面将举例来说明。
简单的情况下,我们很愉快的用下面的代码读入完整的记录数据。
data ex;
infile datalines ;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816 3846
1 3 3819 3820
;
run;
infile datalines ;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816 3846
1 3 3819 3820
;
run;
实际情况下,记录的数据不是那么完整,或多或少的有些缺失值,如下下面这样有缺失值的。
9 1 3811 3812
9 2 3816
1 3 3819 3820
9 2 3816
1 3 3819 3820
用上面的代码读入,显然读入的数据达不到要求,我们可以手动的添加‘.’符号是可以解决这个问题的,但是假如这种缺失值现象非常严重,大量的手动添加是不可能的,而且会出现认为的错误。SAS提供了一个有用的option:missover,当变量没有对应的赋值,相当于可以帮你自动添加点符号,即赋缺失值(一般都给后面没有赋值的变量)。
data ex;
infile datalines missover;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816
1 3 3819 3820
;
run;
infile datalines missover;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816
1 3 3819 3820
;
run;
你会发现假如用option:truncover也可以读到符合要求的数据。
data ex;
infile datalines truncover ;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816
1 3 3819 3820
;
run;
infile datalines truncover ;
input x1 x2 x3 x4;
datalines;
9 1 3811 3812
9 2 3816
1 3 3819 3820
;
run;
truncover和missover的功能有相似的地方,就是两个选项都赋值给余下的INPUT语法给定的变量为缺失值。区别在于当读入的字段长度比该字段对应的变量定义的长度短时,INPUT语句不能读入全部的字段,MISSOVER选项将会设置该变量为缺失值,而TRUNCOVER则可以改进这个问题。【From SAS/HELP】下面的代码就说明了这个问题。
代码1:
filename outdata1 ‘F:\saschm\samplefileforB\out_data2a.txt’;
data numbers;
infile outdata1 truncover;
input testnum 5.;
run;
data numbers;
infile outdata1 truncover;
input testnum 5.;
run;
代码2:
filename outdata1 ‘F:\saschm\samplefileforB\out_data2a.txt’;
data numbers;
infile outdata1 misscover;
input testnum 5.;
run;
data numbers;
infile outdata1 misscover;
input testnum 5.;
run;
有truncover的代码就能很恰当的读入源数据,而missover只有当字段长度达到input规定的要求时,才能读入完整的值,否则赋缺失值。
小结:好像在这里也看到了,解决同一种问题有多种方法的例子,异途同归,她们之间的差异就在于实现的途径上,^_^。
欢迎补充,欢迎拍砖!
ps:原来已经有人对这三个options作为对比了。
FROM:SUGI26.
TITLE: MISSOVER, TRUNCOVER, and PAD, OH MY!! or Making Sense of the INFILE and INPUT Statements.
其中他列了个表,从另外一个应用的角度来对比这几个options的差异,很好:
FLOWOVER :The default. Causes the INPUT statement to jump to the next record if it doesn’t find values for all variables.
MISSOVER :Sets all empty vars to missing when reading a short line. However, it can also skip values.
STOPOVER :Stops the DATA step when it reads a short line.
TRUNCOVER: Forces the INPUT statement to stop reading when it gets to the end of a short line.This option will not skip information.
SCANOVER: Causes the INPUT statement to search the data lines for a character string specified in the INPUT.
PAD: Pads short lines with blanks to the length of the LRECL= option.
MISSOVER :Sets all empty vars to missing when reading a short line. However, it can also skip values.
STOPOVER :Stops the DATA step when it reads a short line.
TRUNCOVER: Forces the INPUT statement to stop reading when it gets to the end of a short line.This option will not skip information.
SCANOVER: Causes the INPUT statement to search the data lines for a character string specified in the INPUT.
PAD: Pads short lines with blanks to the length of the LRECL= option.