AGGREGATE's sort is stable.
[pspp-builds.git] / doc / transformation.texi
1 @node Data Manipulation, Data Selection, Variable Attributes, Top
2 @chapter Data transformations
3 @cindex transformations
4
5 The PSPP procedures examined in this chapter manipulate data and
6 prepare the active file for later analyses.  They do not produce output,
7 as a rule.
8
9 @menu
10 * AGGREGATE::                   Summarize multiple cases into a single case.
11 * AUTORECODE::                  Automatic recoding of variables.
12 * COMPUTE::                     Assigning a variable a calculated value.
13 * COUNT::                       Counting variables with particular values.
14 * FLIP::                        Exchange variables with cases.
15 * IF::                          Conditionally assigning a calculated value.
16 * RECODE::                      Mapping values from one set to another.
17 * SORT CASES::                  Sort the active file.
18 @end menu
19
20 @node AGGREGATE, AUTORECODE, Data Manipulation, Data Manipulation
21 @section AGGREGATE
22 @vindex AGGREGATE
23
24 @display
25 AGGREGATE 
26         OUTFILE=@{*,'filename'@}          
27         /PRESORTED
28         /DOCUMENT
29         /MISSING=COLUMNWISE
30         /BREAK=var_list
31         /dest_var['label']@dots{}=agr_func(src_vars, args@dots{})@dots{}
32 @end display
33
34 @cmd{AGGREGATE} summarizes groups of cases into single cases.
35 Cases are divided into groups that have the same values for one or more
36 variables called @dfn{break variables}.  Several functions are available
37 for summarizing case contents.
38
39 The OUTFILE subcommand is required and must appear first.  Specify a
40 system file by file name string or file handle (@pxref{FILE HANDLE}).
41 The aggregated cases are written to this file.  If @samp{*} is
42 specified, then the aggregated cases replace the active file.
43
44 By default, the active file will be sorted based on the break variables
45 before aggregation takes place.  If the active file is already sorted
46 or otherwise grouped in terms of the break variables, specify
47 PRESORTED to save time.
48
49 Specify DOCUMENT to copy the documents from the active file into the
50 aggregate file (@pxref{DOCUMENT}).  Otherwise, the aggregate file will
51 not contain any documents, even if the aggregate file replaces the
52 active file.
53
54 Normally, only a single case (for SD and SD., two cases) need be
55 non-missing in each group for the aggregate variable to be
56 non-missing.  Specifying /MISSING=COLUMNWISE inverts this behavior, so
57 that the aggregate variable becomes missing if any aggregated value is
58 missing.
59
60 If PRESORTED, DOCUMENT, or MISSING are specified, they must appear
61 between OUTFILE and BREAK.
62
63 At least one break variable must be specified on BREAK, a
64 required subcommand.  The values of these variables are used to divide
65 the active file into groups to be summarized.  In addition, at least
66 one @var{dest_var} must be specified.
67
68 One or more sets of aggregation variables must be specified.  Each set
69 comprises a list of aggregation variables, an equals sign (@samp{=}),
70 the name of an aggregation function (see the list below), and a list
71 of source variables in parentheses.  Some aggregation functions expect
72 additional arguments following the source variable names.
73
74 Aggregation variables typically are created with no variable label,
75 value labels, or missing values.  Their default print and write
76 formats depend on the aggregation function used, with details given in
77 the table below.  A variable label for an aggregation variable may be
78 specified just after the variable's name in the aggregation variable
79 list.
80
81 Each set must have exactly as many source variables as aggregation
82 variables.  Each aggregation variable receives the results of applying
83 the specified aggregation function to the corresponding source
84 variable.  The MEAN, SD, and SUM aggregation functions may only be
85 applied to numeric variables.  All the rest may be applied to numeric
86 and short and long string variables.
87
88 The available aggregation functions are as follows:
89
90 @table @asis
91 @item FGT(var_name, value)
92 Fraction of values greater than the specified constant.  The default
93 format is F5.3.
94
95 @item FIN(var_name, low, high)
96 Fraction of values within the specified inclusive range of constants.
97 The default format is F5.3.
98
99 @item FLT(var_name, value)
100 Fraction of values less than the specified constant.  The default
101 format is F5.3.
102
103 @item FIRST(var_name)
104 First non-missing value in break group.  The aggregation variable
105 receives the complete dictionary information from the source variable.
106 The sort performed by AGGREGATE (and by SORT CASES) is stable, so that
107 the first case with particular values for the break variables before
108 sorting will also be the first case in that break group after sorting.
109
110 @item FOUT(var_name, low, high)
111 Fraction of values strictly outside the specified range of constants.
112 The default format is F5.3.
113
114 @item LAST(var_name)
115 Last non-missing value in break group.  The aggregation variable
116 receives the complete dictionary information from the source variable.
117 The sort performed by AGGREGATE (and by SORT CASES) is stable, so that
118 the last case with particular values for the break variables before
119 sorting will also be the last case in that break group after sorting.
120
121 @item MAX(var_name)
122 Maximum value.  The aggregation variable receives the complete
123 dictionary information from the source variable.
124
125 @item MEAN(var_name)
126 Arithmetic mean.  Limited to numeric values.  The default format is
127 F8.2.
128
129 @item MIN(var_name)
130 Minimum value.  The aggregation variable receives the complete
131 dictionary information from the source variable.
132
133 @item N(var_name)
134 Number of non-missing values.  The default format is F7.0 if weighting
135 is not enabled, F8.2 if it is (@pxref{WEIGHT}).
136
137 @item N
138 Number of cases aggregated to form this group.  The default format is
139 F7.0 if weighting is not enabled, F8.2 if it is (@pxref{WEIGHT}).
140
141 @item NMISS(var_name)
142 Number of missing values.  The default format is F7.0 if weighting is
143 not enabled, F8.2 if it is (@pxref{WEIGHT}).
144
145 @item NU(var_name)
146 Number of non-missing values.  Each case is considered to have a weight
147 of 1, regardless of the current weighting variable (@pxref{WEIGHT}).
148 The default format is F7.0.
149
150 @item NU
151 Number of cases aggregated to form this group.  Each case is considered
152 to have a weight of 1, regardless of the current weighting variable.
153 The default format is F7.0.
154
155 @item NUMISS(var_name)
156 Number of missing values.  Each case is considered to have a weight of
157 1, regardless of the current weighting variable.  The default format is F7.0.
158
159 @item PGT(var_name, value)
160 Percentage between 0 and 100 of values greater than the specified
161 constant.  The default format is F5.1.
162
163 @item PIN(var_name, low, high)
164 Percentage of values within the specified inclusive range of
165 constants.  The default format is F5.1.
166
167 @item PLT(var_name, value)
168 Percentage of values less than the specified constant.  The default
169 format is F5.1.
170
171 @item POUT(var_name, low, high)
172 Percentage of values strictly outside the specified range of
173 constants.  The default format is F5.1.
174
175 @item SD(var_name)
176 Standard deviation of the mean.  Limited to numeric values.  The
177 default format is F8.2.
178
179 @item SUM(var_name)
180 Sum.  Limited to numeric values.  The default format is F8.2.
181 @end table
182
183 Aggregation functions compare string values in terms of internal
184 character codes.  On most modern computers, this is a form of ASCII.
185
186 The aggregation functions listed above exclude all user-missing values
187 from calculations.  To include user-missing values, insert a period
188 (@samp{.}) at the end of the function name.  (e.g.@: @samp{SUM.}).
189 (Be aware that specifying such a function as the last token on a line
190 will cause the period to be interpreted as the end of the command.)
191
192 @cmd{AGGREGATE} both ignores and cancels the current @cmd{SPLIT FILE}
193 settings (@pxref{SPLIT FILE}).
194
195 @node AUTORECODE, COMPUTE, AGGREGATE, Data Manipulation
196 @section AUTORECODE
197 @vindex AUTORECODE
198
199 @display
200 AUTORECODE VARIABLES=src_vars INTO dest_vars
201         /DESCENDING
202         /PRINT
203 @end display
204
205 The @cmd{AUTORECODE} procedure considers the @var{n} values that a variable
206 takes on and maps them onto values 1@dots{}@var{n} on a new numeric
207 variable.
208
209 Subcommand VARIABLES is the only required subcommand and must come
210 first.  Specify VARIABLES, an equals sign (@samp{=}), a list of source
211 variables, INTO, and a list of target variables.  There must the same
212 number of source and target variables.  The target variables must not
213 already exist.
214
215 By default, increasing values of a source variable (for a string, this
216 is based on character code comparisons) are recoded to increasing values
217 of its target variable.  To cause increasing values of a source variable
218 to be recoded to decreasing values of its target variable (@var{n} down
219 to 1), specify DESCENDING.
220
221 PRINT is currently ignored.
222
223 @cmd{AUTORECODE} is a procedure.  It causes the data to be read.
224
225 @node COMPUTE, COUNT, AUTORECODE, Data Manipulation
226 @section COMPUTE
227 @vindex COMPUTE
228
229 @display
230 COMPUTE variable = expression.
231   or
232 COMPUTE vector(index) = expression.
233 @end display
234
235 @cmd{COMPUTE} assigns the value of an expression to a target
236 variable.  For each case, the expression is evaluated and its value
237 assigned to the target variable.  Numeric and short and long string
238 variables may be assigned.  When a string expression's width differs
239 from the target variable's width, the string result of the expression
240 is truncated or padded with spaces on the right as necessary.  The
241 expression and variable types must match.
242
243 For numeric variables only, the target variable need not already
244 exist.  Numeric variables created by @cmd{COMPUTE} are assigned an
245 @code{F8.2} output format.  String variables must be declared before
246 they can be used as targets for @cmd{COMPUTE}.
247
248 The target variable may be specified as an element of a vector
249 (@pxref{VECTOR}).  In this case, a vector index expression must be
250 specified in parentheses following the vector name.  The index
251 expression must evaluate to a numeric value that, after rounding down
252 to the nearest integer, is a valid index for the named vector.
253
254 Using @cmd{COMPUTE} to assign to a variable specified on @cmd{LEAVE}
255 (@pxref{LEAVE}) resets the variable's left state.  Therefore,
256 @code{LEAVE} should be specified following @cmd{COMPUTE}, not before.
257
258 @cmd{COMPUTE} is a transformation.  It does not cause the active file to be
259 read.
260
261 When @cmd{COMPUTE} is specified following @cmd{TEMPORARY}
262 (@pxref{TEMPORARY}), the @cmd{LAG} function may not be used
263 (@pxref{LAG}).
264
265 @node COUNT, FLIP, COMPUTE, Data Manipulation
266 @section COUNT
267 @vindex COUNT
268
269 @display
270 COUNT var_name = var@dots{} (value@dots{}).
271
272 Each value takes one of the following forms:
273         number
274         string
275         num1 THRU num2
276         MISSING
277         SYSMIS
278 In addition, num1 and num2 can be LO or LOWEST, or HI or HIGHEST,
279 respectively.
280 @end display
281
282 @cmd{COUNT} creates or replaces a numeric @dfn{target} variable that
283 counts the occurrence of a @dfn{criterion} value or set of values over
284 one or more @dfn{test} variables for each case.
285
286 The target variable values are always nonnegative integers.  They are
287 never missing.  The target variable is assigned an F8.2 output format.
288 @xref{Input/Output Formats}.  Any variables, including long and short
289 string variables, may be test variables.
290
291 User-missing values of test variables are treated just like any other
292 values.  They are @strong{not} treated as system-missing values.
293 User-missing values that are criterion values or inside ranges of
294 criterion values are counted as any other values.  However (for numeric
295 variables), keyword MISSING may be used to refer to all system-
296 and user-missing values.
297
298 @cmd{COUNT} target variables are assigned values in the order
299 specified.  In the command @code{COUNT A=A B(1) /B=A B(2).}, the
300 following actions occur:
301
302 @itemize @minus
303 @item
304 The number of occurrences of 1 between @code{A} and @code{B} is counted.
305
306 @item
307 @code{A} is assigned this value.
308
309 @item
310 The number of occurrences of 1 between @code{B} and the @strong{new}
311 value of @code{A} is counted.
312
313 @item
314 @code{B} is assigned this value.
315 @end itemize
316
317 Despite this ordering, all @cmd{COUNT} criterion variables must exist
318 before the procedure is executed---they may not be created as target
319 variables earlier in the command!  Break such a command into two
320 separate commands.
321
322 The examples below may help to clarify.
323
324 @enumerate A
325 @item
326 Assuming @code{Q0}, @code{Q2}, @dots{}, @code{Q9} are numeric variables,
327 the following commands:
328
329 @enumerate
330 @item
331 Count the number of times the value 1 occurs through these variables
332 for each case and assigns the count to variable @code{QCOUNT}.  
333
334 @item
335 Print out the total number of times the value 1 occurs throughout
336 @emph{all} cases using @cmd{DESCRIPTIVES}.  @xref{DESCRIPTIVES}, for
337 details.
338 @end enumerate
339
340 @example
341 COUNT QCOUNT=Q0 TO Q9(1).
342 DESCRIPTIVES QCOUNT /STATISTICS=SUM.
343 @end example
344
345 @item
346 Given these same variables, the following commands:
347
348 @enumerate
349 @item
350 Count the number of valid values of these variables for each case and
351 assigns the count to variable @code{QVALID}.
352
353 @item
354 Multiplies each value of @code{QVALID} by 10 to obtain a percentage of
355 valid values, using @cmd{COMPUTE}.  @xref{COMPUTE}, for details.
356
357 @item
358 Print out the percentage of valid values across all cases, using
359 @cmd{DESCRIPTIVES}.  @xref{DESCRIPTIVES}, for details.
360 @end enumerate
361
362 @example
363 COUNT QVALID=Q0 TO Q9 (LO THRU HI).
364 COMPUTE QVALID=QVALID*10.
365 DESCRIPTIVES QVALID /STATISTICS=MEAN.
366 @end example
367 @end enumerate
368
369 @node FLIP, IF, COUNT, Data Manipulation
370 @section FLIP
371 @vindex FLIP
372
373 @display
374 FLIP /VARIABLES=var_list /NEWNAMES=var_name.
375 @end display
376
377 @cmd{FLIP} transposes rows and columns in the active file.  It
378 causes cases to be swapped with variables, and vice versa.
379
380 All variables in the transposed active file are numeric.  String
381 variables take on the system-missing value in the transposed file.
382
383 No subcommands are required.  If specified, the VARIABLES subcommand
384 selects variables to be transformed into cases, and variables not
385 specified are discarded.  If the VARIABLES subcommand is omitted, all
386 variables are selected for transposition.
387
388 The variables specified by NEWNAMES, which must be a string variable, is
389 used to give names to the variables created by @cmd{FLIP}.  If
390 NEWNAMES is not
391 specified then the default is a variable named CASE_LBL, if it exists.
392 If it does not then the variables created by FLIP are named VAR000
393 through VAR999, then VAR1000, VAR1001, and so on.
394
395 When a NEWNAMES variable is available, the names must be canonicalized
396 before becoming variable names.  Invalid characters are replaced by
397 letter @samp{V} in the first position, or by @samp{_} in subsequent
398 positions.  If the name thus generated is not unique, then numeric
399 extensions are added, starting with 1, until a unique name is found or
400 there are no remaining possibilities.  If the latter occurs then the
401 FLIP operation aborts.
402
403 The resultant dictionary contains a CASE_LBL variable, which stores the
404 names of the variables in the dictionary before the transposition.  If
405 the active file is subsequently transposed using @cmd{FLIP}, this
406 variable can
407 be used to recreate the original variable names.
408
409 FLIP honors @cmd{N OF CASES} (@pxref{N OF CASES}).  It ignores
410 @cmd{TEMPORARY} (@pxref{TEMPORARY}), so that ``temporary''
411 transformations become permanent.
412
413 @node IF, RECODE, FLIP, Data Manipulation
414 @section IF
415 @vindex IF
416
417 @display
418 IF condition variable=expression.
419   or
420 IF condition vector(index)=expression.
421 @end display
422
423 The @cmd{IF} transformation conditionally assigns the value of a target
424 expression to a target variable, based on the truth of a test
425 expression.
426
427 Specify a boolean-valued expression (@pxref{Expressions}) to be tested
428 following the IF keyword.  This expression is evaluated for each case.
429 If the value is true, then the value of the expression is computed and
430 assigned to the specified variable.  If the value is false or missing,
431 nothing is done.  Numeric and short and long string variables may be
432 assigned.  When a string expression's width differs from the target
433 variable's width, the string result of the expression is truncated or
434 padded with spaces on the right as necessary.  The expression and
435 variable types must match.
436
437 The target variable may be specified as an element of a vector
438 (@pxref{VECTOR}).  In this case, a vector index expression must be
439 specified in parentheses following the vector name.  The index
440 expression must evaluate to a numeric value that, after rounding down
441 to the nearest integer, is a valid index for the named vector.
442
443 Using @cmd{IF} to assign to a variable specified on @cmd{LEAVE}
444 (@pxref{LEAVE}) resets the variable's left state.  Therefore,
445 @code{LEAVE} should be specified following @cmd{IF}, not before.
446
447 When @cmd{IF} is specified following @cmd{TEMPORARY}
448 (@pxref{TEMPORARY}), the @cmd{LAG} function may not be used
449 (@pxref{LAG}).
450
451 @node RECODE, SORT CASES, IF, Data Manipulation
452 @section RECODE
453 @vindex RECODE
454
455 @display
456 RECODE var_list (src_value@dots{}=dest_value)@dots{} [INTO var_list].
457
458 src_value may take the following forms:
459         number
460         string
461         num1 THRU num2
462         MISSING
463         SYSMIS
464         ELSE
465 Open-ended ranges may be specified using LO or LOWEST for num1
466 or HI or HIGHEST for num2.
467
468 dest_value may take the following forms:
469         num
470         string
471         SYSMIS
472         COPY
473 @end display
474
475 @cmd{RECODE} translates data from one range of values to
476 another, via flexible user-specified mappings.  Data may be remapped
477 in-place or copied to new variables.  Numeric, short string, and long
478 string data can be recoded.
479
480 Specify the list of source variables, followed by one or more mapping
481 specifications each enclosed in parentheses.  If the data is to be
482 copied to new variables, specify INTO, then the list of target
483 variables.  String target variables must already have been declared
484 using @cmd{STRING} or another transformation, but numeric target
485 variables can
486 be created on the fly.  There must be exactly as many target variables
487 as source variables.  Each source variable is remapped into its
488 corresponding target variable.
489
490 When INTO is not used, the input and output variables must be of the
491 same type.  Otherwise, string values can be recoded into numeric values,
492 and vice versa.  When this is done and there is no mapping for a
493 particular value, either a value consisting of all spaces or the
494 system-missing value is assigned, depending on variable type.
495
496 Mappings are considered from left to right.  The first src_value that
497 matches the value of the source variable causes the target variable to
498 receive the value indicated by the dest_value.  Literal number, string,
499 and range src_value's should be self-explanatory.  MISSING as a
500 src_value matches any user- or system-missing value.  SYSMIS matches the
501 system missing value only.  ELSE is a catch-all that matches anything.
502 It should be the last src_value specified.
503
504 Numeric and string dest_value's should also be self-explanatory.  COPY
505 causes the input values to be copied to the output.  This is only value
506 if the source and target variables are of the same type.  SYSMIS
507 indicates the system-missing value.
508
509 If the source variables are strings and the target variables are
510 numeric, then there is one additional mapping available: (CONVERT),
511 which must be the last specified mapping.  CONVERT causes a number
512 specified as a string to be converted to a numeric value.  If the string
513 cannot be parsed as a number, then the system-missing value is assigned.
514
515 Multiple recodings can be specified on a single @cmd{RECODE} invocation.
516 Introduce additional recodings with a slash (@samp{/}) to
517 separate them from the previous recodings.
518
519 @node SORT CASES,  , RECODE, Data Manipulation
520 @section SORT CASES
521 @vindex SORT CASES
522
523 @display
524 SORT CASES BY var_list.
525 @end display
526
527 @cmd{SORT CASES} sorts the active file by the values of one or more
528 variables.
529
530 Specify BY and a list of variables to sort by.  By default, variables
531 are sorted in ascending order.  To override sort order, specify (D) or
532 (DOWN) after a list of variables to get descending order, or (A) or (UP)
533 for ascending order.  These apply to the entire list of variables
534 preceding them.
535
536 The sort algorithms used by @cmd{SORT CASES} are stable.  That is,
537 records that have equal values of the sort variables will have the
538 same relative order before and after sorting.  As a special case,
539 re-sorting an already sorted file will not affect the ordering of
540 cases.
541
542 @cmd{SORT CASES} is a procedure.  It causes the data to be read.
543
544 @cmd{SORT CASES} attempts to sort the entire active file in main memory.
545 If workspace is exhausted, it falls back to a merge sort algorithm that
546 involves creates numerous temporary files.
547
548 @cmd{SORT CASES} may not be specified following TEMPORARY.  
549 @setfilename ignored