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