The selectby
tag is used to automatically assign one or more values to closed-ended variables. The selectby
method determines how the selection is made.
Single method
AGE. What is your age?
type: integer
size: 3
maxlen: 2
range: 1-99
AGE_BRACKET. Age bracket
type: radio
selectby: condition
16. < 18 {if $QAGE < 18}
18. 18 - 24 {if ($QAGE >=18 and $QAGE <=24)}
25. 25 - 34 {if ($QAGE >=25 and $QAGE <=34)}
35. 35 - 44 {if ($QAGE >=35 and $QAGE <=44)}
45. 45 - 54 {if ($QAGE >=45 and $QAGE <=54)}
55. 55 - 64 {if ($QAGE >=55 and $QAGE <=64)}
65. 65 + {if $QAGE >=65}
Multiple methods
3BRANDS. Select up to 3 brands
type: checkbox
maxgroups: 3
selectby: weight, counts
optsfrom: VENDORS [4] {if anyChecked($Q14R[id],3,4)} {weight: 1000000}
optsfrom: VENDORS -[4,97] {if anyChecked($Q14R[id],4) } {weight: 10000 + anyChecked($Q14B,[id])*100}
optsfrom: VENDORS [97] {if anyChecked($Q14R[id],4) } {weight: 1 + anyChecked($Q14B,[id])*10000 }
Details
- Variables with a
selectby
tag are hidden from respondents and automatically punched when the page is submitted. - All
selectby
methods respectcondition
decorators, which limit the option set before applying the selected method. - In the event of a tie, the final selection is made randomly.
- Most
selectby
methods can be combined for a "tiered approach" (e.g.,selectby: weight, counts
), where the second method is applied in case of a tie, overriding the default random selection.
Methods
There are various methods for selecting options, each designed for specific purposes. Below is a complete list of inputs (methods) that can be used with the selectby
tag.
Values | Description |
balance |
Balances selections across multiple variables (requires the |
calculate |
Works with the This method can be used with single and multiselect coded variables. However, it cannot be used with coded number variables or be combined with other |
counts |
Selects the option(s) with the fewest completes to balance response counts. |
order |
Selects the options based on their order listed in the survey code. |
Used with quotas. Ensures proportional selection when quota targets have varying sizes. Selects the option(s) that have the lowest percentage of its target reached in the data. All quota buckets must have targets entered into the Quotas applet for this input to work correctly. If a quota target is left blank, that option will always be assigned. |
|
random / condition
|
The methods Quotas default to |
steps |
New to r9. Specifies a series of programming steps for the software to select multiple options. Each step is evaluated in the order it is programmed and can include additional Only one |
weight |
Prioritizes options based on weights. A higher weight means a higher selection priority. Weights can be static or dynamic (e.g., using a previous response). If a weight is not defined, the default value of '0' is used. |
Used with the An option without an assigned weight will never be selected. |
Tip! It's best practice to use random
/ condition
instead of counts
when possible, as it requires fewer system resources to process.
Commonly used tags
The following tags may be useful to pair with selectby
.
Tag | Description |
applyif |
Skips the variable for the respondent if the specified condition is not met. |
balanceby |
Assigns values to a question in a balanced way, based on nested variables, using a least squares approach. |
cvalue |
Calculates and sets a value for a question or variable. cvalue accepts any logical statement or Perl expression for input. |
exclusive |
Specifies when certain options should not be selected with others. |
maxgroups |
Include with a multiselect variable to set the maximum number of options that can be chosen. |
Additional examples
Condition
To use selectby: condition
, options should include a condition
decorator. In the example below, each respondent is assigned a color category in QCOLORTYPES based on their selection in Q4.
4. Which color is your favorite?
type: radio
1. black
2. gray
3. blue
4. green
5. purple
6. red
7. pink
8. orange
9. yellow
COLORTYPES. Color categories
type: radio
selectby: condition
1. Neutral colors { if anyChecked($Q4,1,2) }
2. Cool colors { if anyChecked($Q4,3..5) }
3. Warm colors { if anyChecked($Q4,6..9) }
Tip! QCOLORTYPES could also be programmed as a coded variable, which uses selectby: condition
by default.
Counts
The example below uses selectby: counts
at QCOLOR. First, an option must be selected at Q1 to be eligible. Then, the system assigns the respondent to the option with the lowest number of completes at the time of response.
setlist: COLORS
1. Red
2. Orange
3. Yellow
4. Green
5. Blue
6. Indigo
7. Violet
1. Which of the following colors do you like?
instructions: Check all that apply.
type: checkbox
optsfrom: COLORS
COLOR. Single color selection
type: coded single select
selectby: counts
optsfrom: COLORS {if anyChecked($Q1,[id])}
Weight
To select option(s) based on a priority or weighted value defined in the survey code, you should use selectby: weight
. The highest weight is selected first. If no weight is defined, the default value is '0.' Options with a weight of '0' can still be selected unless excluded. As with all methods, options can only be selected if the conditions are met, regardless of the weight.
Examples:
- Static weights: Always prioritize options based on a given importance.
- Response-based weights: Use respondent answers, such as numeric inputs, as weight values.
-
Conditioned weights: Logical conditions (e.g.,
anyChecked
) are evaluated as '1' if true and '0' if false. They can act like values in aweight
decorator to adjust weights dynamically based on previous responses.
Static weights
In this example, if option 1 (McDonald's) was selected at Q1, a weight of '10' is assigned. If option 6 (Subway) was also selected, a weight of '1' is assigned. The respondent would be assigned McDonald's at QFOOD because it has the highest weight between those two eligible options.
set list: FASTFOOD 1. McDonald's 2. Wendy's 3. Burger King 4. In-N-Out 5. KFC 6. Subway 1. At which of the following fast food restaurants have you eaten in the past six months? type: checkbox optsfrom: FASTFOOD FOOD. Food quota, prioritizing McDonald's and Burger King, then Wendy's and In-N-Out, and finally KFC and Subway type: quotas selectby: weight optsfrom: FASTFOOD [1,3] { if anyChecked($Q1,[id]) } {weight: 10} optsfrom: FASTFOOD [2,5] { if anyChecked($Q1,[id]) } {weight: 5} optsfrom: FASTFOOD [4,6] { if anyChecked($Q1,[id]) } {weight: 1}
Tip! Static weights can also be applied directly to list options with a weight
decorator.
Response-based weights
In the example below, the numeric response at Q1 populates the placeholder '$Q1R[id]' for the same option at the QFOOD quota. For instance, if Subway is rated '8' and KFC is rated '2' at Q1, each option will have that value inserted as the weight at QFOOD, with all other options set to '0.' The QFOOD quota would assign the respondent to Subway because it has the highest weight. In the case of a tie, the fast food restaurant with the fewest completes (counts) will be assigned.
set list: FASTFOOD
1. McDonald's
2. Wendy's
3. Burger King
4. In-N-Out
5. KFC
6. Subway
1. Of the last ten times you have eaten fast food, how many have been at the following restaurants?
type: integer table
range: 0-10
rowsfrom: FASTFOOD
sct: Total {total: 10} FOOD. Food quota, prioritizing based on where the respondent has eaten most in the past year type: quotas selectby: weight, counts optsfrom: FASTFOOD {weight: $Q1R[id]}
Conditioned weights
Using the FASTFOOD list again, the conditional statement 'if anyChecked($Q1,[id])' ensures that only options "ever heard of" at Q1 are eligible for selection at QFOOD. Since an option cannot be chosen at Q2 or Q3 unless it was selected at Q1, this is the only condition needed.
Weights prioritize selection at Q3 as the most important, followed by Q2. There is no need to assign a weight for Q1, as it holds the lowest priority.
1. Which of the following fast food restaurants have you ever heard of? type: checkbox optsfrom: FASTFOOD 2. And at which have you ever eaten? type: checkbox optsfrom: Q1 3. And at which have you eaten in the past 12 months? type: checkbox optsfrom: Q2 FOOD. Prioritizing based on any restaurants frequented in the past year, followed by any ever eaten, then if ever heard of type: quotas selectby: weight, counts optsfrom: FASTFOOD { if anyChecked($Q1,[id]) } {weight: anyChecked($Q3,[id])*100 + anyChecked($Q2,[id])*10}
Order
In some cases where selectby: weight
is used, selectby: order
can be used instead to achieve the same result. The system checks each option in the order they are listed until it finds one that meets the condition.
In this example, if 'anyChecked($QCURRENT,1)' is true for the respondent, they are assigned option 1 "Purchaser." If not, the system moves down to option 2. If 'anyChecked($QFAMILIAR,1)' is true for the respondent, they are assigned option 2 "Aware." If neither option 1 nor option 2 is true for the respondent, the system defaults to option 3, which has no condition and is available to all respondents.
USER_TYPE. User type overall
type: coded single select
selectby: order
1. Purchaser { if anyChecked($QCURRENT,1) }
2. Aware { if anyChecked($QFAMILIAR,1) }
3. Not aware
Note, this example is reduced for illustrative purposes. All referenced questions would need to also exist in the survey code to make this example work correctly.
Calculate
To use selectby: calculate
, apply the cvalue
tag to perform the necessary calculations used to select options.
In the example below, the QSMP variable utilizes the url_param
method to pull in information from the URL parameter 'smp' in a survey entry link. If the value '10', '20', or '30' is found, it will be selected at QSMP; otherwise, '0' will be the default selection.
SMP. SAMPLE via URL-parameter
type: coded single select
selectby: calculate
cvalue: url_param('smp') || 0
0. NO "smp" parameter-value
10. Representative
20. Augment
30. Oversample
Percentage
To use selectby: percentage
, quota targets must be set for all applicable quota buckets in the Quotas applet.
For example, if researchers aim for 50 responses each for BBC and CMT, and 100 each for Comedy Central and Discovery Channel, these maximums should be entered on QDEEPDIVE in the Quotas applet. As respondents answer Q3, they will be assigned to the channels with the lowest percentage of progress toward the specified maximums. This ensures that all buckets will fill evenly, despite having different maximums.
3. Which of the following TV networks do you watch at least once a week for an hour or more?
type: checkbox
1. BBC America
2. CMT
3. Comedy Central
4. Discovery Channel
5. FOX
6. ESPN
DEEPDIVE. QUOTA | 2 Brands for Deep Dive
type: quotas
maxgroups: 2
selectby: percentage
optsfrom: Q3
Weighted draw
The weighted_draw
method allows you to specify a proportional filling scheme among options.
Imagine adding candies into a jar. You add different amounts of each type of candy as defined by its weight. Then, you randomly pick candies from the jar.
This method allows options to be assigned based on known percentages of total respondents, without necessarily knowing how many total respondents there will be.
It's important to attach the weight
decorator to each option, as items without an assigned weight will never be selected.
PQ. Product quotas type: coded single select selectby: weighted_draw 1. Product A {weight: 4} 2. Product B {weight: 2} 3. Product C {weight: 1}
In the above example, Product A is twice as likely be selected as Product B, which is then in turn twice as likely to be selected as Product C. The total weighted_draw
values sum to 7. So, each product's chance can be thought of as a fraction: Product A has a 4 out of 7 chance, Product B has a 2 out of 7 chance, and Product C has a 1 out of 7 chance of being chosen.
Comments
0 comments
Please sign in to leave a comment.