proc import datafile="auto2.xls" out=auto1 replace;
sheet="page2";
run;
Example 3: Reading a file without variable names
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.
proc import datafile="a:\faq\auto.xls" out=auto replace;
getnames=no;
run;
Writing Excel files out from SAS
It is very easy to write out an Excel file using proc export in SAS version 8. Consider the following sample data file below.
Obs MAKE MPG WEIGHT PRICE
1 AMC 22 2930 4099
2 AMC 17 3350 4749
3 AMC 22 2640 3799
4 Buick 20 3250 4816
5 Buick 15 4080 7827
Here is a sample program that writes out an Excel file called mydata.xls into the directory
"c:\dissertation".
proc export data=mydata outfile='c:\dissertation\mydata.xls' replace;
run;
SAS读入复杂分隔数据——字符长度不同,字符中间有空格作为间隔符
1.字符长度不同
data web;
length site $41;
input age site $ hits;
datalines;
12 /default.htm 123456
130 /index.htm 97654
254 http://www.site3.edu/department/index.htm 987654
;
proc print;
run;
Obs site age hits
1 /default.htm 12 123456
2 /index.htm 130 97654
3 http://www.site3.edu/department/index.htm 254 987654
或者用
data web;
input age site & $41. hits;
datalines;
12 /default.htm 123456
130 /index.htm 97654
254 http://www.site3.edu/department/index.htm 987654
;
proc print;
run;
Obs age site hits
1 12 /default.htm 123456
2 130 /index.htm 97654
3 254 http://www.site3.edu/department/index.htm 987654
2.字符有多个单词,单词之间用空格隔开
data fruit;
infile 'C:\messy.txt' delimiter = ' ' dsd;
length fruit $22;
input zip fruit $ pounds;
proc print;
run;
Obs fruit zip pounds
1 apples, grapes kiwi 10034 123456
2 oranges 92626 97654
3 pears apple 25414 987654
或者
data fruit;
input zip fruit & $22. pounds;
datalines;
10034 apples, grapes kiwi 123456
92626 oranges 97654
25414 pears apple 987654
;
proc print;
run;
Obs zip fruit pounds
1 10034 apples, grapes kiwi 123456
2 92626 oranges 97654
3 25414 pears apple 987654
没有格式库的情况下读入数据:
read a SAS data file when I don't have its format library
If 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.
ERROR: The format $MAKEF was not found or could not be loaded.
ERROR: The format FORGNF was not found or could not be loaded.
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.
OPTIONS nofmterr;
libname in "c:\";
PROC FREQ DATA=in.auto;
TABLES foreign make;
RUN;
高效的保留或者去掉部分变量的方式:
The following program creates exactly the same file, but is a more efficient program because SAS only reads the desired variables.
DATA auto2;
SET auto (KEEP = make mpg price);
RUN;
The drop data step option works in a similar way.
DATA AUTO2;
SET auto (DROP = rep78 hdroom trunk weight length
turn displ gratio foreign);
RUN;
比较双份录入的差异是否存在:
proc compare base = person1 compare = person2 brief;
by id;
id id;
run;