BookmarkSubscribeRSS Feed
Thanu
Calcite | Level 5

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

10 REPLIES 10
ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Thanu
Calcite | Level 5

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.

ChrisHemedinger
Community Manager

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
Tom
Super User Tom
Super User

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
Thanu
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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
Calcite | Level 5
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.
ballardw
Super User

@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.

Ksharp
Super User
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;


Reeza
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 7937 views
  • 0 likes
  • 6 in conversation