Вы находитесь на странице: 1из 4

NESUG 17

Posters

Transporting Data in the SAS Universe


John Malloch and Jayne May Miller Center for Health Program Development and Management University of Maryland, Baltimore County Abstract
In the best of all possible worlds, we would only have to use SAS data. However, since that is not usually the case, this poster presentation will cover some of the basics of moving external data to and from SAS using code examples. We will cover Microsoft Access and Excel, relational databases, and flat files. The examples will use SAS Procedures like IMPORT, EXPORT, DBLOAD, and SQL, as well as the SAS data step. We will also cover some features of the SAS/ACCESS LIBNAME statement and the Pass-Through Facility. We will mention the SAS software utilized by each technique such as SAS Base, SAS/ACCESS for PC, and SAS/ACCESS for Relational Databases.

Introduction
SAS is the most accommodating software in the world for moving data between SAS databases and other file systems. There are many different options and techniques that can be used for transporting data to and from SAS. This paper is meant to serve as a simple introduction to some of the methods available for transporting data using SAS. We will use eight code examples as a quick reference. All of the examples use SAS/PC Version 8 in the Microsoft (MS) Windows environment. All of the examples will work in SAS Version 9, and 1-5 will also work in SAS Version 6. For different platforms and different data sources please refer to the SAS documentation for the SAS products mentioned with each example. For illustrative purposes we will number the examples as flights to and from the SAS 'Data Terminal'. You may use our 'Flight Schedule' below as a guide to send your data to a specific destination or you may wish to look over the entire schedule for future reference.

Arrivals and Departures Flight


1 2 3 4 5 6 7 8

From
SAS MS Excel SAS Flat File SAS MS SQL SAS MS SQL

To
MS Access SAS Flat File SAS MS SQL SAS MS SQL SAS

Carrier
PROC EXPORT PROC IMPORT PROC EXPORT DATA Step PROC DBLOAD DATA Step PROC SQL PROC SQL

Via
SAS/ACCESS SAS/ACCESS SAS Base SAS Base SAS/ACCESS SAS/ACCESS LIBNAME SAS/ACCESS Pass-Through SAS/ACCESS Pass-Through

Flight 1
This code uses PROC EXPORT to create a MS ACCESS table from a SAS file. 'DATA=' points to the SAS file, ' OUTTABLE=' is the name you wish to give the MS ACCESS table, and 'DATABASE=' is the location of the MS ACCESS database. The SAS/ACCESS Interface to PC File Formats is used here. SAS/PC has an online interface that will import and export for you interactively. PROC EXPORT DATA= sasfile OUTTABLE= "access_table" DBMS=ACCESS2000 REPLACE; DATABASE="C:\MS ACCESS\Database.mdb"; RUN;

NESUG 17

Posters

Flight 2
This example illustrates using PROC IMPORT to create a SAS file from a MS EXCEL file. 'GETNAMES=YES' is used to create SAS variable names from the EXCEL column names in the first row of an EXCEL table. The SAS/ACCESS Interface to PC File Formats is used here. PROC IMPORT OUT= sasfile DATAFILE= "C:\excelfile.XLS" DBMS=EXCEL2000 REPLACE; GETNAMES=YES; RUN;

Flight 3
This example uses PROC EXPORT to create a comma delimited flat file from a SAS file. 'DELIMITER=' allows you to specify the delimiter. SAS Base accomplishes this without any other products. PROC EXPORT DATA= sasfile OUTFILE= "C:\delimited_file.txt" DBMS=DLM; DELIMITER= ','; RUN;

Flight 4
The SAS DATA step can be used to create or read delimited files also. This example uses the DATA step to create a SAS file from a flat file with multiple delimiters. DATA sasfile; INFILE "C:\flatfile.txt" LRECL=400 DELIMITER="^~"; INPUT INTERNAL_ID: 10. NAME: $40. DOB: mmddyy10. ; RUN;

Flight 5
Here PROC DBLOAD is used to create a MS SQL Server table from a SAS file. SAS is moving away from DBLOAD, but while it is still around it can be useful especially if you don't know SQL very well. One limitation of DBLOAD is that it will not accept SAS variable names longer than eight characters. You may increase the length of the variable name using the 'RENAME' parameter. The 'TYPE' parameter allows you change the data type of an input variable. SAS/ACCESS for Relational Databases is used here. PROC DBLOAD DBMS=SQLSERVR DATA=sasfile; DATABASE= sql_database; TABLE="sql_table"; TYPE id='CHAR(11)' days='NUMERIC(3)'; RENAME days=days_in_a_year; COMMIT=0; ERRLIMIT=1; LIMIT=0; LOAD; RUN;

