Merge branch 'master' of ssh://jmd@git.sv.gnu.org/srv/git/pspp
[pspp-builds.git] / doc / data-io.texi
1 @node Data Input and Output
2 @chapter Data Input and Output
3 @cindex input
4 @cindex output
5 @cindex data
6 @cindex cases
7 @cindex observations
8
9 Data are the focus of the PSPP language.  
10 Each datum  belongs to a @dfn{case} (also called an @dfn{observation}).
11 Each case represents an individual or ``experimental unit''.
12 For example, in the results of a survey, the names of the respondents,
13 their sex, age, etc.@: and their responses are all data and the data
14 pertaining to single respondent is a case.
15 This chapter examines
16 the PSPP commands for defining variables and reading and writing data.
17 There are alternative commands to  read data from predefined sources
18 such as system files or databases (@xref{GET, GET DATA}.)
19
20 @quotation Note
21 These commands tell PSPP how to read data, but the data will not
22 actually be read until a procedure is executed.
23 @end quotation
24
25 @menu
26 * BEGIN DATA::                  Embed data within a syntax file.
27 * CLOSE FILE HANDLE::           Close a file handle.
28 * DATAFILE ATTRIBUTE::          Set custom attributes on data files.
29 * DATA LIST::                   Fundamental data reading command.
30 * END CASE::                    Output the current case.
31 * END FILE::                    Terminate the current input program.
32 * FILE HANDLE::                 Support for special file formats.
33 * INPUT PROGRAM::               Support for complex input programs.
34 * LIST::                        List cases in the active file.
35 * NEW FILE::                    Clear the active file and dictionary.
36 * PRINT::                       Display values in print formats.
37 * PRINT EJECT::                 Eject the current page then print.
38 * PRINT SPACE::                 Print blank lines.
39 * REREAD::                      Take another look at the previous input line.
40 * REPEATING DATA::              Multiple cases on a single line.
41 * WRITE::                       Display values in write formats.
42 @end menu
43
44 @node BEGIN DATA
45 @section BEGIN DATA
46 @vindex BEGIN DATA
47 @vindex END DATA
48 @cindex Embedding data in syntax files
49 @cindex Data, embedding in syntax files
50
51 @display
52 BEGIN DATA.
53 @dots{}
54 END DATA.
55 @end display
56
57 @cmd{BEGIN DATA} and @cmd{END DATA} can be used to embed raw ASCII
58 data in a PSPP syntax file.  @cmd{DATA LIST} or another input
59 procedure must be used before @cmd{BEGIN DATA} (@pxref{DATA LIST}).
60 @cmd{BEGIN DATA} and @cmd{END DATA} must be used together.  @cmd{END
61 DATA} must appear by itself on a single line, with no leading
62 white space and exactly one space between the words @code{END} and
63 @code{DATA}, like this:
64
65 @example
66 END DATA.
67 @end example
68
69 @node CLOSE FILE HANDLE
70 @section CLOSE FILE HANDLE
71
72 @display
73 CLOSE FILE HANDLE handle_name.
74 @end display
75
76 @cmd{CLOSE FILE HANDLE} disassociates the name of a file handle with a
77 given file.  The only specification is the name of the handle to close.
78 Afterward
79 @cmd{FILE HANDLE}.
80
81 If the file handle name refers to a scratch file, then the storage
82 associated with the scratch file in memory or on disk will be freed.
83 If the scratch file is in use, e.g.@: it has been specified on a
84 @cmd{GET} command whose execution has not completed, then freeing is
85 delayed until it is no longer in use.
86
87 The file named INLINE, which represents data entered between @cmd{BEGIN
88 DATA} and @cmd{END DATA}, cannot be closed.  Attempts to close it with
89 @cmd{CLOSE FILE HANDLE} have no effect.
90
91 @cmd{CLOSE FILE HANDLE} is a PSPP extension.
92
93 @node DATAFILE ATTRIBUTE
94 @section DATAFILE ATTRIBUTE
95 @vindex DATAFILE ATTRIBUTE
96
97 @display
98 DATAFILE ATTRIBUTE
99          ATTRIBUTE=name('value') [name('value')]@dots{}
100          ATTRIBUTE=name@b{[}index@b{]}('value') [name@b{[}index@b{]}('value')]@dots{}
101          DELETE=name [name]@dots{}
102          DELETE=name@b{[}index@b{]} [name@b{[}index@b{]}]@dots{}
103 @end display
104
105 @cmd{DATAFILE ATTRIBUTE} adds, modifies, or removes user-defined
106 attributes associated with the active file.  Custom data file
107 attributes are not interpreted by PSPP, but they are saved as part of
108 system files and may be used by other software that reads them.
109
110 Use the ATTRIBUTE subcommand to add or modify a custom data file
111 attribute.  Specify the name of the attribute as an identifier
112 (@pxref{Tokens}), followed by the desired value, in parentheses, as a
113 quoted string.  Attribute names that begin with @code{$} are reserved
114 for PSPP's internal use, and attribute names that begin with @code{@@}
115 or @code{$@@} are not displayed by most PSPP commands that display
116 other attributes.  Other attribute names are not treated specially.
117
118 Attributes may also be organized into arrays.  To assign to an array
119 element, add an integer array index enclosed in square brackets
120 (@code{[} and @code{]}) between the attribute name and value.  Array
121 indexes start at 1, not 0.  An attribute array that has a single
122 element (number 1) is not distinguished from a non-array attribute.
123
124 Use the DELETE subcommand to delete an attribute.  Specify an
125 attribute name by itself to delete an entire attribute, including all
126 array elements for attribute arrays.  Specify an attribute name
127 followed by an array index in square brackets to delete a single
128 element of an attribute array.  In the latter case, all the array
129 elements numbered higher than the deleted element are shifted down,
130 filling the vacated position.
131
132 To associate custom attributes with particular variables, instead of
133 with the entire active file, use @cmd{VARIABLE ATTRIBUTE} (@pxref{VARIABLE ATTRIBUTE}) instead.
134
135 @cmd{DATAFILE ATTRIBUTE} takes effect immediately.  It is not affected
136 by conditional and looping structures such as @cmd{DO IF} or
137 @cmd{LOOP}.
138
139 @node DATA LIST
140 @section DATA LIST
141 @vindex DATA LIST
142 @cindex reading data from a file
143 @cindex data, reading from a file
144 @cindex data, embedding in syntax files
145 @cindex embedding data in syntax files
146
147 Used to read text or binary data, @cmd{DATA LIST} is the most
148 fundamental data-reading command.  Even the more sophisticated input
149 methods use @cmd{DATA LIST} commands as a building block.
150 Understanding @cmd{DATA LIST} is important to understanding how to use
151 PSPP to read your data files.
152
153 There are two major variants of @cmd{DATA LIST}, which are fixed
154 format and free format.  In addition, free format has a minor variant,
155 list format, which is discussed in terms of its differences from vanilla
156 free format.
157
158 Each form of @cmd{DATA LIST} is described in detail below.
159
160 @xref{GET DATA}, for a command that offers a few enhancements over
161 DATA LIST and that may be substituted for DATA LIST in many
162 situations.
163
164 @menu
165 * DATA LIST FIXED::             Fixed columnar locations for data.
166 * DATA LIST FREE::              Any spacing you like.
167 * DATA LIST LIST::              Each case must be on a single line.
168 @end menu
169
170 @node DATA LIST FIXED
171 @subsection DATA LIST FIXED
172 @vindex DATA LIST FIXED
173 @cindex reading fixed-format data
174 @cindex fixed-format data, reading
175 @cindex data, fixed-format, reading
176 @cindex embedding fixed-format data
177
178 @display
179 DATA LIST [FIXED]
180         @{TABLE,NOTABLE@}
181         [FILE='file-name']
182         [RECORDS=record_count]
183         [END=end_var]
184         [SKIP=record_count]
185         /[line_no] var_spec@dots{}
186
187 where each var_spec takes one of the forms
188         var_list start-end [type_spec]
189         var_list (fortran_spec)
190 @end display
191
192 @cmd{DATA LIST FIXED} is used to read data files that have values at fixed
193 positions on each line of single-line or multiline records.  The
194 keyword FIXED is optional.
195
196 The FILE subcommand must be used if input is to be taken from an
197 external file.  It may be used to specify a file name as a string or a
198 file handle (@pxref{File Handles}).  If the FILE subcommand is not used,
199 then input is assumed to be specified within the command file using
200 @cmd{BEGIN DATA}@dots{}@cmd{END DATA} (@pxref{BEGIN DATA}).
201
202 The optional RECORDS subcommand, which takes a single integer as an
203 argument, is used to specify the number of lines per record.  If RECORDS
204 is not specified, then the number of lines per record is calculated from
205 the list of variable specifications later in @cmd{DATA LIST}.
206
207 The END subcommand is only useful in conjunction with @cmd{INPUT
208 PROGRAM}.  @xref{INPUT PROGRAM}, for details.
209
210 The optional SKIP subcommand specifies a number of records to skip at
211 the beginning of an input file.  It can be used to skip over a row
212 that contains variable names, for example.
213
214 @cmd{DATA LIST} can optionally output a table describing how the data file
215 will be read.  The TABLE subcommand enables this output, and NOTABLE
216 disables it.  The default is to output the table.
217
218 The list of variables to be read from the data list must come last.
219 Each line in the data record is introduced by a slash (@samp{/}).
220 Optionally, a line number may follow the slash.  Following, any number
221 of variable specifications may be present.
222
223 Each variable specification consists of a list of variable names
224 followed by a description of their location on the input line.  Sets of
225 variables may be specified using the @code{DATA LIST} TO convention
226 (@pxref{Sets of
227 Variables}).  There are two ways to specify the location of the variable
228 on the line: columnar style and FORTRAN style.
229
230 In columnar style, the starting column and ending column for the field
231 are specified after the variable name, separated by a dash (@samp{-}).
232 For instance, the third through fifth columns on a line would be
233 specified @samp{3-5}.  By default, variables are considered to be in
234 @samp{F} format (@pxref{Input and Output Formats}).  (This default can be
235 changed; see @ref{SET} for more information.)
236
237 In columnar style, to use a variable format other than the default,
238 specify the format type in parentheses after the column numbers.  For
239 instance, for alphanumeric @samp{A} format, use @samp{(A)}.  
240
241 In addition, implied decimal places can be specified in parentheses
242 after the column numbers.  As an example, suppose that a data file has a
243 field in which the characters @samp{1234} should be interpreted as
244 having the value 12.34.  Then this field has two implied decimal places,
245 and the corresponding specification would be @samp{(2)}.  If a field
246 that has implied decimal places contains a decimal point, then the
247 implied decimal places are not applied.
248
249 Changing the variable format and adding implied decimal places can be
250 done together; for instance, @samp{(N,5)}.
251
252 When using columnar style, the input and output width of each variable is
253 computed from the field width.  The field width must be evenly divisible
254 into the number of variables specified.
255
256 FORTRAN style is an altogether different approach to specifying field
257 locations.  With this approach, a list of variable input format
258 specifications, separated by commas, are placed after the variable names
259 inside parentheses.  Each format specifier advances as many characters
260 into the input line as it uses.
261
262 Implied decimal places also exist in FORTRAN style.  A format
263 specification with @var{d} decimal places also has @var{d} implied
264 decimal places.
265
266 In addition to the standard format specifiers (@pxref{Input and Output
267 Formats}), FORTRAN style defines some extensions:
268
269 @table @asis
270 @item @code{X}
271 Advance the current column on this line by one character position.
272
273 @item @code{T}@var{x}
274 Set the current column on this line to column @var{x}, with column
275 numbers considered to begin with 1 at the left margin.
276
277 @item @code{NEWREC}@var{x}
278 Skip forward @var{x} lines in the current record, resetting the active
279 column to the left margin.
280
281 @item Repeat count
282 Any format specifier may be preceded by a number.  This causes the
283 action of that format specifier to be repeated the specified number of
284 times.
285
286 @item (@var{spec1}, @dots{}, @var{specN})
287 Group the given specifiers together.  This is most useful when preceded
288 by a repeat count.  Groups may be nested arbitrarily.
289 @end table
290
291 FORTRAN and columnar styles may be freely intermixed.  Columnar style
292 leaves the active column immediately after the ending column
293 specified.  Record motion using @code{NEWREC} in FORTRAN style also
294 applies to later FORTRAN and columnar specifiers.
295  
296 @menu
297 * DATA LIST FIXED Examples::    Examples of DATA LIST FIXED.
298 @end menu
299
300 @node DATA LIST FIXED Examples
301 @unnumberedsubsubsec Examples
302
303 @enumerate
304 @item
305 @example
306 DATA LIST TABLE /NAME 1-10 (A) INFO1 TO INFO3 12-17 (1).
307
308 BEGIN DATA.
309 John Smith 102311
310 Bob Arnold 122015
311 Bill Yates  918 6
312 END DATA.
313 @end example
314
315 Defines the following variables:
316
317 @itemize @bullet
318 @item
319 @code{NAME}, a 10-character-wide long string variable, in columns 1
320 through 10.
321
322 @item
323 @code{INFO1}, a numeric variable, in columns 12 through 13.
324
325 @item
326 @code{INFO2}, a numeric variable, in columns 14 through 15.
327
328 @item
329 @code{INFO3}, a numeric variable, in columns 16 through 17.
330 @end itemize
331
332 The @code{BEGIN DATA}/@code{END DATA} commands cause three cases to be
333 defined:
334
335 @example
336 Case   NAME         INFO1   INFO2   INFO3
337    1   John Smith     10      23      11
338    2   Bob Arnold     12      20      15
339    3   Bill Yates      9      18       6
340 @end example
341
342 The @code{TABLE} keyword causes PSPP to print out a table
343 describing the four variables defined.
344
345 @item
346 @example
347 DAT LIS FIL="survey.dat"
348         /ID 1-5 NAME 7-36 (A) SURNAME 38-67 (A) MINITIAL 69 (A)
349         /Q01 TO Q50 7-56
350         /.
351 @end example
352
353 Defines the following variables:
354
355 @itemize @bullet
356 @item
357 @code{ID}, a numeric variable, in columns 1-5 of the first record.
358
359 @item
360 @code{NAME}, a 30-character long string variable, in columns 7-36 of the
361 first record.
362
363 @item
364 @code{SURNAME}, a 30-character long string variable, in columns 38-67 of
365 the first record.
366
367 @item
368 @code{MINITIAL}, a 1-character short string variable, in column 69 of
369 the first record.
370
371 @item
372 Fifty variables @code{Q01}, @code{Q02}, @code{Q03}, @dots{}, @code{Q49},
373 @code{Q50}, all numeric, @code{Q01} in column 7, @code{Q02} in column 8,
374 @dots{}, @code{Q49} in column 55, @code{Q50} in column 56, all in the second
375 record.
376 @end itemize
377
378 Cases are separated by a blank record.
379
380 Data is read from file @file{survey.dat} in the current directory.
381
382 This example shows keywords abbreviated to their first 3 letters.
383
384 @end enumerate
385
386 @node DATA LIST FREE
387 @subsection DATA LIST FREE
388 @vindex DATA LIST FREE
389
390 @display
391 DATA LIST FREE
392         [(@{TAB,'c'@}, @dots{})]
393         [@{NOTABLE,TABLE@}]
394         [FILE='file-name']
395         [SKIP=record_cnt]
396         /var_spec@dots{}
397
398 where each var_spec takes one of the forms
399         var_list [(type_spec)]
400         var_list *
401 @end display
402
403 In free format, the input data is, by default, structured as a series
404 of fields separated by spaces, tabs, commas, or line breaks.  Each
405 field's content may be unquoted, or it may be quoted with a pairs of
406 apostrophes (@samp{'}) or double quotes (@samp{"}).  Unquoted white
407 space separates fields but is not part of any field.  Any mix of
408 spaces, tabs, and line breaks is equivalent to a single space for the
409 purpose of separating fields, but consecutive commas will skip a
410 field.
411
412 Alternatively, delimiters can be specified explicitly, as a
413 parenthesized, comma-separated list of single-character strings
414 immediately following FREE.  The word TAB may also be used to specify
415 a tab character as a delimiter.  When delimiters are specified
416 explicitly, only the given characters, plus line breaks, separate
417 fields.  Furthermore, leading spaces at the beginnings of fields are
418 not trimmed, consecutive delimiters define empty fields, and no form
419 of quoting is allowed.
420
421 The NOTABLE and TABLE subcommands are as in @cmd{DATA LIST FIXED} above.
422 NOTABLE is the default.
423
424 The FILE and SKIP subcommands are as in @cmd{DATA LIST FIXED} above.
425
426 The variables to be parsed are given as a single list of variable names.
427 This list must be introduced by a single slash (@samp{/}).  The set of
428 variable names may contain format specifications in parentheses
429 (@pxref{Input and Output Formats}).  Format specifications apply to all
430 variables back to the previous parenthesized format specification.  
431
432 In addition, an asterisk may be used to indicate that all variables
433 preceding it are to have input/output format @samp{F8.0}.
434
435 Specified field widths are ignored on input, although all normal limits
436 on field width apply, but they are honored on output.
437
438 @node DATA LIST LIST
439 @subsection DATA LIST LIST
440 @vindex DATA LIST LIST
441
442 @display
443 DATA LIST LIST
444         [(@{TAB,'c'@}, @dots{})]
445         [@{NOTABLE,TABLE@}]
446         [FILE='file-name']
447         [SKIP=record_count]
448         /var_spec@dots{}
449
450 where each var_spec takes one of the forms
451         var_list [(type_spec)]
452         var_list *
453 @end display
454
455 With one exception, @cmd{DATA LIST LIST} is syntactically and
456 semantically equivalent to @cmd{DATA LIST FREE}.  The exception is
457 that each input line is expected to correspond to exactly one input
458 record.  If more or fewer fields are found on an input line than
459 expected, an appropriate diagnostic is issued.
460
461 @node END CASE
462 @section END CASE
463 @vindex END CASE
464
465 @display
466 END CASE.
467 @end display
468
469 @cmd{END CASE} is used only within @cmd{INPUT PROGRAM} to output the
470 current case.  @xref{INPUT PROGRAM}, for details.
471
472 @node END FILE
473 @section END FILE
474 @vindex END FILE
475
476 @display
477 END FILE.
478 @end display
479
480 @cmd{END FILE} is used only within @cmd{INPUT PROGRAM} to terminate
481 the current input program.  @xref{INPUT PROGRAM}.
482
483 @node FILE HANDLE
484 @section FILE HANDLE
485 @vindex FILE HANDLE
486
487 @display
488 For text files:
489         FILE HANDLE handle_name
490                 /NAME='file-name'
491                 [/MODE=CHARACTER]
492                 /TABWIDTH=tab_width
493
494 For binary files in native encoding with fixed-length records:
495         FILE HANDLE handle_name
496                 /NAME='file-name'
497                 /MODE=IMAGE
498                 [/LRECL=rec_len]
499
500 For binary files in native encoding with variable-length records:
501         FILE HANDLE handle_name
502                 /NAME='file-name'
503                 /MODE=BINARY
504                 [/LRECL=rec_len]
505
506 For binary files encoded in EBCDIC:
507         FILE HANDLE handle_name
508                 /NAME='file-name'
509                 /MODE=360
510                 /RECFORM=@{FIXED,VARIABLE,SPANNED@}
511                 [/LRECL=rec_len]
512
513 To explicitly declare a scratch handle:
514         FILE HANDLE handle_name
515                 /MODE=SCRATCH
516 @end display
517
518 Use @cmd{FILE HANDLE} to associate a file handle name with a file and
519 its attributes, so that later commands can refer to the file by its
520 handle name.  Names of text files can be specified directly on
521 commands that access files, so that @cmd{FILE HANDLE} is only needed when a
522 file is not an ordinary file containing lines of text.  However,
523 @cmd{FILE HANDLE} may be used even for text files, and it may be
524 easier to specify a file's name once and later refer to it by an
525 abstract handle.
526
527 Specify the file handle name as the identifier immediately following the
528 @cmd{FILE HANDLE} command name.  The identifier INLINE is reserved for
529 representing data embedded in the syntax file (@pxref{BEGIN DATA}) The
530 file handle name must not already have been used in a previous
531 invocation of @cmd{FILE HANDLE}, unless it has been closed by an
532 intervening command (@pxref{CLOSE FILE HANDLE}).
533
534 The effect and syntax of FILE HANDLE depends on the selected MODE:
535
536 @itemize
537 @item
538 In CHARACTER mode, the default, the data file is read as a text file,
539 according to the local system's conventions, and each text line is
540 read as one record.
541
542 In CHARACTER mode only, tabs are expanded to spaces by input programs,
543 except by @cmd{DATA LIST FREE} with explicitly specified delimiters.
544 Each tab is 4 characters wide by default, but TABWIDTH (a PSPP
545 extension) may be used to specify an alternate width.  Use a TABWIDTH
546 of 0 to suppress tab expansion.
547
548 @item
549 In IMAGE mode, the data file is treated as a series of fixed-length
550 binary records.  LRECL should be used to specify the record length in
551 bytes, with a default of 1024.  On input, it is an error if an IMAGE
552 file's length is not a integer multiple of the record length.  On
553 output, each record is padded with spaces or truncated, if necessary,
554 to make it exactly the correct length.
555
556 @item
557 In BINARY mode, the data file is treated as a series of
558 variable-length binary records.  LRECL may be specified, but its value
559 is ignored.  The data for each record is both preceded and followed by
560 a 32-bit signed integer in little-endian byte order that specifies the
561 length of the record.  (This redundancy permits records in these
562 files to be efficiently read in reverse order, although PSPP always
563 reads them in forward order.)  The length does not include either
564 integer.
565
566 @item
567 Mode 360 reads and writes files in formats first used for tapes in the
568 1960s on IBM mainframe operating systems and still supported today by
569 the modern successors of those operating systems.  For more
570 information, see @cite{OS/400 Tape and Diskette Device Programming},
571 available on IBM's website.
572
573 Alphanumeric data in mode 360 files are encoded in EBCDIC.  PSPP
574 translates EBCDIC to or from the host's native format as necessary on
575 input or output, using an ASCII/EBCDIC translation that is one-to-one,
576 so that a ``round trip'' from ASCII to EBCDIC back to ASCII, or vice
577 versa, always yields exactly the original data.
578
579 The RECFORM subcommand is required in mode 360.  The precise file
580 format depends on its setting:
581
582 @table @asis
583 @item F
584 @itemx FIXED
585 This record format is equivalent to IMAGE mode, except for EBCDIC
586 translation.
587
588 IBM documentation calls this @code{*F} (fixed-length, deblocked)
589 format.
590
591 @item V
592 @itemx VARIABLE
593 The file comprises a sequence of zero or more variable-length blocks.
594 Each block begins with a 4-byte @dfn{block descriptor word} (BDW).
595 The first two bytes of the BDW are an unsigned integer in big-endian
596 byte order that specifies the length of the block, including the BDW
597 itself.  The other two bytes of the BDW are ignored on input and
598 written as zeros on output.
599
600 Following the BDW, the remainder of each block is a sequence of one or
601 more variable-length records, each of which in turn begins with a
602 4-byte @dfn{record descriptor word} (RDW) that has the same format as
603 the BDW.  Following the RDW, the remainder of each record is the
604 record data.
605
606 The maximum length of a record in VARIABLE mode is 65,527 bytes:
607 65,535 bytes (the maximum value of a 16-bit unsigned integer), minus 4
608 bytes for the BDW, minus 4 bytes for the RDW.
609
610 In mode VARIABLE, LRECL specifies a maximum, not a fixed, record
611 length, in bytes.  The default is 8,192.
612
613 IBM documentation calls this @code{*VB} (variable-length, blocked,
614 unspanned) format.
615
616 @item VS
617 @itemx SPANNED
618 The file format is like that of VARIABLE mode, except that logical
619 records may be split among multiple physical records (called
620 @dfn{segments}) or blocks.  In SPANNED mode, the third byte of each
621 RDW is called the segment control character (SCC).  Odd SCC values
622 cause the segment to be appended to a record buffer maintained in
623 memory; even values also append the segment and then flush its
624 contents to the input procedure.  Canonically, SCC value 0 designates
625 a record not spanned among multiple segments, and values 1 through 3
626 designate the first segment, the last segment, or an intermediate
627 segment, respectively, within a multi-segment record.  The record
628 buffer is also flushed at end of file regardless of the final record's
629 SCC.
630
631 The maximum length of a logical record in VARIABLE mode is limited
632 only by memory available to PSPP.  Segments are limited to 65,527
633 bytes, as in VARIABLE mode.
634
635 This format is similar to what IBM documentation call @code{*VS}
636 (variable-length, deblocked, spanned) format.
637 @end table
638
639 In mode 360, fields of type A that extend beyond the end of a record
640 read from disk are padded with spaces in the host's native character
641 set, which are then translated from EBCDIC to the native character
642 set.  Thus, when the host's native character set is based on ASCII,
643 these fields are effectively padded with character @code{X'80'}.  This
644 wart is implemented for compatibility.
645
646 @item
647 SCRATCH mode is a PSPP extension that designates the file handle as a
648 scratch file handle.
649 Its use is usually unnecessary because file handle names that begin with
650 @samp{#} are assumed to refer to scratch files.  @pxref{File Handles},
651 for more information.
652 @end itemize
653
654 The NAME subcommand specifies the name of the file associated with the
655 handle.  It is required in all modes but SCRATCH mode, in which its
656 use is forbidden.
657
658 @node INPUT PROGRAM
659 @section INPUT PROGRAM
660 @vindex INPUT PROGRAM
661
662 @display
663 INPUT PROGRAM.
664 @dots{} input commands @dots{}
665 END INPUT PROGRAM.
666 @end display
667
668 @cmd{INPUT PROGRAM}@dots{}@cmd{END INPUT PROGRAM} specifies a
669 complex input program.  By placing data input commands within @cmd{INPUT
670 PROGRAM}, PSPP programs can take advantage of more complex file
671 structures than available with only @cmd{DATA LIST}.
672
673 The first sort of extended input program is to simply put multiple @cmd{DATA
674 LIST} commands within the @cmd{INPUT PROGRAM}.  This will cause all of
675 the data
676 files to be read in parallel.  Input will stop when end of file is
677 reached on any of the data files.
678
679 Transformations, such as conditional and looping constructs, can also be
680 included within @cmd{INPUT PROGRAM}.  These can be used to combine input
681 from several data files in more complex ways.  However, input will still
682 stop when end of file is reached on any of the data files.
683
684 To prevent @cmd{INPUT PROGRAM} from terminating at the first end of
685 file, use
686 the END subcommand on @cmd{DATA LIST}.  This subcommand takes a
687 variable name,
688 which should be a numeric scratch variable (@pxref{Scratch Variables}).
689 (It need not be a scratch variable but otherwise the results can be
690 surprising.)  The value of this variable is set to 0 when reading the
691 data file, or 1 when end of file is encountered.
692
693 Two additional commands are useful in conjunction with @cmd{INPUT PROGRAM}.
694 @cmd{END CASE} is the first.  Normally each loop through the
695 @cmd{INPUT PROGRAM}
696 structure produces one case.  @cmd{END CASE} controls exactly
697 when cases are output.  When @cmd{END CASE} is used, looping from the end of
698 @cmd{INPUT PROGRAM} to the beginning does not cause a case to be output.
699
700 @cmd{END FILE} is the second.  When the END subcommand is used on @cmd{DATA
701 LIST}, there is no way for the @cmd{INPUT PROGRAM} construct to stop
702 looping,
703 so an infinite loop results.  @cmd{END FILE}, when executed,
704 stops the flow of input data and passes out of the @cmd{INPUT PROGRAM}
705 structure.
706
707 All this is very confusing.  A few examples should help to clarify.
708
709 @c If you change this example, change the regression test1 in
710 @c tests/command/input-program.sh to match.
711 @example
712 INPUT PROGRAM.
713         DATA LIST NOTABLE FILE='a.data'/X 1-10.
714         DATA LIST NOTABLE FILE='b.data'/Y 1-10.
715 END INPUT PROGRAM.
716 LIST.
717 @end example
718
719 The example above reads variable X from file @file{a.data} and variable
720 Y from file @file{b.data}.  If one file is shorter than the other then
721 the extra data in the longer file is ignored.
722
723 @c If you change this example, change the regression test2 in
724 @c tests/command/input-program.sh to match.
725 @example
726 INPUT PROGRAM.
727         NUMERIC #A #B.
728         
729         DO IF NOT #A.
730                 DATA LIST NOTABLE END=#A FILE='a.data'/X 1-10.
731         END IF.
732         DO IF NOT #B.
733                 DATA LIST NOTABLE END=#B FILE='b.data'/Y 1-10.
734         END IF.
735         DO IF #A AND #B.
736                 END FILE.
737         END IF.
738         END CASE.
739 END INPUT PROGRAM.
740 LIST.
741 @end example
742
743 The above example reads variable X from @file{a.data} and variable Y from
744 @file{b.data}.  If one file is shorter than the other then the missing
745 field is set to the system-missing value alongside the present value for
746 the remaining length of the longer file.
747
748 @c If you change this example, change the regression test3 in
749 @c tests/command/input-program.sh to match.
750 @example
751 INPUT PROGRAM.
752         NUMERIC #A #B.
753
754         DO IF #A.
755                 DATA LIST NOTABLE END=#B FILE='b.data'/X 1-10.
756                 DO IF #B.
757                         END FILE.
758                 ELSE.
759                         END CASE.
760                 END IF.
761         ELSE.
762                 DATA LIST NOTABLE END=#A FILE='a.data'/X 1-10.
763                 DO IF NOT #A.
764                         END CASE.
765                 END IF.
766         END IF.
767 END INPUT PROGRAM.
768 LIST.
769 @end example
770
771 The above example reads data from file @file{a.data}, then from
772 @file{b.data}, and concatenates them into a single active file.
773
774 @c If you change this example, change the regression test4 in
775 @c tests/command/input-program.sh to match.
776 @example
777 INPUT PROGRAM.
778         NUMERIC #EOF.
779
780         LOOP IF NOT #EOF.
781                 DATA LIST NOTABLE END=#EOF FILE='a.data'/X 1-10.
782                 DO IF NOT #EOF.
783                         END CASE.
784                 END IF.
785         END LOOP.
786
787         COMPUTE #EOF = 0.
788         LOOP IF NOT #EOF.
789                 DATA LIST NOTABLE END=#EOF FILE='b.data'/X 1-10.
790                 DO IF NOT #EOF.
791                         END CASE.
792                 END IF.
793         END LOOP.
794
795         END FILE.
796 END INPUT PROGRAM.
797 LIST.
798 @end example
799
800 The above example does the same thing as the previous example, in a
801 different way.
802
803 @c If you change this example, make similar changes to the regression
804 @c test5 in tests/command/input-program.sh.
805 @example
806 INPUT PROGRAM.
807         LOOP #I=1 TO 50.
808                 COMPUTE X=UNIFORM(10).
809                 END CASE.
810         END LOOP.
811         END FILE.
812 END INPUT PROGRAM.
813 LIST/FORMAT=NUMBERED.
814 @end example
815
816 The above example causes an active file to be created consisting of 50
817 random variates between 0 and 10.
818
819 @node LIST
820 @section LIST
821 @vindex LIST
822
823 @display
824 LIST
825         /VARIABLES=var_list
826         /CASES=FROM start_index TO end_index BY incr_index
827         /FORMAT=@{UNNUMBERED,NUMBERED@} @{WRAP,SINGLE@} 
828                 @{NOWEIGHT,WEIGHT@}
829 @end display
830
831 The @cmd{LIST} procedure prints the values of specified variables to the
832 listing file.
833
834 The VARIABLES subcommand specifies the variables whose values are to be
835 printed.  Keyword VARIABLES is optional.  If VARIABLES subcommand is not
836 specified then all variables in the active file are printed.
837
838 The CASES subcommand can be used to specify a subset of cases to be
839 printed.  Specify FROM and the case number of the first case to print,
840 TO and the case number of the last case to print, and BY and the number
841 of cases to advance between printing cases, or any subset of those
842 settings.  If CASES is not specified then all cases are printed.
843
844 The FORMAT subcommand can be used to change the output format.  NUMBERED
845 will print case numbers along with each case; UNNUMBERED, the default,
846 causes the case numbers to be omitted.  The WRAP and SINGLE settings are
847 currently not used.  WEIGHT will cause case weights to be printed along
848 with variable values; NOWEIGHT, the default, causes case weights to be
849 omitted from the output.
850
851 Case numbers start from 1.  They are counted after all transformations
852 have been considered.
853
854 @cmd{LIST} attempts to fit all the values on a single line.  If needed
855 to make them fit, variable names are displayed vertically.  If values
856 cannot fit on a single line, then a multi-line format will be used.
857
858 @cmd{LIST} is a procedure.  It causes the data to be read.
859
860 @node NEW FILE
861 @section NEW FILE
862 @vindex NEW FILE
863
864 @display
865 NEW FILE.
866 @end display
867
868 @cmd{NEW FILE} command clears the current active file.
869
870 @node PRINT
871 @section PRINT
872 @vindex PRINT
873
874 @display
875 PRINT 
876         OUTFILE='file-name'
877         RECORDS=n_lines
878         @{NOTABLE,TABLE@}
879         [/[line_no] arg@dots{}]
880
881 arg takes one of the following forms:
882         'string' [start-end]
883         var_list start-end [type_spec]
884         var_list (fortran_spec)
885         var_list *
886 @end display
887
888 The @cmd{PRINT} transformation writes variable data to the listing
889 file or an output file.  @cmd{PRINT} is executed when a procedure
890 causes the data to be read.  Follow @cmd{PRINT} by @cmd{EXECUTE} to
891 print variable data without invoking a procedure (@pxref{EXECUTE}).
892
893 All @cmd{PRINT} subcommands are optional.  If no strings or variables
894 are specified, PRINT outputs a single blank line.
895
896 The OUTFILE subcommand specifies the file to receive the output.  The
897 file may be a file name as a string or a file handle (@pxref{File
898 Handles}).  If OUTFILE is not present then output will be sent to
899 PSPP's output listing file.  When OUTFILE is present, a space is
900 inserted at beginning of each output line, even lines that otherwise
901 would be blank.
902
903 The RECORDS subcommand specifies the number of lines to be output.  The
904 number of lines may optionally be surrounded by parentheses.
905
906 TABLE will cause the PRINT command to output a table to the listing file
907 that describes what it will print to the output file.  NOTABLE, the
908 default, suppresses this output table.
909
910 Introduce the strings and variables to be printed with a slash
911 (@samp{/}).  Optionally, the slash may be followed by a number
912 indicating which output line will be specified.  In the absence of this
913 line number, the next line number will be specified.  Multiple lines may
914 be specified using multiple slashes with the intended output for a line
915 following its respective slash.
916
917 Literal strings may be printed.  Specify the string itself.  Optionally
918 the string may be followed by a column number or range of column
919 numbers, specifying the location on the line for the string to be
920 printed.  Otherwise, the string will be printed at the current position
921 on the line.
922
923 Variables to be printed can be specified in the same ways as available
924 for @cmd{DATA LIST FIXED} (@pxref{DATA LIST FIXED}).  In addition, a
925 variable
926 list may be followed by an asterisk (@samp{*}), which indicates that the
927 variables should be printed in their dictionary print formats, separated
928 by spaces.  A variable list followed by a slash or the end of command
929 will be interpreted the same way.
930
931 If a FORTRAN type specification is used to move backwards on the current
932 line, then text is written at that point on the line, the line will be
933 truncated to that length, although additional text being added will
934 again extend the line to that length.
935
936 @node PRINT EJECT
937 @section PRINT EJECT
938 @vindex PRINT EJECT
939
940 @display
941 PRINT EJECT 
942         OUTFILE='file-name'
943         RECORDS=n_lines
944         @{NOTABLE,TABLE@}
945         /[line_no] arg@dots{}
946
947 arg takes one of the following forms:
948         'string' [start-end]
949         var_list start-end [type_spec]
950         var_list (fortran_spec)
951         var_list *
952 @end display
953
954 @cmd{PRINT EJECT} advances to the beginning of a new output page in
955 the listing file or output file.  It can also output data in the same
956 way as @cmd{PRINT}.
957
958 All @cmd{PRINT EJECT} subcommands are optional.
959
960 Without OUTFILE, PRINT EJECT ejects the current page in
961 the listing file, then it produces other output, if any is specified.
962
963 With OUTFILE, PRINT EJECT writes its output to the specified file.
964 The first line of output is written with @samp{1} inserted in the
965 first column.  Commonly, this is the only line of output.  If
966 additional lines of output are specified, these additional lines are
967 written with a space inserted in the first column, as with PRINT.
968
969 @xref{PRINT}, for more information on syntax and usage.
970
971 @node PRINT SPACE
972 @section PRINT SPACE
973 @vindex PRINT SPACE
974
975 @display
976 PRINT SPACE OUTFILE='file-name' n_lines.
977 @end display
978
979 @cmd{PRINT SPACE} prints one or more blank lines to an output file.
980
981 The OUTFILE subcommand is optional.  It may be used to direct output to
982 a file specified by file name as a string or file handle (@pxref{File
983 Handles}).  If OUTFILE is not specified then output will be directed to
984 the listing file.
985
986 n_lines is also optional.  If present, it is an expression
987 (@pxref{Expressions}) specifying the number of blank lines to be
988 printed.  The expression must evaluate to a nonnegative value.
989
990 @node REREAD
991 @section REREAD
992 @vindex REREAD
993
994 @display
995 REREAD FILE=handle COLUMN=column.
996 @end display
997
998 The @cmd{REREAD} transformation allows the previous input line in a
999 data file
1000 already processed by @cmd{DATA LIST} or another input command to be re-read
1001 for further processing.
1002
1003 The FILE subcommand, which is optional, is used to specify the file to
1004 have its line re-read.  The file must be specified as the name of a file
1005 handle (@pxref{File Handles}).  If FILE is not specified then the last
1006 file specified on @cmd{DATA LIST} will be assumed (last file specified
1007 lexically, not in terms of flow-of-control).
1008
1009 By default, the line re-read is re-read in its entirety.  With the
1010 COLUMN subcommand, a prefix of the line can be exempted from
1011 re-reading.  Specify an expression (@pxref{Expressions}) evaluating to
1012 the first column that should be included in the re-read line.  Columns
1013 are numbered from 1 at the left margin.
1014
1015 Issuing @code{REREAD} multiple times will not back up in the data
1016 file.  Instead, it will re-read the same line multiple times.
1017
1018 @node REPEATING DATA
1019 @section REPEATING DATA
1020 @vindex REPEATING DATA
1021
1022 @display
1023 REPEATING DATA
1024         /STARTS=start-end
1025         /OCCURS=n_occurs
1026         /FILE='file-name'
1027         /LENGTH=length
1028         /CONTINUED[=cont_start-cont_end]
1029         /ID=id_start-id_end=id_var
1030         /@{TABLE,NOTABLE@}
1031         /DATA=var_spec@dots{}
1032
1033 where each var_spec takes one of the forms
1034         var_list start-end [type_spec]
1035         var_list (fortran_spec)
1036 @end display
1037
1038 @cmd{REPEATING DATA} parses groups of data repeating in
1039 a uniform format, possibly with several groups on a single line.  Each
1040 group of data corresponds with one case.  @cmd{REPEATING DATA} may only be
1041 used within an @cmd{INPUT PROGRAM} structure (@pxref{INPUT PROGRAM}).
1042 When used with @cmd{DATA LIST}, it
1043 can be used to parse groups of cases that share a subset of variables
1044 but differ in their other data.
1045
1046 The STARTS subcommand is required.  Specify a range of columns, using
1047 literal numbers or numeric variable names.  This range specifies the
1048 columns on the first line that are used to contain groups of data.  The
1049 ending column is optional.  If it is not specified, then the record
1050 width of the input file is used.  For the inline file (@pxref{BEGIN
1051 DATA}) this is 80 columns; for a file with fixed record widths it is the
1052 record width; for other files it is 1024 characters by default.
1053
1054 The OCCURS subcommand is required.  It must be a number or the name of a
1055 numeric variable.  Its value is the number of groups present in the
1056 current record.
1057
1058 The DATA subcommand is required.  It must be the last subcommand
1059 specified.  It is used to specify the data present within each repeating
1060 group.  Column numbers are specified relative to the beginning of a
1061 group at column 1.  Data is specified in the same way as with @cmd{DATA LIST
1062 FIXED} (@pxref{DATA LIST FIXED}).
1063
1064 All other subcommands are optional.
1065
1066 FILE specifies the file to read, either a file name as a string or a
1067 file handle (@pxref{File Handles}).  If FILE is not present then the
1068 default is the last file handle used on @cmd{DATA LIST} (lexically, not in
1069 terms of flow of control).
1070
1071 By default @cmd{REPEATING DATA} will output a table describing how it will
1072 parse the input data.  Specifying NOTABLE will disable this behavior;
1073 specifying TABLE will explicitly enable it.
1074
1075 The LENGTH subcommand specifies the length in characters of each group.
1076 If it is not present then length is inferred from the DATA subcommand.
1077 LENGTH can be a number or a variable name.
1078
1079 Normally all the data groups are expected to be present on a single
1080 line.  Use the CONTINUED command to indicate that data can be continued
1081 onto additional lines.  If data on continuation lines starts at the left
1082 margin and continues through the entire field width, no column
1083 specifications are necessary on CONTINUED.  Otherwise, specify the
1084 possible range of columns in the same way as on STARTS.
1085
1086 When data groups are continued from line to line, it is easy
1087 for cases to get out of sync through careless hand editing.  The
1088 ID subcommand allows a case identifier to be present on each line of
1089 repeating data groups.  @cmd{REPEATING DATA} will check for the same
1090 identifier on each line and report mismatches.  Specify the range of
1091 columns that the identifier will occupy, followed by an equals sign
1092 (@samp{=}) and the identifier variable name.  The variable must already
1093 have been declared with @cmd{NUMERIC} or another command.
1094
1095 @cmd{REPEATING DATA} should be the last command given within an
1096 @cmd{INPUT PROGRAM}.  It should not be enclosed within a @cmd{LOOP}
1097 structure (@pxref{LOOP}).  Use @cmd{DATA LIST} before, not after,
1098 @cmd{REPEATING DATA}.
1099
1100 @node WRITE
1101 @section WRITE
1102 @vindex WRITE
1103
1104 @display
1105 WRITE 
1106         OUTFILE='file-name'
1107         RECORDS=n_lines
1108         @{NOTABLE,TABLE@}
1109         /[line_no] arg@dots{}
1110
1111 arg takes one of the following forms:
1112         'string' [start-end]
1113         var_list start-end [type_spec]
1114         var_list (fortran_spec)
1115         var_list *
1116 @end display
1117
1118 @code{WRITE} writes text or binary data to an output file.  
1119
1120 @xref{PRINT}, for more information on syntax and usage.  @cmd{PRINT}
1121 and @cmd{WRITE} differ in only a few ways:
1122
1123 @itemize @bullet
1124 @item
1125 @cmd{WRITE} uses write formats by default, whereas @cmd{PRINT} uses
1126 print formats.
1127
1128 @item
1129 @cmd{PRINT} inserts a space between variables unless a format is
1130 explicitly specified, but @cmd{WRITE} never inserts space between
1131 variables in output.
1132
1133 @item
1134 @cmd{PRINT} inserts a space at the beginning of each line that it
1135 writes to an output file (and @cmd{PRINT EJECT} inserts @samp{1} at
1136 the beginning of each line that should begin a new page), but
1137 @cmd{WRITE} does not.
1138
1139 @item
1140 @cmd{PRINT} outputs the system-missing value according to its
1141 specified output format, whereas @cmd{WRITE} outputs the
1142 system-missing value as a field filled with spaces.  Binary formats
1143 are an exception.
1144 @end itemize