Learning SAS? Welcome to the exclusive online community for all SAS learners.

Importing a free form text file

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Importing a free form text file

I'm trying to create a SAS dataset from a .txt file that contains free form text (from a company's annual report). I'd ideally like to put each line in the text file into a row in the SAS dataset.

The following code works ok, but it will cut off any line greater than 32767 characters. It doesn't happen often, but it's not ideal.

Suggestions would be most appreciated!

filename text "\0001047469-10-006500.txt";

data filing;

infile text lrecl=50000;

input line $char5.;

linecount=_n_;

run;

Thanks in advance!


Accepted Solutions
Solution
‎07-02-2015 02:55 PM
Super User
Super User
Posts: 7,083

Re: Importing a free form text file

You should be able to read much more than 32K from a line.

filename text temp;

data _null_;

  file text;

  do line=1 to 10;

     len=int(100*ranuni(0));

     length x $100;

     x = repeat('A',len);

     put x;

  end;

run;

data filing;

infile text lrecl=1000000 length=len column=col truncover ;

linecount + 1;

do block=1 by 1 until( col >=len);

  input line $char25. @;

  output;

end;

run;

proc print; run;

View solution in original post


All Replies
Occasional Contributor
Posts: 9

Re: Importing a free form text file

What version of SAS are you using and what operating system is running this code?

New Contributor
Posts: 3

Re: Importing a free form text file

I'm running SAS 9.3 on Windows.

Super User
Super User
Posts: 7,997

Re: Importing a free form text file

Question - why do you have a file which has >32767 characters on one line?  The problem appears to lie in the file your trying to import, getting the vendor of that file to provide a better structured document would solve your problem.

New Contributor
Posts: 3

Re: Importing a free form text file

Thanks RW9, that's a nice alternative. It will take some time, though, so I'd like to solve my problem in SAS as well.

Super User
Posts: 19,878

Re: Importing a free form text file

So I'm assuming you've taken a PDF and converted it to .txt and are trying to import all the data. Do you have SAS EM/Text Analytics - it may have a way of importing this data for you?

Solution
‎07-02-2015 02:55 PM
Super User
Super User
Posts: 7,083

Re: Importing a free form text file

You should be able to read much more than 32K from a line.

filename text temp;

data _null_;

  file text;

  do line=1 to 10;

     len=int(100*ranuni(0));

     length x $100;

     x = repeat('A',len);

     put x;

  end;

run;

data filing;

infile text lrecl=1000000 length=len column=col truncover ;

linecount + 1;

do block=1 by 1 until( col >=len);

  input line $char25. @;

  output;

end;

run;

proc print; run;

Community Manager
Posts: 587

Re: Importing a free form text file

Thank you so much for marking answers helpful and correct!

-
Bev Brown
Visit me on LinkedIn.
Twitter too: @BevBrown
Thank you for using SAS.jpg
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 748 views
  • 1 like
  • 6 in conversation