Data editing > Changing the contents of a variable > Assignment statements > Storing arithmetic values > Real and integer variables
 
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:
t1=2.5 + 3.4
gives
t1=5
but integer values placed in a real variable are saved as reals with decimal places and accuracy to 6 significant figures:
x1=1 + 7
gives
x1=8.0
Integer variables are often used to count the number of respondents having a specific characteristic. For example, to count the number of respondents holidaying at home and the number taking holidays abroad, write:
/* 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 Flow control.
When a record is read with c113’1’, the variable home is increased by 1, and when a record is read with c113’2’ the variable abroad is increased by 1.
For example, you might have five respondents who took the following holidays:
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 is 3 and abroad is 2. The person unlucky enough to have no holiday at all is ignored.
The example above accumulates 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 example:
home=0
For more information about when you might want to do this, see the do statement in Loops.
Columns which contain single codes can be treated as a whole number. For example, if the data is:
+----2----+
4922
the statement:
value=c(219,222)
assigns 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.
See also
Storing arithmetic values