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