Why does the last observation lost when using column pointer control in SAS?

Viewed 34

In the code below, I'm wondering why is the last observation(=carlo) lost when using the column pointer control?

data work.toExercise ; 
infile "/home/u61425323/BASE_DATA/exercise.txt" ; /* my direction */
input Name $7. +3 Nation $7. +2 Code $5. ;
title "Why is the last observation(=carlo) lost?" ;  
run;
proc print ; run ; 

Below are the exercise.txt.

natasha   korea    a1111
kelly     america  b2222 
carlo     mexico   c333

Below are the output results.

enter image description here

Please forgive my poor English.

3 Answers

To stop SAS from going to a new line for input when the line is too short to satisfy the INPUT statement use the TRUNCOVER option on the INFILE statement.

Let's create a text file with your variable length records.

filename text temp;
options parmcards=text;
parmcards;
natasha   korea    a1111
kelly     america  b2222 
carlo     mexico   c333
;

If you read it with your data step we get this message:

NOTE: LOST CARD.
Name=carlo Nation=mexico Code=  _ERROR_=1 _N_=3
NOTE: 3 records were read from the infile TEXT.
      The minimum record length was 23.
      The maximum record length was 24.
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.ORGINAL has 2 observations and 3 variables.

But when we add the TRUNCOVER option it reads all three observations.

data want ; 
  infile text truncover ;
  input Name $7. +3 Nation $7. +2 Code $5. ;
run;

Result

enter image description here

Do not use the ancient MISSOVER option. That option will discard text at the end of lines that are not long enough for the format that is reading them. It can work if you only use LIST MODE input style where SAS adjusts the width of the informat to match the length of the next word on the line, but then you are just getting the TRUNCOVER behavior anyway so why not be specific.

data wrong ; 
  infile text missover ;
  input Name $7. +3 Nation $7. +2 Code $5. ;
run;

enter image description here

I think that happens because you have the last record shorter than the code expects.

You can try one of the infile options to control the processing in this case, for example:

infile "/home/u61425323/BASE_DATA/exercise.txt" MISSOVER;

I also do not know your task requirements but probably this version of the code would work more stable:

data work.toExercise ; 
  length Name $7 Nation $7 Code $5;
  infile "/home/u61425323/BASE_DATA/exercise.txt" dlm=' ';
  input Name Nation Code;
  title "Why is the last observation(=carlo) lost?" ;  
run;

Use the TRUNCOVER option with the INFILE statement.

From the INPUT documentation

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.

Related