<< Click to Display Table of Contents >> Navigation: Reference > Commands > WRITE BACK example |
A simple two level trailer where each trailer relates to a particular brand:
start control,
c=test.cba,
finish control,
start data,
!
! This CL script shows one method of handling a
! simple trailer survey which goes -
!
! card 1
! card 2 repeats for each brand
!
! The serial number is needed to check when
! the end of each record is reached.
! The card numbers command allocates space
! only, the READ command gets the data.
! The card number is in column 6 and will be
! tested after the cards are read.
!
serial number in 1-5,
card numbers /1/,/2/,
!
! Here we initialise variables for each record.
! In this case we are going to count the
! number of brand cards read and accumulate
! a "brands used" variable.
! The variable $level will be used in tables to
! determine the type of record.
!
di $num2=0,
xt='Number of brand trailer cards',
dm $level(2),
xt='Level of data',
x='Respondent;Brand',
!
! Now we read the card 1 which is assumed to
! be the first card in each record.
!
read $101-180,
if $106/n1, then,
l 'Not card 1',
goto finish,
endif,
!
! Now we define variables from the card 1
! (respondent details).
....
!
! Now we read the brand cards (card 2).
! If there are no more card 2 we goto @nomore.
!
@again,
read $201-280 erec=@nomore,
if $206/n2, then,
l 'Not card 2',
goto @again,
endif,
!
! Now we process the card 2 (brand card).
!
di $num2=$num2+1,
ds $bbrand=$210/1-4,
xt='This brand',
x='Brand A;Brand B;Brand C;Brand D',
ordm $rbrand=$bbrand,
xt='All brands for this respondent',
x='Brand A;Brand B;Brand C;Brand D',
....
dm $level=f,t,
write,
goto @again,
!
! When all cards have been read we can do
! some final checking and write away the
! respondent record.
!
@nomore,
if $num2/0, l 'No brand cards for this questionnaire',
....
!
! Before we write the respondent record we
! need to write back the values of $num2 and
! $rbrand because their contents were not
! valid whilst we were writing the
! level 2 records.
! The proper values for these variables
! did not exist until all the brand cards have
! been read and processed.
!
write back $num2.$rbrand,
!
dm $level=t,f,
write,
finish data,
start tables,
! filter respondent tables - if $level/1,
! filter card 2 tables - if $level/2,
!
! every table should be filtered on $level.
!
....
finish tables,
If we produce a level 2 table for brand A which filters on $rbrand/3 (Brand C used as well) this will be correct. Without the WRITE BACK this table would only be correct if the brand C trailer always appeared in the data before the brand A trailer. Similarly, if a brand table is produced using $num2 (number of brands used) this will only be correct for the last brand card in each questionnaire unless we use WRITE BACK.
For trailer surveys with three levels of data a WRITE MARK (2) will be needed at the beginning of the second level and a WRITE BACK (2) at the end of the second level after all level 3 cards have been processed for that level.
If your CL script does not reach FINISH DATA and you use WRITE BACK you must be sure to execute a WRITE MARK at the appropriate place(s), for example at the beginning of each new questionnaire. Also be careful with WRITE BACK (0) which will overwrite the whole IDF.