Data editing > Changing the contents of a variable > Reading numeric codes into an array
 
Reading numeric codes into an array
Quick reference
To set up an array based on numeric codes in the data, type:
field array_name=column_spec [,$code$=cell_number, ...]
column_specs are references to the fields containing the numeric codes. code is a non-numeric code present in those fields and cell_number is the cell of the array which should be incremented whenever that code is encountered.
Cells in the array are reset to zero at the start of each new record. To prevent this happening, enter the statement name as fieldadd rather than field. The rest of the statement is as shown.
More information
On some studies you will find responses which are represented by numbers rather than codes. There are various methods of checking and tabulating these responses. Which one you use depends on whether you want to know the number of respondents whose record contains a given code in a field or group of fields, or the number of times a code appears in a group of fields.
To illustrate this, suppose the question and response list in the questionnaire are as follows:
Q6A: Which types of restaurant did you visit the last three
     times that you ate out?
 
(12-13)
(14-15)
(16-17)
Mexican ..................
Indian ...................
French ...................
Italian ..................
Chinese ..................
01
02
03
04
05
01
02
03
04
05
01
02
03
04
05
If you want a table which shows how many people visited each type of restaurant, you can use a fld statement in the axis which tells Quantum which columns to read and which codes represent each restaurant. For information about the fld statement, see Responses with numeric codes: fld.
Another way is to use a combination of field in the edit and bit in the axis. This is particularly efficient if, rather than wanting to count the number of people who visited each type of restaurant, you want to count the number of times each type of restaurant was visited.
The field statement counts the number of times a particular code appears in a list of fields for each respondent. It stores these counts in an integer array that consists of as many cells as there are fields to count. In the restaurant example, the array has five cells. Cell 1 holds the number of times code 01 appears in the fields c(12,13), c(14,15) and c(16,17). If the respondent visited an Italian restaurant, then Chinese, and then Italian again, their data is:
1----+----2
040504
Cell 4 (Italian) of the array is set to 2, and cell 5 (Chinese) of the array is set to 1. You can then tabulate the contents of this array using a bit statement in the axis.
The format of the field statement is:
field output_array = column_specs [,special_specs]
output_array is the name of the array in which you want to store the counts of responses. You can use spare columns in the C array, but you might find a program is easier to read if you define an integer array of your own with a name which reflects the type of information it contains. For example, if you want an integer array called restaurant, write:
int restaurants 5s
ed
field restaurants = .....
When you define the integer array, make sure that you request as many cells as there are codes in the data. In this example, there are five restaurants, so you define the array as having five cells. Quantum automatically creates an extra cell (cell 0) which it uses to count responses for which there is no cell allocated. If there were six restaurants, for example, Quantum would increment cell 0 each time it found code 06 in the restaurants columns. You can check the value of this cell as a means of reporting on invalid codes:
if (restaurants0 .gt. 0) write c(1,20) $Bad restaurant code$
Negative and zero values also cause cell zero to be incremented. Codes which are shorter than the field width are accepted as long as they are left-padded with blanks or zeros. Codes which are shorter than the field width and which are right-padded with blanks only increment cell zero.
The input_specs part of the statement defines the columns to read. You have a number of choices here:
1 List each column or field reference one after the other, separated by commas. The list must be enclosed in parentheses. In the example this would be:
field restaurants = (c(12,13), c(14,15), c(16,17))
2 If you have sequential fields as you do here, you can type the start columns of each field followed by the field length. The list of start columns is separated by commas and enclosed in parentheses, and the field length comes after the closing parenthesis and starts with a colon. To use this notation for the restaurant example, write:
field restaurants = (c12, c14, c16) :2
If you want, you can abbreviate this further by typing just the start columns of the first and last fields, followed by the field length.
field restaurants = c12, c16 :2
3 If the fields are not sequential, you list the start columns and field width of each group of columns (as shown above) and separate each group with a slash. For example, to read data from columns 12 to 17 and 52 to 57, with each field being two columns wide, you would type:
field restaurants = c12, c16 / c52, c56 :2
This reads c(12,13), c(14,15), c(16,17), c(52,53), c(54,55) and c(56,57).
You can also use this notation for single non-sequential fields. For example:
field restaurants = c23 / c36 / c71 :2
means c(23,24), c(36,37) and c(71,72).
The special_specs part of the statement is optional. You use it when a field contains non-numeric codes such as $&&$ for None of these restaurants. If you want to count codings of this type, you must remember to allocate cells in the array for each code or group of codes you want to count. Then include the notation:
$code$ = cell_number
to count those codes. For example:
int restaurants 6s
ed
field restaurants = (c12, c14, c16) :2, $&&$=6
If you want to count more than one non-numeric code, list each one individually, separated by commas.
Note To tabulate data counted by a field statement, you use a bit statement which names the integer array you have created and defines the element texts associated with each cell of the array. For more information about the bit statement, see Responses with numeric codes: bit.
Quantum normally resets the cells of the integer array to zero at the start of each record. If you want counts to continue from one record to another, use a fieldadd statement instead of field. For example:
fieldadd restaurants = (c12, c14, c16) :2
Note The advantage of using field or fieldadd is that they automatically count the number of times a code appears in a list of fields. If you want a table which uses this information, you just tell Quantum to increment the counts in the table by the values stored in the appropriate cells of the array.
You can also manipulate the values stored in the cells before you tabulate the data. For example, if you are analyzing movies that respondents have seen, and you have codes for Aliens 1, Aliens 2 and Aliens 3, you can merge them into a single cell for all Aliens movies so that the tabulation spec is easier to write.
See also
Changing the contents of a variable