Next: , Up: Data transformations   [Contents][Index]


12.1 AGGREGATE

AGGREGATE
        OUTFILE={*,’file_name’,file_handle} [MODE={REPLACE, ADDVARIABLES}]
        /PRESORTED
        /DOCUMENT
        /MISSING=COLUMNWISE
        /BREAK=var_list
        /dest_var[’label’]…=agr_func(src_vars, args…)…

AGGREGATE summarizes groups of cases into single cases. Cases are divided into groups that have the same values for one or more variables called break variables. Several functions are available for summarizing case contents.

The OUTFILE subcommand is required and must appear first. Specify a system file or portable file by file name or file handle (see File Handles), or a dataset by its name (see Datasets). The aggregated cases are written to this file. If ‘*’ is specified, then the aggregated cases replace the active dataset’s data. Use of OUTFILE to write a portable file is a PSPP extension.

If OUTFILE=* is given, then the subcommand MODE may also be specified. The mode subcommand has two possible values: ADDVARIABLES or REPLACE. In REPLACE mode, the entire active dataset is replaced by a new dataset which contains just the break variables and the destination varibles. In this mode, the new file contains as many cases as there are unique combinations of the break variables. In ADDVARIABLES mode, the destination variables are appended to the existing active dataset. Cases which have identical combinations of values in their break variables, receive identical values for the destination variables. The number of cases in the active dataset remains unchanged. Note that if ADDVARIABLES is specified, then the data must be sorted on the break variables.

By default, the active dataset is sorted based on the break variables before aggregation takes place. If the active dataset is already sorted or otherwise grouped in terms of the break variables, specify PRESORTED to save time. PRESORTED is assumed if MODE=ADDVARIABLES is used.

Specify DOCUMENT to copy the documents from the active dataset into the aggregate file (see DOCUMENT). Otherwise, the aggregate file does not contain any documents, even if the aggregate file replaces the active dataset.

Normally, only a single case (for SD and SD., two cases) need be non-missing in each group for the aggregate variable to be non-missing. Specifying /MISSING=COLUMNWISE inverts this behavior, so that the aggregate variable becomes missing if any aggregated value is missing.

If PRESORTED, DOCUMENT, or MISSING are specified, they must appear between OUTFILE and BREAK.

At least one break variable must be specified on BREAK, a required subcommand. The values of these variables are used to divide the active dataset into groups to be summarized. In addition, at least one dest_var must be specified.

One or more sets of aggregation variables must be specified. Each set comprises a list of aggregation variables, an equals sign (‘=’), the name of an aggregation function (see the list below), and a list of source variables in parentheses. Some aggregation functions expect additional arguments following the source variable names.

Aggregation variables typically are created with no variable label, value labels, or missing values. Their default print and write formats depend on the aggregation function used, with details given in the table below. A variable label for an aggregation variable may be specified just after the variable’s name in the aggregation variable list.

Each set must have exactly as many source variables as aggregation variables. Each aggregation variable receives the results of applying the specified aggregation function to the corresponding source variable. The MEAN, MEDIAN, SD, and SUM aggregation functions may only be applied to numeric variables. All the rest may be applied to numeric and string variables.

The available aggregation functions are as follows:

FGT(var_name, value)

Fraction of values greater than the specified constant. The default format is F5.3.

FIN(var_name, low, high)

Fraction of values within the specified inclusive range of constants. The default format is F5.3.

FLT(var_name, value)

Fraction of values less than the specified constant. The default format is F5.3.

FIRST(var_name)

First non-missing value in break group. The aggregation variable receives the complete dictionary information from the source variable. The sort performed by AGGREGATE (and by SORT CASES) is stable. This means that the first case with particular values for the break variables before sorting is also the first case in that break group after sorting.

FOUT(var_name, low, high)

Fraction of values strictly outside the specified range of constants. The default format is F5.3.

LAST(var_name)

Last non-missing value in break group. The aggregation variable receives the complete dictionary information from the source variable. The sort performed by AGGREGATE (and by SORT CASES) is stable.

