Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
How Do I Read a File That Uses Commas

How Do I Read a File That Uses Commas

Ratings: (0)|Views: 142 |Likes:
Published by Amitmse

More info:

Published by: Amitmse on Jun 02, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

06/02/2011

pdf

text

original

 
How do I read a file that uses commas, tabs or spaces as delimiters to separatevariables in SAS version 8?
Comma-separated files
It is quite easy to read a file that uses a comma as a delimiter using
proc import
inSAS version 8. There are two slightly different ways of reading a comma delimitedfile using
proc import
. In SAS version 8, a comma delimited file can be consideredas a special type of external file with special file extension .csv, which stands for comma-separated-variables. We show here the first sample program making use of this feature. Let's say we have following data stored in a file called
comma.csv
.
 AMC,22,3,2930,0,11:11 AMC,17,3,3350,0,11:30 AMC,22,,2640,0,12:34 Audi,17,5,2830,1,13:20 Audi,23,3,2070,1,11:11
Then the following
proc import
statement will read it in and create a temporary dataset called
mydata
.
 proc
im 
 port
da
t
afile="
co
 mma.
csv
"
o
u
t
=myda
t
a dbm 
s
=
csv
 
r
e
 p
la
c
e;ge
tn
ame
s
=
no
;
r
u
n
;
 proc
 
 pr
i
nt
da
t
a=myda
t
a;
r
u
n
;
 
As you can see in the output below, the data was read properly. Also notice that SAScreate default variable names as VAR1-VARn when variables names are not presentin the raw data file.
O
 b
s
VAR1 VAR2 VAR3 VAR4 VAR5 VAR61 AMC 22 3 2930 011:112 AMC 17 3 3350 011:303 AMC 22 . 2640 012:344 Audi 17 5 2830 113:205 Audi 23 3 2070 111:11
 
Y
ou might have a file where you have the names at the top of the file like the one below. With such a file you would like SAS to use the variable names from the file(e.g.,
make mpg
etc.).
 
 make,m 
 p
g,
r
e
 p
78,weigh
t
,f
or
eig
n
,
t
ime AMC,22,3,2930,0,11:11 AMC,17,3,3350,0,11:30 AMC,22,,2640,0,12:34 Audi,17,5,2830,1,13:20 Audi,23,3,2070,1,11:11
We can use the
getnames=yes;
statement to tell SAS we want it to read the variablenames from the first line of the data file, as illustrated below.
 proc
im 
 port
da
t
afile="
co
 mma1.
csv
"
o
u
t
=myda
t
a dbm 
s
=
csv
 
r
e
 p
la
c
e;ge
tn
ame
s
=ye
s
;
r
u
n
;
 proc
 
 pr
i
nt
da
t
a=myda
t
a;
r
u
n
;
 
As you can see from the output of the
proc print
shown below, the data are readcorrectly.
O
 b
s
make m 
 p
g
r
e
 p
78 weigh
t
f
or
eig
n
 
t
ime1 AMC 22 3 2930 011:112 AMC 17 3 3350 011:303 AMC 22 . 2640 012:344 Audi 17 5 2830 113:205 Audi 23 3 2070 111:11
 
Another way of reading a comma delimited file is to consider a comma as an ordinarydelimiter. Here is a program that shows how to usethe
dbms=dlm
and
delimiter=","
option to read a file just like we did above. Alsonotice that the external file doesn't have to have .csv extension.
 proc
im 
 port
da
t
afile="
co
 mma1.
txt
"
o
u
t
=myda
t
a dbm 
s
=dlm 
r
e
 p
la
c
e;delimi
t
e
r
=",";ge
tn
ame
s
=ye
s
;
r
u
n
;
 
Y
ou may want to create a permanent SAS data file using
 
proc import
. Suppose that we want tocreate a permanent SAS data file called
 
mydata
 
in the directory "c:\dissertation". We can do thefollowing.
lib
n
ame di
s
 
v
8 "
c
:\di
ss
e
rt
a
t
i
on
";
 proc
im 
 port
da
t
afile="
co
 mma1.
txt
"
o
u
t
=di
s
.myda
t
a dbm 
s
=dlm 
r
e
 p
la
c
e;delimi
t
e
r
=",";ge
tn
ame
s
=ye
s
;
r
u
n
;
 
 
Another feature of 
 
proc import
 
is that you can read in the input file starting from a specific rownumber using
 
datarow=
 
statement. Let's say that we want read from observation 4 on of the textfile
comma1.txt
. Since variables have names on the first row in the raw data file, we have touse
 
datarow=5
.
 
 proc
im 
 port
da
t
afile="
co
 mma1.
txt
"
o
u
t
=myda
t
a dbm 
s
=dlm 
r
e
 p
la
c
e;delimi
t
e
r
=",";ge
tn
ame
s
=ye
s
;da
t
a
ro
 w=5;
r
u
n
;
 proc
 
 pr
i
nt
da
t
a=myda
t
a;
r
u
n
;
 
 N
ow we can see from the output below the data has been read correctly.
 
O
 b
s
make m 
 p
g
r
e
 p
78 weigh
t
f
or
eig
n
 
t
ime1 Audi 17 5 2830 113:202 Audi 23 3 2070 111:11
 
O
n the other hand, if our variables don't have names in the raw file, we need touse
 
getnames=no
 
and
 
datarow=4
 
as shown below.
 
 proc
im 
 port
da
t
afile="
co
 mma2.
txt
"
o
u
t
=myda
t
a dbm 
s
=dlm 
r
e
 p
la
c
e;delimi
t
e
r
=",";ge
tn
ame
s
=
no
;da
t
a
ro
 w=4;
r
u
n
;
 
T
ab-delimited files
It is quite easy to read a file that uses a tab as a delimiter using
proc import
in SASversion 8. There are two slightly different ways of reading a tab delimited fileusing
proc import
. In SAS version 8, a tab delimited file can be considered as aspecial type of external file with file extension .txt. We show here the first sample program making use of this feature. Let's say we have following data stored in a filecalled
tab.txt
.
 AMC C
oncro
d 22 2930 4099 AMC Pa
c
e
r
17 3350 4749 AMC S
 pr
i
nt
22 2640 3799Bui
c
k Ce
nt
u
r
y 22 3250 4816Bui
c
k Ele
ctr
a 15 4080 7827
 
Then the following
proc import
statement will read it in and create a temporary dataset called
mydata
.
 proc
im 
 port
da
t
afile="
t
ab.
txt
"
o
u
t
=myda
t
a dbm 
s
=
t
ab
r
e
 p
la
c
e;ge
tn
ame
s
=
no
;
r
u
n
;
 proc
 
 pr
i
nt
da
t
a=myda
t
a;
r
u
n
;
 

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->