• No results found

Assignment statements

In document Quantum Vol1 (Page 103-116)

An assignment statement normally means ‘put the specified information into the given variable overwriting anything already in that variable’. It can be used with any type of variable to perform any of the following tasks:

• To copy codes from one column into another.

• To replace certain codes in one column with those from a second column.

• To assign the value of an arithmetic expression to a variable.

To copy codes from groups of columns into another column using the logical operators and, or and xor.

In spite of the diversity of these functions the basic format of any assignment statement is:

variable=item

where item defines what is to be copied into the variable.

Remember that comments can be identified by an uppercase C in column 1. If the first variable in your statement starts with a C, make sure that you type it in lower case otherwise the whole line will be read as a comment and will be ignored. For example:

Alternatively, you may precede assignment statements with the word set, thus:

set c(15,16)=$12$

c(15,16)=$12$ is correct, but

C(15,16)=$12$ is read as a comment even though the syntax is correct

Copying codes

Quick Reference

To copy codes into a single data variable, overwriting the variable’s original contents, type:

variable=’codes’

To copy a string of codes into a field, type:

var_name(start,end)=$codes$

To copy the contents of one variable or field into another, type:

variable1 = variable2

Assignment statements are most commonly used to copy codes into a column or to copy the contents of one variable into another. For instance:

c121=’159’

c121=c134

In the first example we are copying the codes 1, 5 and 9 into column 121 overwriting whatever is already there. The second example copies everything in column 134 into column 121, again overwriting what was originally there. Column 134 remains unchanged.

You can also copy strings of characters into fields of columns. Let’s say we want to copy the code 59642 into columns 76 to 80 of card 3; we would write:

c(376,380)=$59642$

Notice that the characters to be copied into the array are enclosed in dollar signs as is the rule when dealing with strings.

If you need to use a semicolon in a string, you must type it as:

\;

Quantum uses a semicolon to mark the end of a statement, and will issue an error message if it finds a semicolon by itself in the middle of a string. The backslash in front of the semicolon tells Quantum to read the next character as an ordinary character with no special meaning. For example:

c(376,380)=$59\;42$

inserts 59 in c(376,377), a semicolon in column 378, and 42 in c(379,380).

When characters are being copied into columns, the equals sign may be omitted:

Just as the contents of a single column can be copied into another, so the contents of one field can be copied into another field. For example:

copies the contents of c(70,79) into c(10,19) and the contents of c(45,47) into c(20,22), in both cases overwriting the original contents of those columns.

Data variables in assignment statements may be subscripted. The following are valid:

c(t1)=c145

c(178,180)=c(t4,t5) c(t3,t5)=c(t10,t10+2)

The equals sign in these kinds of operation may not be omitted.

When subscripting columns, remember that the current values of the integer variables will be substituted in the expression before the statement itself is executed. If t3=120 and t10=240, the statement:

c(t3,t3+2)=c(t10,t10+2) means:

c(120,122)=c(240,242)

Generally you will know how many characters are required to hold the information they will receive, but this is not always the case. What if the field on the left of the equals sign is longer than the string to be copied into it? Quantum always copies a string starting with the right-most column and transferring it into the right-most column of the field. It continues in this way until all characters have been copied, then if there are still columns left in the field they are reset to blanks.

When strings are copied in this way they are called ‘right-justified and blank-padded’.

c10’4’ is the same as c10=’4’

c(11,14)$6353$ is the same as c(11,14)=$6353$

c(10,19)=c(70,79) or c(20,22)=c(45,47)

Let’s clarify this with a couple of examples. Suppose we have:

----+----9--- ... ---4----+----5 100 84635 and we enter:

c(241,245)=c(185,187) We will then have

----+----9--- ... ---4----+----5 100 100

If there are fewer characters than there are columns in the field, the characters are right-justified in the field with the remaining columns set to blanks. If the reverse is true, and there are more characters than there are columns in the field, the error message ‘Attempt to set too many columns into too few columns’ is issued.

Columns in assignment statements may overlap; for instance:

c(145,150)=c(143,148)

