SAS Library
Reading Data into SAS


This page was adapted from a page created by Oliver Schabenberger.  We thank Professor Schabenberger for permission to adapt and distribute this page via our web site.


1. Reading data inline
2. Reading data with column pointers
3. Reading data from ASCII files
4. Reading dBase files
5. Creating a permanent data set
6. Reading Excel files into SAS

 

  Variable 1

Variable 2

Variable 3

Observation 1 ---->      
Observation 2 ---->      
Observation 3 ---->      

1. Reading data inline

options nocenter;
DATA survey;
   INPUT id sex $ age inc r1 r2 r3 ;
   DATALINES;
 1  F  35 17  7 2 2
17  M  50 14  5 5 3
33  F  45  6  7 2 7
49  M  24 14  7 5 7
65  F  52  9  4 7 7
81  M  44 11  7 7 7
2   F  34 17  6 5 3
18  M  40 14  7 5 2
34  F  47  6  6 5 6
50  M  35 17  5 7 5
;
PROC PRINT; RUN;

OBS    ID    SEX    AGE     INC    R1    R2    R3

  1     1     F       35     17     7      2     2
  2    17     M       50     14     5      5     3
  3    33     F       45      6      7     2     7
  4    49     M       24     14     7      5     7
  5    65     F       52      9      4     7     7
  6    81     M       44     11     7      7     7
  7     2     F       34     17     6      5     3
  8    18     M       40     14     7      5     2
  9    34     F       47      6      6     5     6
 10    50     M       35     17     5      7     5

DATA survey;
   INPUT id sex  age inc r1 r2 r3 ;
   DATALINES;
 1  F  35 17  7 2 2
17  M  50 14  5 5 3
33  F  45  6  7 2 7
49  M  24 14  7 5 7
65  F  52  9  4 7 7
81  M  44 11  7 7 7
2   F  34 17  6 5 3
18  M  40 14  7 5 2
34  F  47  6  6 5 6
50  M  35 17  5 7 5
;
PROC PRINT; RUN;

OBS    ID    SEX    AGE     INC    R1    R2    R3

  1     1     .       35     17     7      2     2
  2    17     .       50     14     5      5     3
  3    33     .       45      6      7     2     7
  4    49     .       24     14     7      5     7
  5    65     .       52      9      4     7     7
  6    81     .       44     11     7      7     7
  7     2     .       34     17     6      5     3
  8    18     .       40     14     7      5     2
  9    34     .       47      6      6     5     6
 10    50     .       35     17     5      7     5

3.2. Reading data with column pointers

data thisone;
  input name $1-20  age city $29-37 state $ ;
  datalines;
Oliver Schabenberger    33  Lansing    MI
John T. Smith        37     New York   NY
;
run;
proc print data=thisone; run;

OBS    NAME                     AGE       CITY      STATE

 1     Oliver Schabenberger     33     Lansing      MI
 2     John T. Smith             37     New York     NY

data thisone;
  input name $1-20  age city $ state $ ;
  datalines;
Oliver Schabenberger    33  Lansing    MI
John T. Smith        37     New York   NY
;
run;
proc print data=thisone; run;

OBS    NAME                     AGE       CITY      STATE

 1     Oliver Schabenberger     33     Lansing      MI
 2     John T. Smith             37     New          York

data XYZ;
  input x y z;
  datalines;
1  1  12.4
1  2  11.3
1  3   1.4
2  1   2.1
2  2  19.4
2  3  10.0
;
run;

data XYZ;
  input x y z @@;
  datalines;
1  1  12.4 1  2  11.3 1  3   1.4
2  1   2.1 2  2  19.4 1  3  10.0
;
run;

3.3. Reading data from ASCII files
Reading data from ASCII files is more convenient than reading data line. Especially when the data contains many observations, this cuts down on the size of the actual program. To read an ASCII file in a SAS DATA step use the infile statement before the INPUT statement. The structure of the INPUT statement does not change. It is exactly as if you were to copy the contents of the ASCII file inside the DATA step after the DATALINES statement.

data readasc;
  infile 'D:\Research\Toxin\Data1998\DON.txt';
  input location truck probe DON;
run;

filename toxic 'D:\Research\Toxin\Data1998\DON.txt';
data readasc;
  infile toxic;
  input location truck probe DON;
run;

filename toxic 'D:\Research\Toxin\Data1998\DON.txt';
data readasc;
  infile toxic delimiter=',';
  input location truck probe DON;
run;

filename toxic 'D:\Research\Toxin\Data1998\DON.txt';
data readasc;
  infile toxic delimiter=',' firstobs=12 obs=100;
  input location truck probe DON;
run;

3.4. Reading dBase files

filename inf 'C:\Temp\mydata.dbf';

proc dbf db4=inf out=dbdata;

run;

3.5. Creating a permanent data set

data XYZ;
  input x y z;
  datalines;
1  1  12.4
1  2  11.3
1  3   1.4
2  1   2.1
2  2  19.4
2  3  10.0
;
run;

202  data XYZ;
203    input x y z;
204    datalines;

NOTE: The data set WORK.XYZ has 6 observations and 3 variables.
NOTE: The DATA statement used 0.26 seconds.
 

211  ;
212  run;
213

libname mylib 'D:\Research\Data\1996';

libname mylib 'C:\Research\Data\Manure';

data mylib.XYZ;

  input x y z;
  datalines;
1  1  12.4
1  2  11.3
1  3   1.4
2  1   2.1
2  2  19.4
2  3  10.0
;
run;

libname what 'C:\Research\Data\Manure';

proc print data=what.xyz;
run;

3.6. Reading Excel files into SAS

3.6.1. Using the Import facility

3.6.2. Using PROC ACCESS

proc access;
run;

proc access dbms=xls;
   create convert.xlsa.access;
   path = 'C:\Courses\Independent Study\Data\Soyref95.xls';
   scantype  = yes;
   worksheet = REF95;
   getnames  = yes;
   assign    = yes;
   unique    = yes;
   create Convert.xlsv.view;
   select all;
run;

data Soybean;
  set convert.xlsv;
run;


proc datasets library=convert;
  delete xlsv / memtype=view;

  delete xlsa / memtype=access;
run;
quit;


proc print data=soybean;
run;

3.6.3. Using Dynamic Data Exchange (DDE)

filename soy dde 'Excel|C:\Courses\Independent Study\Data\[SOYREF95.XLS]REF95!R2C2:R229C10';
data bean;
  infile soy missover;
  input labid $ month reps moisture abn dead abn1 abn2 abn3;
run;
proc print data=bean; run;

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.