FILENAME in FTP 'gpa.txt' LRECL=80
CD='/local2/samples/sas/ats/'
HOST='cluster.oac.ucla.edu'
USER='joebruin'
PASS='yourpassword' ;
DATA gpa ;
INFILE in ;
INPUT gpa hsm hss hse satm satv gender ;
RUN;
PROC PRINT DATA=gpa(obs=10) ;
RUN;
读入多个数据文件
quarter1.dat
1 120321 1236 154669 211326
1 326264 1326 163354 312665
1 420698 1327 142336 422685
1 211368 1236 156327 655237
1 378596 1429 145678 366578
quarter2.dat
2 140362 1436 114641 362415
2 157956 1327 124869 345215
2 215547 1472 165578 412567
2 204782 1495 150479 364474
2 232571 1345 135467 332567
quarter3.dat
3 140357 1339 142693 205881
3 149964 1420 152367 223795
3 159852 1479 160001 254874
3 139957 1527 163567 263088
3 150047 1602 175561 277552
quarter4.dat
4 479574 1367 155997 36134
4 496207 1459 140396 35941
4 501156 1598 135489 39640
4 532982 1601 143269 38695
4 563222 1625 147889 39556
filename year ('d:\quarter1.dat' 'd:\quarter2.dat' 'd:\quarter3.dat' 'd:\quarter4.dat');
data temp;
infile year;
input quarter sales tax expenses payroll;
run;
proc print data = temp;
run;
读取excel 数据集
Reading an Excel file into SAS
Suppose that you have an Excel spreadsheet called auto.xls. The data for this spreadsheet are shown below.
MAKE MPG WEIGHT PRICE
AMC Concord 22 2930 4099
AMC Pacer 17 3350 4749
AMC Spirit 22 2640 3799
Buick Century 20 3250 4816
Buick Electra 15 4080 7827
Using the Import Wizard is an easy way to import data into SAS. The Import Wizard can be found on the drop down file menu. Although the Import Wizard is easy it can be time consuming if used repeatedly. The very last screen of the Import Wizard gives you the option to save the statements SAS uses to import the data so that they can be used again. The following is an example that uses common options and also shows that the file was imported correctly.
PROC IMPORT OUT= WORK.auto1
DATAFILE= "C:\auto.xls"
DBMS=EXCEL REPLACE;
SHEET="auto1";
GETNAMES=YES;
MIXED=YES;
USEDATE=YES;
SCANTIME=YES;
RUN;
proc print data=auto1;
run;
Obs MAKE MPG WEIGHT PRICE
1 AMC Concord 22 2930 4099
2 AMC Pacer 17 3350 4749
3 Amc Spirit 22 2640 3799
4 Buick Century 20 3250 4816
5 Buick Electra 15 4080 7827
First we use the out= statement to tell SAS where to store the data once they are imported.
Next the datafile= statement tells SAS where to find the file we want to import.
The dbms= statement is used to identify the type of file being imported. This statement is redundant if the file you want to import already has an appropriate file extension, for example *.xls.
The replace statement will overwrite an existing file.
To specify which sheet SAS should import use the sheet="sheetname" statement. The default is for SAS to read the first sheet. Note that sheet names can only be 31 characters long.
The getnames=yes is the default setting and SAS will automatically use the first row of data as variable names. If the first row of your sheet does not contain variable names use the getnames=no.
SAS uses the first eight rows of data to determine whether the variable should be read as character or numeric. The default setting mixed=no assumes that each variable is either all character or all numeric. If you have a variable with both character and numeric values or a variable with missing values use mixed=yes statement to be sure SAS will read it correctly.
Conveniently SAS reads date, time and datetime formats. The usedate=yes is the default statement and SAS will read date or time formatted data as a date. When usedate=no SAS will read date and time formatted data with a datetime format. Keep the default statement scantime=yes to read in time formatted data as long as the variable does not also contain a date format.
Example 1: Making a permanent data file
What if you want the SAS data set created from proc import to be permanent? The answer is to use
libname statement. Let's say that we have an Excel file called auto.xls in directory "d:\temp" and we want to convert it into a SAS data file (call it myauto) and put it into the directory "c:\dissertation". Here is what we can do.
libname dis "c:\dissertation";
proc import datafile="d:\temp\auto.xls" out=dis.myauto replace;
run;
Example 2: Reading in a specific sheet
Sometimes you may only want to read a particular sheet from an Excel file instead of the entire Excel file. Let's say that we have a two-sheet Excel file called auto2.xls. The example below shows how to use the option sheet=sheetname to read the second sheet called page2 in it.