copies the contents of columns 143 to 148 into columns 145 to 150, so:

----+----5 ----+----5 83645902 becomes 83836459

When a field is set to blanks it is never wrong to type in as many blanks (enclosed in dollar signs) as there are columns in the field, but it is much quicker and more efficient to type, say:

c(301,380)=$ $

Partial column replacement

Quick Reference

To replace a code or set of codes in one data variable with a code or set of codes in a second data variable, type:

variable1’codes1’=variable2’codes2’

codes1 and codes2 must contain the same number of codes, and the codes must be in superimposable order (e.g., ‘123’ and ‘456’, but not ‘123’ and ‘135’).

Assignment statements are also used to replace parts of one column with those of another, leaving the remaining contents of that column intact. Note that this is the only time that assignment does not overwrite everything in the recipient variable. Let’s start with a simple example. Suppose we have:

----+----3 ... ----+----6 3 6 / / 7 8

and we want column 124 to contain a ‘1’ only if column 159 contains a ‘7’. We would write:

c124’1’=c159’7’

Once this statement has been executed, we will have:

----+----3 ... ----+----6 1 6 3 / / 8 7

However, if we wrote:

c124’3’=c159’3’

meaning that c124 should only contain a ‘3’ if c159 contains a ‘3’, Quantum would give us:

----+----3 ... ----+----6 4 6 / / 7 8

As you can see, the ‘3’ in c124 has been deleted because there is no ‘3’ in c159. Both examples could equally well be written using if, else, emit and delete, but an assignment statement is much more efficient when you have a set of codes to check for.

For further information about if, see section 9.1, ‘Statements of condition – if’.

For further information about else, see section 9.2, ‘Statements of condition – else’.

For further information about emit, see section 8.2, ‘Adding codes into a column’.

For further information about delete, see section 8.3, ‘Deleting codes from a column’.

Let’s say we type:

c10’123’=c11’456’

and our data is:

+----1----+

14 35 4

Quantum will give us:

+----1----+

14 25 4

Column 10 contains a ‘1’ and a ‘2’ because c11 contains a ‘4’ and a ‘5’. The ‘3’ that was originally there has been removed because there was no ‘6’ in c11. The ‘4’ in column 10 remains untouched because it has no corresponding code in c11.

Partial assignment need not have different column numbers either side of the equals sign. Quantum accepts statements of the form:

c127’0/3’ = c127’1/4’

which can be used for recoding incorrectly coded data. The example we have used will recode a

‘0’ in column 127 as a ‘1’, a ‘1’ in column 127 as a ‘2’, and so on.

When entering codes with this type of statement, make sure that there are the same number of codes on either side of the equals sign and that they are in the same relative positions in the order

&-0123456789. In the previous example we used ‘123’ and ‘456’. We could also have used ‘&-1’,

‘789’ or ‘234’ instead of ‘456’, to name but a few alternatives. The important thing is that the two groups follow the same pattern: if the first set names alternate codes (for example, ‘1357’) then so must the second (for example, ‘&024’).

The following statements are valid:

c21’&–0’=c92’456’

c21’05’=c86’49’

these are not:

c56’ 0’=c91’15’

c78’123’=c81’367’

The statement for columns 56 and 91 is incorrect because blank is not a valid code here; the statement for columns 78 and 81 is wrong because the codes ‘367’ cannot be superimposed on

‘123’ (either 345 or 567 would be correct).

Storing arithmetic values

Quick Reference

To store the value of an arithmetic expression in a variable, type:

variable = expression

To copy a real value into a data variable, type:

var_name(start,end) :dp = expression where dp is the number of decimal places required.

In many of your Quantum programs you will need to save the result of some arithmetic expression in a variable. The variable may be a column or an integer or real variable and the arithmetic information may be the contents of a column, integer or real variable, an integer or real number, or the results of the functions numb or random. It can also include arithmetic expressions which have been manipulated using the arithmetic operators +, −, / and *. Here are some examples to start with:

var1=100

