# How to use scripts

In Research Studio you can export data captured in the survey and send to external scripts. The result can be returned and reimported for later use in the questionnaire. This can be e.g.

• To calculate (difference, product, sum etc)
• To calculate segments
• To select random values from a multiple question (“I will now concentrate on 3 out of your selected brands”)
• Select a maximum out of two numbers
• Calculate the time used between two timers

Where to use the scripts

When you call external scripts in Research Studio (these are Visual Basic Scripts that all need to be in a scripts file on the server. These need to be installed by the MI Pro team), you may send any number of parameters to the script. These parameters can be normal numbers (1,2,3) or text ('Hello') or they can be references to questions and system variables (\gender, \age.a.1, \@sys_time, \@quot_male).

These scripts are commonly used in the following instances:

• Answer control (Range expression): \script:sum2(\q1,\q2)
• Automatically fill in answers (Autofill expression): \script:diff2(\q1,\q2,0)
• Reference to previous question (Refans expression): \script:sum3(\q1,\q2,\q3)

Scripts: An overview

The following table describes some of the scripts that you can use to create quantitative variables in Research Studio:

 VB Script Description Diff(x1,x2,x3) Calculates the difference between two values, x1 and x2. If x1 or x2 is non-numeric, the script alters the value with x3. Diff0(x1,x2,x3) Calculates the difference between two values, x1 and x2. If x1 or x2 is non-numeric, the script alters the value with x3. If difference is negative, the script returns 0. Diffneg(x1,x2,x3) Calculates the difference between two values, x1 and x2. If x1 or x2 is non-numeric, the script alters the value with x3. If difference is negative, the script returns the ABSOLUTE VALUE, e.g. positive. Sum2(x1,x2) Calculates the sum of two numbers. If any number is non-numeric, it is set to zero. Sum3(x1,x2,x3) Calculates the sum of three numbers. If any number is non-numeric, it is set to zero. Max2(x1,x2) Finds the maximum of two numbers. If any number is non-numeric, it is set to zero. Max3(x1,x2,x3) Finds the maximum of three numbers. If any number is non-numeric, it is set to zero. Mult2(x1,x2) Multiplies two numbers. If any number is non-numeric, it is set to zero. SelectRandomN(x1,x2,x3) Select RANDOM codes from a list: x1 – Codes to pick from x2 – Numbers to pick x3 – A number to initiate the random generator, usually \@sys_random is used to ensure the same sequence the next time around. SelectFirstN(x1,x2) Select FIRST codes from a list. x1 – Codes to pick from x2 – Numbers to pick TimeDiffSeconds(x1,x2) The TimeDiffSeconds calculates the difference between ANY two timers, e.g. start and end time. Returns time used (in seconds) x1 = TimeStart x2 = TimeEnd

x1,x2 simply refers to parameter 1, parameter 2 while \q1, \q2 references questions q1 and q2. When using a reference to a question, the actual answer (or answers) will be sent as a parameter to the function.

If the question type is a single choice or a numeric type, only one value is passed. If the question type is a multiple choice, then a ";" semicolon delimited list of values are sent. The script then returns a "character" string representing the "answer" from the script. This answer can then be used as the actual value wherever the script is called, for example, to supply the list of allowed responses (range) or a value to compare against if the example is used in a filter.

Some examples of using scripts

There will normally be a project called Templates in your MI Pro installation.

The following examples in this chapter refer to one of the questionnaires in this project: Scripts. (If this is not included in your setup, please contact support@mipro.net )

The input section The input section will include the following questions:

• A timer (autofilled)
• The random number seed (autofilled)
• A numeric with three inputs (The second value should be LESS than the value in row one)
• A multiple choice question (select 5 or more categories)

These will be used as input to the scripts included.

Multiply 2 numbers (Script: Mult2)
The script Mult2 will multiply two values and return the product.

1. In answer control, specify this: script:mult2(\seed1.a.1,\seed2.a.1,0)where \seed.1.a.1 and \seed2.a.1 refers to 2 numbers (If these were categories from a single question, syntax would be \seed1.a and \seed2.a) 2. This will return a product of the two input numbers
3. If you want the question to be autofilled, simply add afilla (in the row) sys_range c (fill in automatically and Continue) or sys_range e (verify an Enter)
4. So from the inputs (in this example) 100 and 45 you will have Create a sum of 2 or 3 numbers (Script: Sum2 or Script: Sum3)

In the same way, we have a script that adds together two or three numbers. Similarly to the example above, you can do as follows:

1. In the answer control specify the following
• Answer control: script:sum3(\seed.1, \seed.2, \seed.3)
2. In afilla in the row: sys_range c (Continue) or sys_range e (verify and Enter) Create a difference, subtract a from b (Scripts: diff, diff0, diffneg)

• Diff(x1,x2,x3) (subtracts x2 from x1). Returns the difference or x3 if either x1 or x2 is non-numeric
• Diff0(x1,x2,x3) (subtracts x2 from x1). Returns the difference OR zero if difference is negative OR x3 if either x1 or x2 is non-numeric
• Diffneg(x1,x2,x3) (subtracts x2 from x1). Returns the ABSOLUTE VALUE of the difference OR x3 if either x1 or x2 is non-numeric Select N random variables from a list (script:selectrandomN)

If you have e.g. a multiple question of 10 categories ticked and want the system to draw e.g. 3 randomly of these, you can use the script selectRandomN

Specify in answer control: script:selectRandomN(\x1,x2,x3) where

• X1= list / name of question to use as a seed
• X2= Number to select
• X3= Random seed (usually use the random variable) Select first N variables from a list (script:selectFirstN)

If you have e.g. a multiple question of 10 categories ticked and want the system to draw e.g. the first 3 categories of these, you can use the script selectFirstN

Specify in answer control: script:selectFirstN(\x1,x2) where

• X1= list / name of question to use as a seed
• X2= Number to select Select the highest number from a list (scripts: Max2 and Max 3)

If you have e.g. 2 or 3 numeric values and want the system to draw the highest of these numbers, you can use one of the scripts max2 or max3

Specify in answer control: script:max2(\x1,x2) or max3(x1,x2,x3) where

• X1,X2,(X3)= list / names of questions to use as a seed
• The script will return 0 (zero) if any value is non-numeric

Estimate the time used / using timers in the questionnaire

You can insert timers anywhere in the questionnaire

1. Create a numeric question
2. In the row, specify in the afilla attribute: sys_timenowf c
3. The question will automatically autofill the question with the timestamp the respondent passed this question.
4. In a later numeric question you can insert another timer (as described above)
5. Finally, you can create a question usin the script TimeDiffSeconds 