Desktop productivity for business analysts and programmers

Reading a text file without any delimiter

Reply
Occasional Contributor
Posts: 11

Reading a text file without any delimiter

How can we import a text file with series of numbers and no delimiters. I need a new observation at every 12th numeric digit. Is this possible? Assume the variable name as 'numbers" with numeric datatype.

 

 

example of contents in text file:
numbers123456789012345678901234567890123456789012345678

 

 

I need the sas to read this file like this:

numbers
123456789012
345678901234
567890123456
789012345678

 

TIA,

Thanu

Community Manager
Posts: 3,012

Re: Reading a text file without any delimiter

Try the File->Import Data task, Fixed Width format.  You can place the delimiting column positions exactly where you need.  It's a gem of a tool.

Occasional Contributor
Posts: 11

Re: Reading a text file without any delimiter

Posted in reply to ChrisHemedinger

This is a part of an automation project we are handling. So I will not be able to file-> import. 

Can we write a data step? or directly use Proc import.

Community Manager
Posts: 3,012

Re: Reading a text file without any delimiter

The File->Import Data task will generate DATA step code, which you can then copy/adapt.  On the last page of the wizard, check "Generalize for use outside of SAS Enterprise Guide" (can't remember exact wording) which will make the code easer to use elsewhere.

 

But if you don't know ahead of time how many 12-digit numbers to expect, you might be better off with a solution like that offered by @Ksharp.

Super User
Super User
Posts: 7,144

Re: Reading a text file without any delimiter

[ Edited ]

If you literally have no line delimiters or field delimiters then just read it using formatted input.

Does the file literally have the letters 'numbers' at the beginning?  If so that you can conditionally skip those characters on the first pass of the data step.

Lets setup a dummy file that looks like your example.

filename test temp;
data _null_;
  file test recfm=n;
  put 'numbers123456789012345678901234567890123456789012345678';
run;

Then we can read that file like this.

data want ;
  infile test recfm=n ;
  if _n_=1 then input +7 @;
  input numbers 12. @@ ;
run;

 And we get this resulting data.

Obs       numbers

 1     123456789012
 2     345678901234
 3     567890123456
 4     789012345678
Occasional Contributor
Posts: 11

Re: Reading a text file without any delimiter

I tried testing your solution, but the output was not as expected, I get missing values in between the observations. ie., one record is read from raw data file, 12 records after that are missed and shown as missing values , and 13th observation is appearing in the output. please help.

Super User
Super User
Posts: 7,144

Re: Reading a text file without any delimiter

Post sample data (make sure to use the Insert Code icon looks like {i}).

You can also ask SAS to show you the contents.

data _null_;
  infile 'myfile.txt' obs=4 ;
  input;
  list;
run;
Occasional Contributor
Posts: 11

Re: Reading a text file without any delimiter

I tried testing your solution, but the output was not as expected, I get missing values in between the observations. ie., one record is read from raw data file, 12 records after that are missed and shown as missing values , and 13th observation is appearing in the output. please help.
Super User
Posts: 11,517

Re: Reading a text file without any delimiter


Thanu wrote:
I tried testing your solution, but the output was not as expected, I get missing values in between the observations. ie., one record is read from raw data file, 12 records after that are missed and shown as missing values , and 13th observation is appearing in the output. please help.

 

Post the code you used and an example of the data you used with this behavior.

 

The behavior you describe could mean that your line lengths are different than you expect or you have stuff in you data you haven't explained.

Super User
Posts: 10,116

Re: Reading a text file without any delimiter

Very interesting question.



data have;
input x $1. @@ ;
cards;
numbers123456789012345678901234567890123456789012345678
;
run;
data have;
 set have;
 retain found group 0;
 if anydigit(x) then found=1;
 if found then do;
   n+1;
   if mod(n,12)=1 then group+1;
 end;
run;
data want;
length want $ 20;
 do until(last.group);
  set have;
  by group;
   want=cats(want,x);
 end;
 keep want;
 run;


Super User
Posts: 20,203

Re: Reading a text file without any delimiter

You need a record layout. If you know you're only reading the first 12 characters you can use the trailing @@ to keep the record in line and keep reading the values. I'll assume you can remove the 'numbers' from the text yourself. 

 

You can replace the word CARDS in the INFILE statement with the myFile reference after you point it to your file.

 

 

file myfile 'path to my text file';
data want; input myvar $12. @@;
infile cards; cards; 123456789012345678901234567890123456789012345678 ; run;
Ask a Question
Discussion stats
  • 10 replies
  • 397 views
  • 0 likes
  • 6 in conversation