SAS System Generates Code for You while Using Import/Export Procedure Anjan Matlapudi and J. Daniel Knapp Pharmacy Informatics, PerformRx, The Next Generation PBM, 200 Stevens Drive, Philadelphia, PA 19113 ABSTRACT It is a very common programming task to input data from flat files and export data into flat files. Most of these files are delimited text (tab, pipe, comma, etc.) or CSV files come from various sources such as third party vendor software. Earlier we demonstrated techniques to read complex flat files extracted from DB2 data source. Many of us use different approaches to read and write data files such as text, ASCII, sequential or flat files. Some of us might not be aware of how the SAS system generates code. This paper illustrates an easy way to obtain the actual SAS code while importing or exporting data from an external source using either the IMPORT/EXPORT wizard or IMPORT/EXPORT procedures. INTRODUCTION The SAS system has an excellent facility for importing and exporting data. We import flat files in the SAS system to perform further analysis and output results based on our business requirements. In this paper we will demonstrate an easy method to obtain SAS system generated code using the SAS IMPORT/EXPORT wizard and IMPORT/EXPORT procedures. FILE IMPORT WIZARD We now demonstrate the steps to obtain SAS generated code from the IMPORT wizard, specifically importing a tab delimited text file. We hope the following steps and screen shots will help you to import and obtain the SAS system generated code. Step 1: Go to File menu and click on File Import tab as shown below. 1
Step 2: You have an option to choose file type; in this example highlight tab delimited file. Step 3: Locate the file by browsing the directory path and highlight file name to select. Step 4: Input a name for the data set ( by default the imported dataset will reside in the SAS work directory unless you specify otherwise). Click the Finish button. 2
***Step 5: This is most important step to remember. Open a new program window or existing program window and press the F4 key (recall key), and magically the code will appear. You have the option now to make changes to the logic. FILE EXPORT WIZARD This same methodology applies to the EXPORT wizard. FILE IMPORT PROCEDURE You can also import any kind of delimited flat files using the file IMPORT procedure and get the SAS system generated code as shown below. *---Import Procedure to Import Comma Delimited Flat File---*; proc import datafile='c:\anjan Personnel\NESUG 2012\Import Export\Comma Delimited Text file.txt' out=dataout dbms=dlm; delimiter=","; /********************************************************************** * PRODUCT: SAS * VERSION: 9.2 * CREATOR: External File Interface * DATE: 10SEP12 * DESC: Generated SAS Datastep Code * TEMPLATE SOURCE: (None Specified.) ********************************************************************** data WORK.DATAOUT ; %let _EFIERR_ = 0; infile 'C:\NESUG 2012\Import Export\Comma Delimited Text file.txt' delimiter = MISSOVER DSD lrecl=32767 firstobs=2 ; informat Row best32. ; in informat Claim_Number best32. ; in informat Ammount_Paid comma32. ; format Row best12. ; 3
input Row Member_Number $ Claim_Number Date_Filled Ammount_Paid; if _ERROR_ then call symputx('_efierr_',1); /* set ERROR detection macro variable */ You can also import CSV files as shown and obtain the system generated code by pressing F4 key. *---CSV File---*; proc import datafile='c:\anjan Personel\NESUG 2012\Import Export\CSV_Formated.csv' out =CVSOut; FILE EXPORT PROCEDURE Similarly, you can also work with PROC EXPORT using the file export wizard or running the following code to get the system generated code. PROC EXPORT DATA=connmadel OUTFILE='C:\NESUG 2012\Import Export\CommaDelimited.txt' DBMS=DLM replace ; DELIMITER=; RUN; By pressing F4 key on a new program window this following code will be generated. data _null_; %let _EFIERR_ = 0; file 'C:\NESUG 2012\Import Export\CommaDelimited.txt' delimiter= DSD DROPOVER lrecl=32767; if _n_ = 1 then do; put "Row" "Member_Number" "Claim_Number" "Date_Filled" "Ammount_Paid" ; end; set CONNMADEL end=efieod; format Row best12. ; do; EFIOUT + 1; put Row @; put Member_Number $ @; 4
put Claim_Number @; put Date_Filled @; put Ammount_Paid ; ; end; if _ERROR_ then call symputx('_efierr_',1); if EFIEOD then call symputx('_efirec_',efiout); You can cleanup this as shown below to output delimited text file. data _null_; file 'C:\Anjan Personel\NESUG 2012\Import Export\CommaDelimited.txt' delimiter= DSD DROPOVER lrecl=32767; set CONNMADEL(drop=row) ; put Member_Number $ @; put Claim_Number @; put Date_Filled @; put Ammount_Paid ; if _ERROR_ then call symputx('_efierr_',1); IMPORT/EXPORT THROUGH MACRO VARIABLES Once you change the system generated code as per the logic, you can use positional macros to work with many files as shown below. *--Files import using Macro Variable---*; %FileImport (InFileName =,OutFileName=); proc import datafile="c:\nesug 2012\Import Export\In &In_FileName..txt" out=&outfilename. dbms=dlm; delimiter=","; %mend; %FileExport (InFileName =FileIn1,OutFileName=FileOut1); %FileExport (InFileName =FileIn2,OutFileName=FileOut2); The following logic is used to export many files using macro variables. *--Files Export using Macro Variable---*; %macro FileExport(FileIn=, FileOut=); data _null_; %let _EFIERR_ = 0; file "C:\NESUG 2012\Import Export\&FileOut..txt" delimiter= DSD DROPOVER lrecl=32767; set &FileIn. (drop=row) ; put Member_Number $ @; put Claim_Number @; put Date_Filled @; put Ammount_Paid ; ; if _ERROR_ then call symputx('_efierr_',1); 5
%mend; *--Passing FileName as Psotional Marco Vriable---*; %FileExport (FileIn=commadel, FileOut=CommaDelout1) %FileExport (FileIn=commadel, FileOut=CommaDelout1) CONCLUSION We hope this paper will be useful to educate SAS users to use the SAS IMPORT/EXPORT wizards and IMPORT/EXPORT Procedures, in conjuction with the F4 short cut key to obtain SAS generated code. You can modify the code based on your needs and preferences. We also demonstrated use of macro variables to handle multiple files and datasets. REFERENCES Anjan Matlapudi and J. Daniel Knapp Challenge! Reading Mainframe Hex Delimited Flat File Where Each Line Has Different Layout. Global SAS Forum 2010 Coders Corner, Paper 107-2010 Kuligowski, T. Andew. Datalines, Sequential Files, CVS, HTML and More Using INFILE and INPUT Statements to Introduce External Data into the SAS System, SUGI 31 Tutorials Paper 228-31. AKNOWLEDGMENTS We would like to acknowledge Mr. Shimels Afework, Senior Director, PerformRx. PerformRx provides pharmacy benefit management (PBM) services through proactively managing escalating pharmacy costs while focusing on clinical improvement and financial results. CONTACT INFORMATION: Your comments and questions are valued and encouraged. Contact the authors at \ Name Anjan Matlapudi Senior Pharmacy Analyst, Pharmacy Informatics Department Address PerformRx, The Next Generation PBM 200 Stevens Drive Philadelphia, PA 19113 Work Phone: (215)937-7252 Fax: (215)863-5100 E-mail: anjan.matlapudi@performrx.com anjanmat@gmail.com Name Knapp, J. Daniel, MBA Senior Manager, Pharmacy Informatics Department Address PerformRx, The Next Generation PBM 200 Stevens Drive Philadelphia, PA 19113 Work Phone: (215)937-7251 Fax: (215)863-5100 E-mail: Daniel.Knapp@performrx.com Jdjeep57@yahoo.com SAS is a registered trademark or trademark of SAS Institute, Inc. in the USA and other countries. 6