BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Stretlow
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
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;

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User
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;
Stretlow
Obsidian | Level 7
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)
data_null__
Jade | Level 19

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.

Kurt_Bremser
Super User

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.

data_null__
Jade | Level 19

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Stretlow
Obsidian | Level 7

Thank you for your swift responses on this, its appreciated

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 2644 views
  • 2 likes
  • 4 in conversation