Data editing > Using subroutines in the edit > Writing your own routines > Writing subroutines in Quantum > An example of a Quantum subroutine
 
An example of a Quantum subroutine
In this example, a survey has been conducted to test the market for a new TV station which would be available via the satellite network. When it comes to asking how likely respondents would be to take this new channel, people who already subscribe to the satellite network are asked slightly different questions from those who do not. However, the possible responses to each set of questions are identical.
One way of checking these answers is to write a subroutine and call it up using variables to define the columns to be checked. For example:
ed
/* c(21,23) is for those already subscribing
/* c(24,26) is for those who don't subscribe
if (c17'1') call subchk(21,22,23); else; call subchk(24,25,26)
/* rest of edit
return
subroutine subchk(high,low,dep)
/* high – willingness to take at $20
/* low – willingness to take at $10
/* dep – willingness to pay advance deposit
int high
int low
int dep
r sp '1/59' c(high), c(low), c(dep)
return
end
As the comments show, the fields to be checked are c(21,23) for those already subscribing to the satellite network and c(24,26) for non-subscribers. Both calls to the subroutine subchk name the columns in the field individually. This is because you want to look at the codes present in each column. The data variables have not been defined at the start of the edit because they are read automatically from Quantum’s variables file. This means that they are external variables and can have their values accessed by the subroutine.
The subroutine statement uses local variables with names describing the contents of the variables they represent. The variable high represents c21 and c24 which show how likely the respondent would be to take the new station if it cost $20 a month. Similarly the variable low represents c22 and c25 and dep represents c23 and c26. All local variables are defined in the subroutine as the name of the variable they represent.
The require statement checks whether each column is single-coded in the range ‘1/59’.
Though this example talks about columns in the data, they are treated as integers. The call to the subroutine gives the column numbers without a preceding ‘c’. The subroutine itself defines its arguments as integers and then uses them as pointers into the C array. There are two reasons for this:
It allows Quantum to report the column numbers correctly if it finds records which fail the require statement. Passing columns to a subroutine as data variables causes Quantum always to refer to column 0 in the output from require regardless of the true column number which is in error.
It enables you to set new codes into the columns used in the subroutine. Normally, any changes made to the C array inside a subroutine are forgotten when control passes back to the main program. Referring to the columns as pointers into the C array, as in this example, causes any changes to the C array to be remembered when the subroutine finishes.
See also
Writing subroutines in Quantum