Add @1 in the Input ID and ANS (Wrong Program):
DATA SCORE;
ARRAY KEY[10] $ 1 _TEMPORARY_;
ARRAY ANS[10] $ 1;
IF _N_ = 1 THEN
DO I=1 TO 10;
INPUT KEY[I] @;
END;
INPUT @1 ID $ @5 (ANS1-ANS10) ($1.);
RAWSCORE = 0;
DO I=1 TO 10;
RAWSCORE = RAWSCORE + (ANS[I] EQ KEY[I]);
END;
PERCENT = 100*RAWSCORE/10;
DROP I;
DATALINES;
A B C D E E D C B A
001 ABCDEABCDE
002 AAAAABBBBB
;
PROC PRINT DATA=SCORE;
TITLE "SCORE Data Set";
ID ID;
VAR RAWSCORE PERCENT;
RUN;
This is a program from the book <Applied statistics and the SAS programming language> Chapter 15.
The original program from the textbook, there is no @1 at the INPUT statement for ID, it will give the desired output; If I added a column pointer at ID as shown below, it will read the key, the first dataline again, even though there exists a IF THEN statement and uses N to test whether it is the first dataline.
Can anyone explain what is the problem of this column @1, and why @5 is fine with this program?
You are missing another INPUT statement.
On the first iteration of the data step you first run 10 INPUT statements with trailing @. So you are still on the first line.
Since the program does not use the TRUNCOVER (or even the ancient MISSOVER) option the default action of FLOW OVER will apply. In which case as long as there is not an eleventh KEY value on the first line the INPUT statement without the pointer motion control will skip to the second line when it does not find anything at the end of the first line to satisfy the input of ID. There should be a note in the SAS LOG saying that INPUT had to move to new line.
If you add the @1 then on the first iteration of the data step that will move the pointer from after the last KEY value back to the first column. Which cause it to read the first KEY value as the ID instead. On the other iterations the @1 has no impact since that is the default location for the pointer at the start of an iteration.
It is probably worth being more explicit about when you are done with the first line.
Now adding pointer motion to move to the first column will not have any impact.