NESUG 17

Posters

Flight 6
The SAS/ACCESS Libname feature, introduced in SAS Version 7, is used here to create a SAS file from a SQL Server file using the SAS DATA step. This example shows three variables being kept from the SQL table. Two of the variables are used to create a new variable in the SAS file and then are dropped. The SAS file has a new budget total by month. This example and the next use the SAS/ACCESS Interface for ODBC, which is the documented way to get to SQL Server until SAS 9. SAS Version 9 has new SAS/ACCESS interfaces specifically for SQL Server and MySQL. Note: SAS procedures may also use this Libname interface. LIBNAME sqldb ODBC NOPROMPT="DSN=sql_database"; DATA sasfile; SET sqldb.sql_table(keep=budget1 budget2 month); budget_total = budget1 + budget2; DROP budget1 budget2; RUN;

Flight 7
This example is a little tricky. It creates a SQL Server table from a SAS file using the SAS/ACCESS Libname feature, and the SAS/ACCESS Pass-Through Facility. We have done this for two reasons. First, to show you that it can be done, and second, to show you a limitation of PROC SQL with SAS/ACCESS Libname. PROC SQL using SAS/ACCESS Libname will allow you to create a SQL table but will not allow you to alter it. The SAS/ACCESS Pass-Through Facility 'EXECUTE' command will allow you to alter a table and do other things that you cannot do directly with PROC SQL. LIBNAME sqldb ODBC NOPROMPT="DSN=sql_database"; PROC SQL; CONNECT TO ODBC (DATABASE= sql_database); CREATE TABLE sqldb.sql_table as SELECT * FROM sasfile; EXECUTE (ALTER TABLE sql_table ALTER COLUMN idno CHAR(11)) BY ODBC; DISCONNECT FROM ODBC;

Flight 8
The SAS/ACCESS Pass-Through Facility is used here with PROC SQL to create a SAS file from a SQL Server table. The 'CREATE TABLE' command creates a SAS file. The 'LIBNAME' statement points to the SAS database. LIBNAME sasdb 'C:\sasdb'; PROC SQL; CONNECT TO ODBC (DATABASE= sql_database); CREATE TABLE sasdb.sasfile AS SELECT * FROM CONNECTION TO ODBC (SELECT * FROM sqltbl); DISCONNECT FROM ODBC;

Conclusion
All of this can be a lot to take in. Here are some general guidelines for transporting data to and from SAS: The SAS DATA step may be used to process flat files with SAS Base. The SAS DATA step and SAS procedures may be used with SAS/ACCESS to process relational databases. PROC IMPORT and PROC EXPORT may be used to process flat files with SAS Base. PROC IMPORT and PROC EXPORT may be used to process Microsoft EXCEL and ACCESS files with SAS/ACCESS for PC Files. PROC DBLOAD may be used to create relational database files with SAS/ACCESS for Relational Databases. PROC SQL may be used to process relational databases with SAS/ACCESS for Relational Databases. Also of note: There are SAS/ACCESS products and interfaces for many databases as well as interfaces for ODBC and OLE DB. SAS Version 9 SAS/ACCESS Software for Relational Databases has new interfaces for MS SQL Server and MySQL.

NESUG 17

Posters

References
SAS OnlineDoc, Version 8, SAS Institute Inc. (Cary, NC): Base SAS Software SAS/ACCESS

Trademarks
SAS and all other SAS Institute, Inc., product or service names are registered trademarks or trademarks of SAS Institute., in the USA and other countries. indicates USA registration. Microsoft and all other Microsoft, Inc., product or service names are registered trademarks or trademarks of Microsoft, Inc., in the USA and other countries.

Contact Information
John Malloch CHPDM/UMBC 1000 Hilltop Circle Social Sciences 309 Baltimore, MD 21250 (410) 455-6297 malloch@chpdm.umbc.edu Jayne May Miller CHPDM/UMBC 1000 Hilltop Circle Social Sciences 309 Baltimore, MD 21250 (410) 455-6849 jmiller@chpdm.umbc.edu

Вам также может понравиться