Creating Text Variables

From Q
Jump to: navigation, search
Related Online Training modules
JavaScript If Statements
Generally it is best to access online training from within Q by selecting Help > Online Training

Text Variables can be created as JavaScript Variables or using Excel-Style Formulas, selecting Text rather than Numeric when creating the variables.

The only difference between creating Text Variables and creating Numeric Variables is that one returns a string of text whereas the other returns either a number or a NaN.

JavaScript example

The following JavaScript code produces a variable containing Cat for respondents with an ID of less than 1000, and Dog for the remaining respondents:

if (ID < 1000) "cat"; else "dog";

Key points to note about this expression are:

  • We need to surround text with quotation marks.
  • The quotation marks cannot be the tidy ones that Microsoft Word automatically creates – see that it is "dog", not “dog”. To get unformatted double quotation marks if writing your code in Word, type the quotation mark as you normally would and then press Ctrl and z on your keyboard. More generally, it is better to use a text editor (see Setting Up a Text Editor for QScript).

Converting numeric variables into text variables

Any Numeric Variable can be turned into a Text Variable (or returned as text in a part of code being used to construct a variable) by using the expression String(varName), where varName is the name of the variable that you want converted to a string.

Converting categorical variables into text variables

Any Categorical Variable can be converted into a Text Variable. If you wish to see the values of the variable, then use the String function described in the previous section. If you wish the Text Variable to contain the labels, then use Q.Label(varName), where varName is the name of the variable of interest.

See also

Manipulating Text with JavaScript