SAS Support

Inputing data into SAS

Inputting data into SAS.  UCLA: Statistical Consulting Group. 
from http://www.ats.ucla.edu/stat/sas/modules/input.htm (accessed August 11, 2015).

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;
 INPUT make $ model $ mpg weight price;
CARDS;
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
;
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   MAKE      MODEL     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

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;
  INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;
CARDS;
AMC  Concord2229304099
AMC  Pacer  1733504749
AMC  Spirit 2226403799
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    MAKE      MODEL     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

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.  Here's what the data in the file cars3.dat look like:

AMC  Concord2229304099
AMC  Pacer  1733504749
AMC  Spirit 2226403799
BuickCentury2032504816
BuickElectra1540807827 

To read the file cars3.dat, use the following syntax.

 DATA cars3;
  INFILE "c:\carsdata\cars3.dat";
  INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;
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    MAKE      MODEL     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

Suppose you were working on UNIX.  The UNIX version of this program, assuming the file cars3.dat is located in the directory ~/carsdata, would use the syntax shown below.  (Note that the "~" in the UNIX pathname above refers to the user's HOME directory. Hence, the directory called carsdata that is located in the users HOME directory.)

DATA cars3;
  INFILE "~/carsdata/cars3.dat";
  INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;
RUN;

TITLE "cars3 data";
PROC PRINT DATA=cars3(obs=5);
RUN; 

Likewise, suppose you were working on a Macintosh.  The Macintosh version of this program, assuming cars3.dat is located on your hard drive (called Hard Drive) in a folder called carsdata would look like this.

DATA cars3;
  INFILE 'Hard Drive:carsdata:cars3.dat';
  INPUT make $ 1-5 model $ 6-12 mpg 13-14 weight 15-18 price 19-22;
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   17 3350 4749
AMC Spirit  22 2640 3799
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;
  INFILE "c:\carsdata\cars4.dat";
  INPUT make $ model $ mpg weight price;
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    MAKE      MODEL     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 

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;
  INFILE "c:\carsdata\cars5.dat" delimiter=',';
  INPUT make $ model $ mpg weight price;
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    MAKE      MODEL     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 

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;
  INFILE "c:\carsdata\cars6.dat" DELIMITER='09'x;
  INPUT make $ model $ mpg weight price;
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    MAKE      MODEL     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

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

Selected Books on SAS

Administrative Healthcare Data: A Guide to Its Origin, Content, and Application Using SAS®  

Craig Dickstein, Renu Gehring

Epub ISBN# 978-1-62959-381-4

Mobi ISBN# 978-1-62959-382-1

PDF ISBN# 978-1-62959-380-7

Hardcopy ISBN# 978-1-61290-886-1

Pages 250

http://www.sas.com/store/prodBK_66981_en.html

 

The Little SAS® Book: A Primer, Fifth Edition

Lora D. Delwiche, Susan J. Slaughter

Epub ISBN# 978-1-61290-400-9

Mobi ISBN# 978-1-61290-945-5

PDF ISBN# 978-1-62959-013-4

Hardcopy ISBN# 978-1-61290-343-9

Pages 376

Learning SAS® by Example: A Programmer's Guide

Ron Cody

Epub ISBN# 978-1-59994-426-5

Mobi ISBN# 978-1-61290-946-2

PDF ISBN# 978-1-62959-014-1

Hardcopy ISBN# 978-1-59994-165-3

Pages 664

 

Step-by-Step Programming with Base SAS® 9.4

Publisher: SAS Institute

Copyright Date: July 2013

The PDF file of this book can be found at:

http://support.sas.com/documentation/cdl/en/basess/64003/HTML/default/viewer.htm#titlepage.htm

Longitudinal Data and SAS®: A Programmer's Guide

Ron Cody

Epub ISBN# 978-1-62959-249-7

Mobi ISBN# 978-1-62959-248-0

PDF ISBN# 978-1-62959-247-3

Hardcopy ISBN# 978-1-58025-924-8

Pages 208

http://www.sas.com/store/books/categories/usage-and-reference/longitudinal-data-and-sas-a-programmer-s-guide/prodBK_58176_en.html

 
Marriott Library Eccles Library Quinney Law Library