SAS中文论坛
标题:
[转] SAS中informat和format的使用
[打印本页]
作者:
shiyiming
时间:
2013-8-6 07:18
标题:
[转] SAS中informat和format的使用
SAS中informat用来设定输入数据的格式,而format用来设定输出数据的格式。举个例子,输出当前时间:
[code:2yfsuhel]data time;
x=date();
format x mmddyy10.;
put x=;
run;[/code:2yfsuhel]
如果没有format行,那么SAS系统就会把当前时间输出为一个数字(SAS用相对1960年1月1日的天数来存储日期),format指定了x的输出格式,以便于用户阅读。Format改变的是输出格式,而informat则是关于SAS的读入格式。举例,如果你的C盘下有个文本数据文件informat.txt,储存的是一个时间变量dte,有两个观测值:
dte
2008-4-8
2007-9-12
你要是这样读取,就会出错:
[code:2yfsuhel]data a;
infile “c:\informat.txt” firstobs=2 obs=3;
input dte;
run;[/code:2yfsuhel]
因为这里并没有指定输入数据格式,如果是数值数据,这当然没问题,但SAS并不认识时间,这时你就要用informat了,下面的程序就能正确读入informat.txt了:
[code:2yfsuhel]data a;
infile “c:\informat.txt” firstobs=2 obs=3;
input dte;
informat dte yymmdd10.;
run;[/code:2yfsuhel]
这里yymmdd10.就是时间的读入的格式,这让SAS知道了读入的数据是时间,它可以进行相应的转换来存储并读入。由于SAS对时间做了转换,也就是存储为相对于1960年1月1日的天数,为了能够让输出我们能够明白,即不是数字,我们还可以用format来改变输出格式,完整的程序如下:
[code:2yfsuhel]data a;
infile “c:\informat.txt” firstobs=2 obs=3;
input dte;
informat dte yymmdd10.;
format dte yymmdd10.;
put dte=;
run;[/code:2yfsuhel]
注:选项firstobs表示从第几行开始读入,obs表示到第几行读入结束,也就是从第firstobs行读到obs行,不设置obs则一直读到数据文件结尾。
原帖:http://www.itongji.cn/article/101512432012.html
欢迎光临 SAS中文论坛 (http://www.mysas.net/forum/)
Powered by Discuz! X3.2