SAS数据的输入&输出

标签:
教育 |
分类: 03SAS数据处理 |
变截距模型:变量输入common里面;
变系数模型:c和变量都要放在cross—section specific里面。
Reading Data into SAS
This page was adapted from a page
created by Oliver Schabenberger.
1.
2.
3.
4.
5.
6.
1. SAS
数据的输入&输出(.TXT .DAT .XLS .MDB
.SAV格式)
一、导入导出文本文件(txt格式)、纯数据文件(dat格式);其实都是导入导出DLM文件(*.*),需要指定分隔符号。
1.TAB分割,第一行为变量名(以txt为例,dat同样)
PROC IMPORT OUT=A
RUN;
PROC EXPORT DATA=A
RUN;
DATA toads;
INFILE ’c:\MyRawData\ToadJump.dat’;
INPUT ToadName $ Weight Jump1 Jump2 Jump3;
RUN;
http://blog.sina.com.cn/s/blog_65abf1890100qthq.html
The following is a typical INFILE statement:
The INPUT statement is used to list the variables you want to read from the file, for example:
DELIMITER= (DLM=)
This option enables you to tell SAS what single character is used as a delimiter in the file you are reading. Some common delimiters are comma (,), vertical pipe (|), semi-colon (;), and tab. At this time the TAB needs to be by its hexadecimal value. For the ASCII systems (UNIX, Windows, and Linux), the value is 09. For EBCDIC systems (z/OS and MVS), the value is 05. The syntax looks like this for the pipe: DLM='|'. It looks like this for a TAB on ASCII systems: DLM='09'x.
data a;
infile
input AcquirorPERMN DateAnnounced
format DateAnnounced
run;
DATA toads0;
INFILE
INPUT t1 t2 t3 $ t4 $
RUN;
2.SPACE分割,第一行为变量名
PROC IMPORT OUT=A
RUN;
PROC EXPORT DATA=A
RUN;
二、导入导出EXCEL文件(xls格式)程序如下:
PROC IMPORT OUT=A
RUN;
PROC EXPORT DATA=WORK.A
RUN;
http://blog.sina.com.cn/s/blog_743c902a0100z6e2.html
三、导入导出EXCEL文件(CSV格式)程序如下:
PROC IMPORT OUT=A
RUN;
DATA music;
INFILE ’c:\MyRawData\Bands.csv’ DLM = ’,’ DSD MISSOVER;
INPUT BandName :$30. GigDate :MMDDYY10. EightPM NinePM TenPM ElevenPM;
RUN;
PROC EXPORT DATA=A
RUN;
四、导入导出ACCESS文件(mdb格式)程序如下:
PROC IMPORT OUT=A
RUN;
PROC EXPORT DATA=A
RUN;
五、导入SPSS文件(SAV格式)程序如下:
PROC IMPORT OUT=A
RUN;
proc export
data=A
run;
六、导入文件(STATA格式)程序如下:
proc import
out=a
proc contents data=
run;
proc export data=dd.return
run;
SAS导出到STATA格式:
%include "C:\Program
Files\SAS\savastata.sas";
data
result;
set a;
run;
%savastata(D:\,
-replace);
Reading PC SAS Data Files & Formats into SPSS for Windows
- Under SAS, create a library defined as the output file for an
export job, e.g.:
LIBNAME SPSS XPORT 'C:\SPSS\SASXPRT1.DAT';
which creates a SAS export file called SASXPRT1.DAT in a directory called C:\SPSS, referred to under SAS as libname SPSS. Any of these names can be changed, except for the engine type XPORT. Note that the library is associated with a DOS file, rather than a DOS directory. - If you need user-defined formats to be copied with the data,
associate a different libname with the XPORT engine, and copy your
formats catalog to a scratch dataset (called, say, FORMTS) in the
WORK directory, e.g.,
LIBNAME FORMT XPORT 'C:\SPSS\SASFORMT.DAT'; PROC FORMATS LIBRARY=LIBRARY CNTLOUT=FORMTS;
- Use PROC COPY to put the relevant data into the empty file,
e.g.,
PROC COPY IN=DATA OUT=SPSS; SELECT MYDSET;
which puts dataset DATA.MYDSET into the export file. If you made a format file in step 2, then also execute the command:PROC COPY IN=WORK OUT=FORMT; SELECT FORMTS;
- Under SPSS, execute the command:
GET SAS DATA='C:\SPSS\SASXPRT1.DAT' DSET(MYDSET) /FORMATS='C:\SPSS\SASFORMT.DAT' FSET(FORMTS).
assuming the file name and location to be as specified in step 1; omit the /FORMATS switch if you didn't do the stuff in step 2.
ods
proc print data=sashelp.class;run;
ods
proc freq data=sashelp.class;
run;
ods tagsets.excelxp close;
SAS Learning Module
Inputting data into SAS
This module will show how to input raw data into SAS,
showing how to read instream data and external raw data files using
some common raw data formats.
1. Reading free formatted data instream
One of the most common ways to read data into SAS is by reading the data instream in a data step - that is, by typing the data directly into the syntax of your SAS program. This approach is good for relatively small datasets. Spaces are usually used to "delimit" (or separate) free formatted data. For example:
DATA cars1;
CARDS;
AMC Concord 22 2930 4099
AMC Pacer
AMC Spirit
Buick Century 20 3250 4816
Buick Electra 15 4080 7827
;
RUN;
After reading in the data with a data step, it is usually a good idea to print the first few cases of your dataset to check that things were read correctly.
title "cars1 data";
PROC PRINT DATA=cars1(obs=5);
RUN;
Here is the output produced by the proc print statement above.
cars1 data
OBS
1
2
3
4
5
2. Reading fixed formatted data instream
Fixed formatted data can also be read instream. Usually, because there are no delimiters (such as spaces, commas, or tabs) to separate fixed formatted data, column definitions are required for every variable in the dataset. That is, you need to provide the beginning and ending column numbers for each variable. This also requires the data to be in the same columns for each case. For example, if we rearrange the cars data from above, we can read it as fixed formatted data:
DATA cars2;
CARDS;
AMC
AMC
AMC
BuickCentury2032504816
BuickElectra1540807827
;
RUN;
TITLE "cars2 data";
PROC PRINT DATA=cars2(obs=5);
RUN;
The benefit of fixed formatted data is that you can fit
more information on a line when you do not use delimiters such as
spaces or commas.
Here is the output produced by the proc print statement above.
cars2 data
OBS
1
2
3
4
5
3. Reading fixed formatted data from an external file
Suppose you are using a PC and you have a file named
cars3.dat, that is stored in the c:\carsdata directory of your
computer.
AMC
AMC
AMC
BuickCentury2032504816
BuickElectra1540807827
To read the file cars3.dat, use the following syntax.
RUN;
TITLE "cars3 data";
PROC PRINT DATA=cars3(obs=5);
RUN;
Here is the output produced by the proc print statement above.
cars3 data
OBS
1
2
3
4
5
Suppose you were working on UNIX.
DATA cars3;
RUN;
TITLE "cars3 data";
PROC PRINT DATA=cars3(obs=5);
RUN;
Likewise, suppose you were working on a Macintosh.
DATA cars3;
RUN;
TITLE "cars3 data";
PROC PRINT DATA=cars3(OBS=5);
RUN;
In examples 4, 5 and 6 below, you can change the infile statement as these examples have shown to make the programs appropriate for UNIX or for the Macintosh.
4. Reading free formatted (space delimited) data from an external file
Free formatted data that is space delimited can also be
read from an external file. For example, suppose you have a space
delimited file named cars4.dat, that is stored in the c:\carsdata
directory of your computer.
Here's what the data in the file cars4.dat look like:
AMC Concord 22 2930 4099
AMC Pacer
AMC Spirit
Buick Century 20 3250 4816
Buick Electra 15 4080 7827
To read the data from cars4.dat into SAS, use the following syntax:
DATA cars4;
RUN;
TITLE "cars4 data";
PROC PRINT DATA=cars4(OBS=5);
RUN;
Here is the output produced by the proc print statement above.
cars4 data
OBS
1
2
3
4
5
5. Reading free formatted (comma delimited) data from an external file
Free formatted data that is comma delimited can also be
read from an external file. For example, suppose you have a comma
delimited file named cars5.dat, that is stored in the c:\carsdata
directory of your computer.
Here's what the data in the file cars5.dat look like:
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
To read the data from cars5.dat into SAS, use the following syntax:
DATA cars5;
RUN;
TITLE "cars5 data";
PROC PRINT DATA=cars5(OBS=5);
RUN;
Here is the output produced by the proc print statement above.
cars5 data
OBS
1
2
3
4
5
6. Reading free formatted (tab delimited) data from an external file
Free formatted data that is TAB delimited can also be
read from an external file. For example, suppose you have a tab
delimited file named cars6.dat, that is stored in the c:\carsdata
directory of your computer.
Here's what the data in the file cars6.dat look like:
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
To read the data from cars6.dat into SAS, use the following syntax:
DATA cars6;
RUN;
TITLE "cars6 data";
PROC PRINT DATA=cars6(OBS=5);
RUN;
Here is the output produced by the proc print statement above.
cars6 data
OBS
1
2
3
4
5
7. Problems to look out for
If you read a file that is wider than 80 columns, you may need to use the lrecl= parameter on the infile statement.
8. For more information
For more detailed information on reading raw data into SAS, see Reading data into SAS in the SAS Library.
To learn how to create permanent SAS system files, see the SAS Learning Module on Reading and writing SAS system files.
For information on creating and recoding variables once you have entered your data, see the SAS Learning Module on Creating and recoding variables.
Section 2.4
First program
* Read internal data into SAS data set
uspresidents;
DATA uspresidents;
INPUT President $ Party $ Number;
Adams
Lincoln
Grant
Kennedy
RUN;
Second program
* Read data from external file into SAS data
set;
DATA uspresidents;
RUN;
Section 2.5
* Create a SAS data set named toads;
* Read the data file ToadJump.dat using list input;
DATA toads;
RUN;
* Print the data to make sure the file was read correctly;
PROC PRINT DATA = toads;
RUN;
Section 2.6
* Create a SAS data set named sales;
* Read the data file OnionRing.dat using column input;
DATA sales;
RUN;
* Print the data to make sure the file was read correctly;
PROC PRINT DATA = sales;
RUN;
Section 2.7
* Create a SAS data set named contest;
* Read the file Pumpkin.dat using formatted input;
DATA contest;
RUN;
* Print the data set to make sure the file was read correctly;
PROC PRINT DATA = contest;
RUN;
Section 2.9
First Program
* Create a SAS data set named nationalparks;
* Read a data file NatPark.dat mixing input styles;
DATA nationalparks;
RUN;
PROC PRINT DATA = nationalparks;
RUN;
Second Program
* Create a SAS data set named nationalparks;
* Read a data file NatPark.dat mixing input styles;
DATA nationalparks;
RUN;
PROC PRINT DATA = nationalparks;
RUN;
Section 2.10
DATA weblogs;
RUN;
PROC PRINT DATA = weblogs;
RUN;
Section 2.11
* Create a SAS data set named highlow;
* Read the data file using line pointers;
DATA highlow;
RUN;
PROC PRINT DATA = highlow;
RUN;
Section 2.12
* Input more than one observation from each record;
DATA rainfall;
RUN;
PROC PRINT DATA = rainfall;
RUN;
Section 2.13
* Use a trailing @, then delete surface streets;
DATA freeways;
RUN;
PROC PRINT DATA = freeways;
RUN;
Section 2.14
First Program
DATA icecream;
RUN;
DATA icecream;
RUN;
DATA class102;
RUN;
DATA homeaddress;
RUN;
Section 2.15
DATA reading;
RUN;
DATA music;
RUN;
PROC PRINT DATA = music;
RUN;
Section 2.16
PROC IMPORT DATAFILE ='c:\MyRawData\Bands2.csv' OUT = music REPLACE;
RUN;
PROC PRINT DATA = music;
Section 2.17
(data must be read from a
spreadsheet)
PROC IMPORT DATAFILE = 'c:\MyExcelFiles\OnionRing.xls' DBMS=XLS OUT = sales;
RUN;
PROC PRINT DATA = sales;
Section 2.18
* Read an Excel spreadsheet using DDE;
FILENAME baseball DDE 'CLIPBOARD';
DATA sales;
RUN;
* Read an Excel spreadsheet using DDE;
OPTIONS NOXSYNC NOXWAIT;
X '"C:\MyFiles\BaseBall.xls"';
FILENAME baseball DDE 'Excel|C:\MyFiles\[BaseBall.xls]sheet1!R2C1:R5C7';
DATA sales;
RUN;
Section 2.19
DATA distance;
RUN;
PROC PRINT DATA = distance;
RUN;
DATA Bikes.distance;
RUN;
PROC PRINT DATA = Bikes.distance;
RUN;
Section 2.20
LIBNAME plants 'c:\MySASLib';
DATA plants.magnolia;
RUN;
LIBNAME example 'c:\MySASLib';
PROC PRINT DATA = example.magnolia;
RUN;
Section 2.21
DATA 'c:\MySASLib\magnolia';
RUN;
PROC PRINT DATA = 'c:\MySASLib\magnolia';
RUN;
Section 2.22
DATA funnies (LABEL = 'Comics Character Data');
53
54
55
56
* Use PROC CONTENTS to describe data set funnies;
PROC CONTENTS DATA = funnies;
RUN;