N.M
N.M

Reputation: 685

reading a string from input file SAS

I am new to SAS, for my homework I need to read this file: http://www.math.tau.ac.il/~liadshek/Books.txt

I am trying the following:

DATA books_data;
   INFILE books firstobs=2;
   INPUT year numberBooks words copies annual author;
RUN;

But the author field is not read properly, generating the following error:

NOTE: Invalid data for author in line 2 28-32.
RULE:     ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
2         2008 1 82835 668118 199514 "X.C" 32
year=2008 numberBooks=1 words=82835 copies=668118 annual=199514 author=. _ERROR_=1 _N_=1

I tried adding DSD to my command, as suggested in these links:

http://www.ats.ucla.edu/stat/sas/faq/readdsd2.htm http://www.ats.ucla.edu/stat/sas/faq/InfileOptions_ut.htm

But that doesn't seem to help (all fields are not read properly when I do that...). What am I doing wrong?

Thanks and sorry for the dumb question.

Edit - My solution:

 FILENAME books URL "http://www.math.tau.ac.il/~liadshek/Books.txt" ;
 DATA books_data;
    INFILE books firstobs=2 dlm=" " DSD;
    INPUT year numberBooks words copies annual author $;
 RUN;

Upvotes: 1

Views: 314

Answers (2)

Joe
Joe

Reputation: 63434

One good approach to writing input statements for delimited files is to start by writing a proc import:

proc import file="blah.txt" out=want dbms=dlm dlm=' ' replace dsd;
run;

That will generate input code in the log that has a lot of the common options set. I don't recommend using proc import in production code, but it can be very helpful as a learning tool.

Upvotes: 1

user667489
user667489

Reputation: 9569

Without completely answering the question for you, since you've said this is homework, you need to find a way to tell SAS that 'author' is a character variable. If you don't tell it otherwise, it will assume that all variables are supposed to be numeric and will throw errors accordingly.

Upvotes: 4

Related Questions