How to Use SAS – Lesson 3 – Importing External Data

This video series is intended to help you learn how to program using SAS for your statistical needs. Lesson 3 introduces the concept of permanent or external data sets and how to import them into SAS. I provide basic methods of importing permanent data sets using the INFILE ;statement and the IMPORT procedure (PROC ;IMPORT) for non-SAS based data files. I also discuss libraries and the LIBNAME ;statement to import SAS data directly using the SET statement. Finally, I show how one can save a SAS data set from the data step using LIBNAMEs in the DATA step.

Helpful Notes:

1. The LIBNAME statement is used to point SAS towards a specific folder on your computer.
2. The INFILE statement “reads” data into SAS if it is of a certain format (usually comma, space, or tab delimited).
3. PROC IMPORT – imports data of any of several different file formats into SAS.
4. The SET statement imports data from a library into SAS at the DATA STEP.
5. The library name in a data step’s data name “writes” data from SAS into your library folder using SAS’s own file format system.

Today’s Code:
data main;
input x y z;
cards;
1 2 3
7 8 9
;
run;

proc contents data=main;
run;

proc print data=main;
run;

/* TEMPLATED CODE: .txt file type, with or without delimiters */
data [appropriate data set name here]; infile ;”[your file location here, including .txt extension]” LRECL=[a logical length of your data to emcompass ;ENTIRE data] DLM=’,’;
input
[variable names here]
;
run;

data infile_main;
infile “C:\My SAS Files\main.txt”;
input x y z;
run;

proc print data=infile_main;
run;

/* TEMPLATED CODE: Microsoft Excel (.xls) file type */
proc import out=[your data set name here]
datafile='[your file location here, including .xls extension]’
dbms=excel replace;
*Optional statements are below; sheet='[specify sheet to obtain]’; getnames=[yes/no – first row = variable names]; mixed=[yes/no – refers to data types, if num AND char varibles, use yes]; usedate=[yes/no – read date formatted data as date formatted SAS data]; scantime=[yes/no – read in time formatted data as long as variable is not date format];
run;

proc import out=imported_excel
datafile=’C:\My SAS Files\main.xls’
dbms=excel replace;
*Optional statements are below; sheet=’Sheet1′; getnames=yes;
run;

proc print data=imported_excel;
run;

libname home “C:\My SAS Files\”;

data sas_format; set home.main;
run;

data home.sas_format; set infile_main;
run;

-FAIR ;USE-
“Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

Anayansi Gamboa has an extensive background in clinical data management as well as experience with different EDC systems including Oracle InForm, InForm Architect, Central Designer, CIS, Clintrial, Medidata Rave, Central Coding, OpenClinica Open Source and Oracle Clinical.

Advertisements

One thought on “How to Use SAS – Lesson 3 – Importing External Data”

Comments are closed.