Reputation: 1
I want to read a CSV document by using a filename
statement in SAS but Excel already included variable names as the first line when I input variable names by using an input
statement--there is going to be a mistake. How can I deal with this situation?
filename outdata "C:\Users\Xiang\Desktop\crime2005.csv";
data crime;
infile outdata dlm="," dsd ;
run;
proc means mean std maxdec=1 ;
run;
proc print;
run;
Upvotes: 0
Views: 3472
Reputation: 63424
First off - you're confusing things a bit by saying 'via the filename statement'. This is via datastep. The filename statement happens to be a relatively small component of this.
Second, let's get this into proper SAS indenting so we can see what's going on:
filename outdata "C:\Users\Xiang\Desktop\crime2005.csv";
data crime;
infile outdata dlm="," dsd ;
input [your-variable-list];
run;
proc means data=crime mean std maxdec=1 ;
run;
proc print data=crime;
run;
Data steps and Procs end with run (except for Procs that end in quit). Each of these is a separate step, so always include the run. Always include data= , unless you're using some fancy programming trick. 'data' always is in the first column, not indented - data step is the master statement, not filename.
These make your code readable, and protect you from mistakes. Readable code is important, even if you work alone; it means you understand what you wrote five years ago, five years from now.
Your original question - how do I avoid the errors from the header row?
filename outdata "C:\Users\Xiang\Desktop\crime2005.csv";
data crime;
infile outdata dlm="," dsd firstobs=2;
input [your-variable-list];
run;
There you go. FIRSTOBS=2 tells SAS to skip the first line [ie, the header row].
One thing you might try is a PROC IMPORT. PROC IMPORT with DBMS=CSV will do something really handy for you - it will put in the log a complete data step with all of the code to read the file in yourself. So while I don't actually recommend PROC IMPORT for production code [as it often makes poor decisions as to character/numeric formatting and lengths, among other things], it is very helpful to see how to get started with an input statement.
proc import file=outdata out=crime dbms=csv replace;
run;
Then look at your log, and copy that code out (removing line numbers); now you can modify it to your heart's content.
Upvotes: 3