The cvalue
(calculated value) tag calculates a mathematical equation, evaluates a logical expression, or accepts a value directly, and assigns it to a question or variable.
REV. Revisions
type: radio
invisible: y
cvalue: 10
chapter: S
csentrycalc: y
10. Soft launch
Details
- Add the
cvalue
tag directly on the question or variable to set a value to that widget. Thecvalue
tag accepts various inputs, including logical statements, mathematical expressions, Perl expressions, heredoc, list functions, and more. - The
cvalue
tag can be applied to coded number variables or used withselectby: calculate
with closed-ended coded variables. - If you're using the
type: radio
tag for your question, you can addinvisible: y
to hide it from the respondent. - The value is set when the page is submitted, but adding the
onload
tag allows thecvalue
to execute when the page loads, instead of when the page is submitted.
Additional examples
Using a mathematical equation
A common use of the cvalue
tag is to calculate a mathematical expression. In the example below, the responses from Q1 and Q2 are summed and stored in Q3.
1. How many apples did you eat last month?
type: number
datatype: whole
size: 3
2. How many oranges did you eat last month?
type: number
datatype: whole
size: 3
3. Total pieces of fruit
type: coded number
cvalue: $Q1 + $Q2
In this example, imagine a variable stores the current year. The respondent enters their birth year at QS3, and the cvalue
tag calculates their age at QAGE. If the respondent is under 18, they are automatically terminated from the survey.
S3. What year were you born?
type: number
datatype: whole
range: 1930-2024
AGE. VAR | Calculated AGE (via current-YEAR –S3)
type: coded number
translate: n
cvalue: $QYEAR-$QS3
termif: $QAGE<18 {id: QAGE_17} {text: S2 | UNDER 18 years old}
Reminder, a coded number variable stores data as text.
Converting and storing a response in uppercase
There are instances where we use the cvalue
tag on respondent-facing questions to store data differently than how it was entered. For example, if we allow respondents to enter a postal code in lowercase but need to save it in uppercase for analysis, we can use the 'uc' Perl function, as shown in the example below.
4. Where is your business located?
instructions: Please provide your six character postal code:
type: text
datatype: capostcode
prefix: Postal code:
cvalue: uc($Q4)
Formatting and storing a date
Date questions store data in a "YYYY-MM-DD" format in reports, but you can store the response in a text variable with a different format, such as "MM-DD-YYYY." To do this, apply the cvalue
tag to the text variable and use the 'return' function to format the stored date as needed for use throughout the survey.
9. What is your favorite date of the year?
type: date
9TEXT. Date in mm/dd/yyyy format
type: text
invisible: y
cvalue: return $Q9->date_mmddyyyy;
Storing a value based on a URL parameter
In almost every survey, entry links include parameters. Data from these parameters can be captured, referenced, and stored in a variable using the cvalue
tag. In the example below, the parameter 'os' is attached to entry links with the values '0' or '1' (e.g., '&os=1'). If '0' or '1' is found, the value is stored in QOSAMP. If no parameter is found, the double pipe '||', which acts as the "or" operator, will default the value to '0'.
OSAMP. Oversample Identifier
type: radio
invisible: y
chapter: panel_data
cvalue: url_param('os') || 0
0. Sample
1. Over sample
Calculating a closed-ended question with 'selectby'
The cvalue
tag is usually applied to a text question or variable but can also be used with closed-ended variables if combined with selectby: calculate
. For single select or multiselect questions, use selectby: calculate
with the cvalue
tag to define how values are calculated.
For example, if all supermarkets in the STORES list have option IDs below 5, and club stores have IDs of 5 or above, QCLUB can calculate whether a respondent's primary store (from Q8) falls into the "supermarket" or "club" group based on the option ID.
set list: STORES
randomize: y
1. Market Basket
2. Price Chopper
3. Price Rite
4. ShopRite
5. BJ's Wholesale Club
6. Costco
7. Sam's Club
...
PRIMARY. Store where majority of shopping done
type: quotas
optsfrom: Q8
CLUB. Is primary store club or grocery?
type: coded single select
selectby: calculate
cvalue: if ($QPRIMARY<5) {1} else {2}
1. Supermarket
2. Club/Warehouse
Storing a value from a sheet with 'fetchcell'
cvalue
tag can also use the fetch_cell
function to reference data from a spreadsheet. In this example, if the respondent's $trans_id
is found in this "permitted IDs" sheet, a value of '1' is stored in QIDCHECK1. Then, QCHECKIDS2 will automatically select "Yes" if QIDCHECK1 has a value of '1' or "No" if it does not.
IDCHECK1. VAR | ID check - only those loaded should be able to continue
type: text
invisible: y
cvalue: fetch_cell( name=> 'is0910626.idvalidation', row => $trans_id, column => 'approve' )
CHECKIDS2. VAR | Check id to list
type: coded single select
1. Yes {if $QIDCHECK1==1}
2. No {if !($QIDCHECK1==1)}
Comments
0 comments
Please sign in to leave a comment.