# How to Create a Variable with Standardized Values

From Q

Sometimes it is necessary to transform the values of a variable so that the mean of the data is 0 and the standard deviation is 1. Such a variable is *normalized* or *standardized*. This can be achieved by creating a JavaScript Variable to do the transformation, and the method is described below.

Note that if you want to standardize the data from several variables by case you should instead use the QScript Create New Variables - Standardize Data by Case

## JavaScript Variable

To make a variable with standardized values, you can use the following method:

- Go to the Variables and Questions tab.
- Right-click and select
**Insert Variables > JavaScript Formula > Numeric**. - Select the option
**Access all data rows (advanced)**. - Copy and paste the code below into the
**Expression**. - Change the target variable
`Q9_A_5`to be the Variable Name of the variable that you want to standardize. - Set an appropriate
**Label**and click**OK**.

// Get the values from the target variable var _starting_vals = Q9_A_5; // Remove any missing values var _vals = _starting_vals.filter(function (_v) { return !isNaN(_v); }) // Get mean and standard deviation var _sd = jStat.stdev(_vals, true); var _mean = jStat.mean(_vals); // Generate the standardized value for each of the original values and return the results var _results = _starting_vals.map(function (_v) { return (_v - _mean) / _sd; } ); _results

Notes:

- The formula calculate the mean and standard deviation of the values in the variable, and then produces a new set of values by subtracting the mean and dividing by the standard deviation.
- The formula above uses the
*Sample Standard Deviation*. - If you wish to use the
*Population Standard Deviation*instead, change the line`_sd = jStat.stdev(_vals, true);`to`_sd = jStat.stdev(_vals, false);`. - The
**Access all data rows (advanced)**option used above allows the formula to look at all of the respondent data for this variable instead of processing each value individually. - If you want to repeat this process for many variables, then create the first one as above, and then apply Use as Template for Replication.
- If the values of the source variable change, for example by recoding or adding new data to the project, then the new standardized variable will update automatically.