BookmarkSubscribeRSS Feed
rookie72
Calcite | Level 5

Hi All,

I am trying to read a comma separatef flat file with following layout.

000000001687561, 000000001915561, 000000002877561, 000000004974561, 000000005703561, 000000006139561, 000000006594561, 000000006612561, 000000006806561, 000000007

108561, 000000007114561, 000000007581561, 000000007682561, 000000008259561, 000000008594561, 000000008864561, 000000009334561, 000000009840561, 000000010167561, 00

01333566192, 041501334051192, 041501340710192, 041501367218192, 041501462985192

I need to read this flat file in on column. Let us say.

obs    id

1     000000001687561

2     000000001915561

3     000000002877561

4    ...

5   .....

I am having issue reading the last record of each line as it goes to next line to complete the record. I am using the following code. Is there better code for this? Can someone please help me out ?

data

test (drop=rec_no i);

Thanks !

infile "c:\aa.txt" dlm = ',' dsd missover

;

length id $ 15

;

retain rec_no 0

;

rec_no +

1

;

do i = 1 to 40

input

i $ @;

if i = ' ' then delete

;

output

;

end

;

run

;

9 REPLIES 9
Astounding
PROC Star

rookie,

It's simpler than you might think.  Try this:

data test;

infile "c:\aa.txt" dlm=',' dsd;

length id $ 15;

input id @@;

run;

Do NOT add MISSOVER when using @@.

Good luck.

Astounding
PROC Star

Oops ... it's a little more complex because of the split records.

data test;

infile "c:\aa.txt" dlm=',' dsd;

length id $ 15;

input id @@;

last_id = lag(id);

if length(id)=15 then do;

   output;

   return;

end;

if length(id) + length(last_id) = 15 then do;

   id = compress(last_id || id);

   output;

end;

keep id;

run;

Sorry about that!

rookie72
Calcite | Level 5

Thank you very much I tired this code. It is reading only the first line of the raw data file. I will work on this code to make it work.

Astounding
PROC Star

Did you remember to remove MISSOVER?

rookie72
Calcite | Level 5

Yes I did remove the MISSOVER from the code. Logically your code should work. But for some reason this is reading only the first line of the raw file.

Astounding
PROC Star

OK, last guess.  It's a little difficult to do this from afar.

Is it possible that there really is only one line of data?  And that the tool you used to examine the raw data file split it into multiple lines for viewing purposes only?

That's easy enough to test.  See how many lines get read in:

data _null_;

infile 'c:\aa.txt';

input id $ 1;

run;

rookie72
Calcite | Level 5

Indeed there was only one data line in the raw file. The view mode in the text editor was splitting the single dataline into many lines. Thank you for guiding me in the right direction. I really appreciate it !

Tom
Super User Tom
Super User

You might need to add LRECL option to the INFILE statement to prevent SAS from truncating the line.  You should be able to tell from the messages in the SAS log.

Ksharp
Super User

OR

recfm=n

data x;

infile 'c:\aa.txt' recfm=n dlm=',';

input x : $50. @@;

cards;

run;

Ksharp

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
  • 9 replies
  • 2008 views
  • 0 likes
  • 4 in conversation