资源描述
程序:简短数据运用datalines或cards直接输入数据进行运算。
运行结果:
方法二:运用format读入数据;
运行结果:
You see here that there is a colon preceding each informat. This colon (called an informat
modifier) tells SAS to use the informat supplied but to stop reading the value for this
variable when a delimiter is encountered. Do not forget the colons because without them.
SAS may read past a delimiter to satisfy the width specified in the informat.
Colon:冒号;delimiter:定界符(在这里定界符是空格);
冒号的作用是从下一个非空的字符开始,读到下一个空格或者是informat指定的长度为止。
libname learn "E:/SAS1/chapter4";
data learn.Perm;
input ID : 3.
Gender : 1.
DOB : mmddyy10.
Height Weight;
label DOB="Date of Birth"
Height="Height in inches"
Weight="Weight in pounds";
format DOB date9.;
datalines;
001 M 10/21/1946 68 150
002 F 5/26/1950 63 122
003 M 5/11/1981 72 175
004 M 7/4/1983 70 128
005 F 12/25/2005 30 40
;
run;
title "Listing of Information";
proc print data=learn.Perm;
run;
如果吧ID和GENDER的两个冒号去掉,那SAS读记录的时候,
1. ID只会读取informat中指定的长度,即前三个字符 (如果每行数据前没有空格,那ID的值是正常的);
2. GENDER会是接下来的一个字符,而不会跳过空格,所以GENDER不会读到F和M这两个值。
事实上,这里不用format语句也可以正确输出gender。
只有当数据集中出现sas不能读的格式时,才需要用到format或者informat。如日期:1/1/1995
可以用format date9.将它表示为01Jan1995;如$12345,可以用format dollar5. 将它表示成$12345;
第二题:
程序:
运行结果:
Csv(comma separated values)。后缀为.csv的文件就是一种特殊格式的纯文本文件,即是一组字符序列,字符之间以英文字符的逗号或制表符(Tab)分隔。
可以使用记事本创建CSV文件。在保存时直接将后缀改为.csv即可。
第四题:
程序:
结果:
第五题:
You want to create a test data set that uses a DATALINES statement to read in
values for X and Y. In the DATA step, you want to create a new variable, Z, equal to
100 + 50X + 2X2 – 25Y + Y2. Use the following (X,Y) data pairs: (1,2), (3,6), (5,9),
and (9,11).
程序:
结果:
展开阅读全文