DATA Step, Macro, Functions and more

Reading a Txt file that is all one line of consistant data.

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

Reading a Txt file that is all one line of consistant data.

Hello there.

 

I'm a little stuck and was sondering if someone had a solution to my problem. Im on version 9.3

 

I have a text file that contains account numbers. These account numbers are all 16 digits long but are all in one row in the text file.

 

My question is, is there a way to create an observation for each of the 16 digit account numbers

 

so 123456788765432112345678876543211234567887654321

 

would become

 

1234567887654321

1234567887654321

1234567887654321

 

Many thanks in advance for any help

 

 


Accepted Solutions
Solution
‎02-02-2016 06:51 AM
Super User
Posts: 6,936

Re: Reading a Txt file that is all one line of consistant data.

data want (keep=accountid);
infile 'path' lrecl=32000;
length line_in $ 32000;
input;
line_in = _infile_;
i = 1;
do until i > length(line_in)
  accountid = substr(line_in,i,16);
  output;
  i = i + 16;
end;
run;

Now, if you have no records at all (the file being one quasi-infinite stream):

data want;
infile 'path' lrecl=16 recfm=f;
input accountid $16.;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎02-02-2016 06:51 AM
Super User
Posts: 6,936

Re: Reading a Txt file that is all one line of consistant data.

data want (keep=accountid);
infile 'path' lrecl=32000;
length line_in $ 32000;
input;
line_in = _infile_;
i = 1;
do until i > length(line_in)
  accountid = substr(line_in,i,16);
  output;
  i = i + 16;
end;
run;

Now, if you have no records at all (the file being one quasi-infinite stream):

data want;
infile 'path' lrecl=16 recfm=f;
input accountid $16.;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 49

Re: Reading a Txt file that is all one line of consistant data.

data want;
infile 'path' lrecl=16 recfm=f;
input accountid $16.;
run;

this worked except I had to remove
lrecl=16 recfm=f;

otherwise it didnt keep 16 for each record (some were 15)
Respected Advisor
Posts: 3,777

Re: Reading a Txt file that is all one line of consistant data.

Show the log messages from both with and without LRECL and RECFM.

 

Seems to me that you may have record separaters that you are not aware and or other file qualities that you don't know.

Super User
Posts: 6,936

Re: Reading a Txt file that is all one line of consistant data.

In this case, I would take a more in-depth look at the file with a tool that shows all bytes (hexedit, hexdump or anything similar). It seems that you do have something that SAS treats as line separators.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: Reading a Txt file that is all one line of consistant data.


KurtBremser wrote:

In this case, I would take a more in-depth look at the file with a tool that shows all bytes (hexedit, hexdump or anything similar). It seems that you do have something that SAS treats as line separators.


SAS has tools for that too.  One being the LIST; statement.

Super User
Super User
Posts: 7,401

Re: Reading a Txt file that is all one line of consistant data.

Well, you can read the file one character at a time, then an if statement to check length:

data want (drop=chr);
  length id $16;
  retain id;
  infile "s:\temp\rob\tmp.txt" recfm=n;
  input chr $char1.;
  id=cats(id,chr);
  if lengthn(id)=16 then do;
    output;
    id="";
  end;
run;
Contributor
Posts: 49

Re: Reading a Txt file that is all one line of consistant data.

Thank you for your swift responses on this, its appreciated

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 285 views
  • 0 likes
  • 4 in conversation