/* Next statement expects that variable ntim is < 10 c135=ntim

/* In next example, if c31’5678’, variable np=4 np=numb(c31)

/* Increment rect (record total) by 1 for each record processed rect=rect+1

Copying a number into an integer or real variable is easy because the variable has no predetermined size — that is, Quantum does not say that such variables may only store numbers of up to, say, three digits. Integer variables can store any whole number in the range +2,147,483,648 to -2,147,483,647 and real variables may take values of any magnitude with six digits accuracy.

Suppose our questionnaire tells us how many pints of milk a respondent bought and we want to save this is in an integer variable called npt. Here’s what we might write:

npt=c(125,126)

Similarly, if we know how many miles the respondent travels to work each day, and we want to convert this to kilometers, we could save the conversion in a real variable called km0:

If the respondent travels 5 miles, km will have the value 8.045, but if he or she travels 9 miles, km would be 14.481.

The main difference between the two examples is the type of variable in which the results are saved.

The number of pints bought will always be a whole number so we save it in an integer variable, whereas the conversion from miles to kilometers is likely to produce a real number so we save it in a real variable.

Real and integer variables

When copying a real value into an integer variable or vice versa, remember that the accuracy of the result depends upon the type of variable in which the value is saved. Real values saved in integer variables are truncated before the decimal point, thus:

but integer values placed in a real variable are saved as reals with decimal places and accuracy to 6 significant figures:

Integer variables are often used to count the number of respondents having a specific characteristic.

For instance, to count the number of respondents holidaying at home and the number taking holidays abroad we can say,

/* Home is c113’1’; abroad is c113’2’; both is c113’12’

if (c113’1’) home=home+1 if (c113’2’) abroad=abroad+1

This example uses the if statement that is described in chapter 9, ‘Flow control’.

Whenever a record is read with c113’1’, the variable home will be incremented by one and whenever a record is read with c113’2’ the variable abroad will be increased by 1.

Let’s say we have five respondents who took the following holidays:

t1=2.5 + 3.4 gives t1=5

x1=1 + 7 gives x1=8.0

Respondent 1 Home c113’1’

Respondent 2 Home and abroad c113’12’

Respondent 3 Home c113’1’

Respondent 4 No holiday c113’ ’

Respondent 5 Abroad c113’2’

At the start of the run, the variables home and abroad are both zero. After these records have been processed, home will equal 3 and abroad will be 2. The person unlucky enough to have no holiday at all will be ignored.

In the example above we were accumulating information about holiday habits for all respondents together, but on many occasions you will want to store information on a per respondent basis instead. Normally, integer and real variables are not reset between respondents, but all you need do to overcome this is to enter a statement at the start of your edit to reset the variable in question to zero each time a new record is read. For instance:

home=0

We will discuss in more detail the times when you might want to do this when we describe the do statement in section 9.5, ‘Loops’.

Columns which contain single codes may be treated as a whole number. For instance, if our data is:

+----2----+

4922 the statement:

value=c(219,222)

will assign the value 4922 to value. If any of the columns are blank or multicoded in any way, they are ignored.

+----2----+ +----2----+

49 2 and 4912 2 both give value=492.

Columns

Columns may also store arithmetic information, but unlike other variables they have a predefined size which means they can only store numbers of a certain size. For instance, c(1,10) can store numbers of up to ten digits whereas c(1,3) only stores numbers of up to three digits.

If the number is negative Quantum places the minus sign in the column immediately to the left of the first digit, but if there are no spare columns the first digit will be dropped and the minus sign placed in the left-hand column. If t5=−278, the statement:

c(46,49)=t5 gives 4----+----5 -278 but:

c(47,49)=t5 yields 4----+----5 -78

Note that this does not hold true for negative numbers whose length exceeds the field width by more than one character. Then, the number is copied into the field from the right and the minus sign and any excess digits are ignored. Thus, if t5=−1278, c(42,44) will contain the number 278.

If the value to be saved has fewer digits than there are columns in the field, it will be right-justified in the field and the remaining columns padded with zeros.

Here are some more examples:

/* Room to store values of t60 between –99 and +999 c(110,112)=t60

/* visits*4 should be between –999 and +9999, otherwise truncated c(34,37)=visits*4

/* Result never truncated since maximum value is 81 c(10,11)=c7*c8

/* Total holidays taken c(224,230)=home + abroad /* Count the number of codes

pch=numb(c21,c22,c23’1/5’,c24’1/9’)

When copying real numbers into columns, Quantum needs to know how many decimal places are required. This is done by following the variable with a colon and a digit defining the number of places. For example, if x5=10.22, the statement:

cx(15,19):2=x5 results in:

10.22

If the real number has more decimal places than we have allowed for, say 3 instead of 2, the extra decimal places will be ignored.

Assignment with and, or and xor

Quick Reference

To copy codes which are present in at least one of a list of columns, type:

data_var_name=or(cnum1[’codes1’], cnum2[’codes2’], ...) To copy codes which are present in all of a list of columns, type:

data_var_name=and(cnum1[’codes1’], cnum2[’codes2’], ...) To copy codes which are present in only one of a list of columns, type:

data_var_name=xor(cnum1[’codes1’], cnum2[’codes2’], ...)

If any of these statements includes codes (p), only those codes are checked for. Any unlisted codes are then ignored.

The final type of assignment is copying codes from a set of columns. The codes copied depend upon the type of operator used:

The format of the statement is:

column = operator(ca,cb,cc, ...)

where ca, cb, and cc are the columns whose codes are to be compared. Note that even if you are comparing codes in consecutive columns, each column must be identified separately, preceded by a c.

and Copy codes present in all columns.

or Copy codes present in one or more columns.

xor Copy codes present in one column only.

Suppose we have:

----+----4 111 /22 453 77 and we type:

c181=and(c137,c138,c139) we will then have:

----+----4 ... ---8----+

111 1 /22 2 453

77

Notice that even though the codes ‘3’ and ‘7’ appear in more than one column they are not copied to c181 because they are not common to all columns.

Let’s take the same three columns with the or operator. We type:

c182=or(c137,c138,c139) which gives us:

----+----4 ... ---8----+

111 1 /22 / 453 5 77 7

c182 contains a list of all codes present in at least one of the named columns.

Now, look at the same columns with xor:

c183=xor(c137,c138,c139) yields:

----+----4 ... ---8----+

111 4 /22 5 453 77

Here only two codes have been copied because all other codes appear in more than one column. If one column was blank, this would be ignored if there were other codes unique to one column. Only if there were no other unique codes would column 183 be blank. For instance, if we have c11=’ ’, c12=’12’, c13=’13’ and we type:

c14=xor(c11,c12,c13)

we would have c14=’23’, but if c13 were to contain a ‘12’ instead, c14 would be blank.

All our examples so far have referred to whole columns, but sometimes you will only be interested in specific codes in those columns. To write this in Quantum, follow each column number with the positions to be checked enclosed in single quotes. Any unnamed codes in those columns are then automatically ignored. Here is an example. Our data is:

----+----4----+----5 1 1 2 / 3 / 5 5 6

the statement c85=and(c31’1/3’,c41’1/3’,c45’1/3’) gives us:

----+----4----+----5 ... 8----+----9 1 1 2 3 / 3 /

5 5 6

Even though column 31, 41 and 45 all contain a ‘3’ and a ‘5’, Quantum only copies the ‘3’ because the ‘5’ is not part of our specification. We have used the same code specification for all three columns, but you can use whatever combination you like.

These types of statement are extremely useful for setting up shorthand references to the codes present in a group of columns. Say, for instance, that you wanted various statements throughout the edit to be executed only if there was a ‘1’ in one or more of c110, c112, c120 and c125. You can always write out each column and code separately each time:

if(c110’1’.or.c112’1’.or.c120’1’.or.c125’1’) ...

but it is simpler and much more efficient to say:

c181=or(c110,c112,c120,c125) if (c181’1’) ...

especially if you will need to refer to the contents of these columns again later on in the edit.

This facility may also be used to simplify what would otherwise be complicated filter conditions in the tabulation section.

In document Quantum Vol1 (Page 103-116)