MAX(var_name)

Maximum value. The aggregation variable receives the complete dictionary information from the source variable.

MEAN(var_name)

Arithmetic mean. Limited to numeric values. The default format is F8.2.

MEDIAN(var_name)

The median value. Limited to numeric values. The default format is F8.2.

MIN(var_name)

Minimum value. The aggregation variable receives the complete dictionary information from the source variable.

N(var_name)

Number of non-missing values. The default format is F7.0 if weighting is not enabled, F8.2 if it is (see WEIGHT).

N

Number of cases aggregated to form this group. The default format is F7.0 if weighting is not enabled, F8.2 if it is (see WEIGHT).

NMISS(var_name)

Number of missing values. The default format is F7.0 if weighting is not enabled, F8.2 if it is (see WEIGHT).

NU(var_name)

Number of non-missing values. Each case is considered to have a weight of 1, regardless of the current weighting variable (see WEIGHT). The default format is F7.0.

NU

Number of cases aggregated to form this group. Each case is considered to have a weight of 1, regardless of the current weighting variable. The default format is F7.0.

NUMISS(var_name)

Number of missing values. Each case is considered to have a weight of 1, regardless of the current weighting variable. The default format is F7.0.

PGT(var_name, value)

Percentage between 0 and 100 of values greater than the specified constant. The default format is F5.1.

PIN(var_name, low, high)

Percentage of values within the specified inclusive range of constants. The default format is F5.1.

PLT(var_name, value)

Percentage of values less than the specified constant. The default format is F5.1.

POUT(var_name, low, high)

Percentage of values strictly outside the specified range of constants. The default format is F5.1.

SD(var_name)

Standard deviation of the mean. Limited to numeric values. The default format is F8.2.

SUM(var_name)

Sum. Limited to numeric values. The default format is F8.2.

Aggregation functions compare string values in terms of internal character codes. On most modern computers, this is ASCII or a superset thereof.

The aggregation functions listed above exclude all user-missing values from calculations. To include user-missing values, insert a period (‘.’) at the end of the function name. (e.g.SUM.’). (Be aware that specifying such a function as the last token on a line causes the period to be interpreted as the end of the command.)

AGGREGATE both ignores and cancels the current SPLIT FILE settings (see SPLIT FILE).

12.1.1 Aggregate Example

The personnel.sav dataset provides the occupations and salaries of many individuals. For many purposes however such detailed information is not interesting, but often the aggregated statistics of each occupation are of interest. In Example 12.1 the AGGREGATE command is used to calculate the mean, the median and the standard deviation of each occupation.

get file="personnel.sav".

aggregate outfile=* 
        mode = replace
        /break= occupation
        /occ_mean_salaray = mean (salary)
        /occ_median_salary = median (salary)
        /occ_std_dev_salary = sd (salary).

list.

Example 12.1: Calculating aggregated statistics from the personnel.sav file.

Since we chose the ‘MODE = REPLACE’ option, in Results 12.1 cases for the individual persons are no longer present. They have each been replaced by a single case per aggregated value.

Data List
occupation occ_mean_salaray occ_median_salary occ_std_dev_salary
Artist 37836.18 34712.50 7631.48
Baker 45075.20 45075.20 4411.21
Barrister 39504.00 39504.00 .
Carpenter 39349.11 36190.04 7453.40
Cleaner 41142.50 39647.49 14378.98
Cook 40357.79 43194.00 11064.51
Manager 46452.14 45657.56 6901.69
Mathematician 34531.06 34763.06 5267.68
Painter 45063.55 45063.55 15159.67
Payload Specialist 34355.72 34355.72 .
Plumber 40413.91 40410.00 4726.05
Scientist 36687.07 36803.83 10873.54
Scrientist 42530.65 42530.65 .
Tailor 34586.79 34586.79 3728.98

Results 12.1: Aggregated mean, median and standard deviation per occupation.

Note that some values for the standard deviation are blank. This is because there is only one case with the respective occupation.


Next: AUTORECODE, Up: Data transformations   [Contents][Index]