2

I am trying to read this fixed width data into SAS:

John Garcia     114   Maple Ave.   
Sylvia Chung    1302  Washington Drive   
Martha Newton   45    S.E. 14th St.

I used this code:

libname mysas 'c:\users\LELopez243\mysas';
filename address 'c:\users\LELopez243\mysas\address.dat';
data mysas.address2;
infile address;
input Name $ 1-15 Number 16-19 Street $ 22-37;
run;
proc print data=mysas.address2;
run;

Got this result:

Obs Name            Number  Street
1   John Garcia     114     Sylvia Chung 1
2   Martha Newton   45

If I edit the .dat file and manually add spaces at the end of each line until they are each the same length, the code works. Any ideas for code that takes into account differing line lengths (w/o manually entering spaces).

Cœur
  • 37,241
  • 25
  • 195
  • 267
LELopez243
  • 21
  • 1
  • 3
  • Well it should not have given you the wrong result, not sure why this is happening. I executed the same code but under the datalines statement and got the correct results. – in_user Dec 22 '14 at 03:55
  • 1
    Try adding the truncover option to the end of the infile statement. – Reeza Dec 22 '14 at 04:27
  • @Reese ..Well it is not the case of missing columns, I am wondering how 'truncover' would help here – in_user Dec 22 '14 at 04:39
  • The second last statement about editing the line to make them each the same size makes it work. To me that says that SAS is having issues with the size of the lines. I'm not sure it works, and can't to check without looking at the actual .dat file. Or a hex dump of the first few lines. Basically, it's the first thing I'd try if I was having issues :) – Reeza Dec 22 '14 at 04:54
  • Tested it with the truncover option and it works :). Similar output as OP without the option. – Reeza Dec 22 '14 at 05:00
  • Thank you both!! @Reese the truncover option works. I just started using SAS, and it looks like the default was flowover. – LELopez243 Dec 22 '14 at 17:13
  • @Reese .. I suggest you should put the solution in the answer section, that would help others as well in quickly identifying the answer!! Thanks! – in_user Dec 23 '14 at 04:54

1 Answers1

2

Add the truncover option to your infile statement.

Truncover overrides the default behavior of the INPUT statement when an input data record is shorter than the INPUT statement expects. By default, the INPUT statement automatically reads the next input data record. TRUNCOVER enables you to read variable-length records when some records are shorter than the INPUT statement expects. Variables without any values assigned are set to missing.

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000146932.htm

libname mysas 'c:\users\LELopez243\mysas';
filename address 'c:\users\LELopez243\mysas\address.dat';
data mysas.address2;
infile address truncover;
input Name $ 1-15 Number 16-19 Street $ 22-37;
run;
proc print data=mysas.address2;
run;
Reeza
  • 20,510
  • 4
  • 21
  • 38