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
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.
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.
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.
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
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 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;
@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.
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;
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.