09-17-2013 10:15 AM
Just to learn the basics, I'm experimenting with combinations of different options of infile and input statemets, and sometimes the results are not quite those I would suppose. For example:
My data, koe.dat, contains only thoe two rows:
aa bb cc dd
gg hh ii mm
With column input,
input yks $ 1-2 kaks $ 4-5 kolme $ 7-8 nelj $ 10-11 @@;
I run into difficulties: there seems to be an infinite loop. At least the program never ends.
On the other hand, list input version of the same data step,
input yks $ kaks $ kolme $ nelj $ @@;
works perfectly, and produces a set with two observations.
What's actually happening in both instances?
Of course, to read my data, I do not need @@. However, I'd really like to know how it works exactly, with different input styles and different infile options.
09-18-2013 03:15 AM
@@ holds the input record for the next input statement across iterations of the data step. Your first example goes into a infinite loop because it will never move past the first input records as defined with column style input. This detail is loosely explained in the INPUT Statement documentation under the section about pointer control. With the relative pointer input style (you are using list style in your second example) SAS will move the pointer upon subsequent calls of the input statement to read the next line, and ultimately reach the end of you data file. With you example data, it is also worth noting that there is no need to use the double trailing @ line hold specifier as your lines all contain only one set of data each.
try adding something like the following to better show yourself what is happening.
if _n_ gt 1 then put _n_ _infile_;
input (yks kaks kolme nelj) ($) @@;
aa bb cc dd ww xx yy
gg hh ii mm zz
first data step iteration:
read first line (aa bb cc dd ww xx yy zz) and store in the _infile_ variable, assign first four columns, space delimited to listed variables and output record. behind the scenes, hold file cursor at end of input for nelj variable.
second data step interation:
when you use column style input you reset the curos position to 1 and reread the line as above and repeat to infinity.
when you use list style input you continue reading from stored cursor position assigning variables values until the end of a input record is reach and another is retrieved. variables continue to be assigned across input records until the variables are all filled, or the end of the file is encountered. if the end of file was not encountered, we would store the current input record and cursor position for the next data step record and repeat this process.