There are many kinds of conditions you can specify by selecting a variable or multiple variables from the left column, moving them to the center text field, and using the blue buttons to specify values e.
You can also use the built-in functions in the Function Group list under the right column. After you are finished defining the conditions under which your computation will be applied to the data, click Continue. Note that when you specify a condition in the Compute Variable: If Cases window, the computation will only be performed on the cases meeting the specified condition.
If a case does not meet that condition, it will be assigned a missing value for the new variable. You do not necessarily need to use the Compute Variables dialog window in order to compute variables or generate syntax. You can write your own syntax expressions to compute variables and it is often faster and more convenient to do so! The first line gives the COMPUTE command, which specifies the name of the new variable on the left side of the equals sign, and its formula on the right side of the equals sign.
The formula on the right side of the equals sign corresponds to what you would enter in the Numeric Expression field in the Compute Variables dialog window. To compute string variables, the general syntax is virtually identical. However, with string variables, you must first "declare" a new variable as a string variable before you can define it using a COMPUTE statement:.
Note that the format must be put inside parentheses. The format specification for strings will always start with the letter A, followed by a number giving the "width" of the string the maximum number of characters that variable can contain. In this case, the new variable will have a width of 20, so data values can contain up to 20 characters. When declaring a new string variable, you should take care to set the width of the string to be wide enough so that your data values aren't accidentally cut short.
Now we will use what we have learned throughout this tutorial to demonstrate how to compute a new variable. In this example, we wish to compute BMI for the respondents in our sample. The height in inches and weight in pounds of the respondents were observed; so to compute BMI, we want to plug those values into the formula.
Alternatively, you can double-click on the variable names in the left column to move them to the Numeric Expression field, and then write the expression around them. This expression says that the new variable will be calculated as variable Weight multiplied by , divided by the square of variable Height. This syntax can be generated automatically by following the dialog window steps above and clicking Paste instead of OK.
Notice that in the sample dataset, the test score variables in the sample dataset are all next to each other. In the previous example, we explicitly specified all four test score variables in the MEAN function. But what if there had been ten or twenty test score variables?
It would take much longer to manually enter all twenty variable names. What if we wanted to refer to the entire range of test score variables, beginning with English and ending with Writing , without having to type out each variable's name? Let's repeat the previous example and show how the TO statement is used to refer to a range of variables inside a function.
This method is dependent on the positions of the variables in the dataset. If the variables are not in sequential order, this method may not work correctly. Your final numeric expression should appear as.
The final expression indicates that the new variable, AverageScore3 , will be calculated as the average of all the variables between English and Writing in the dataset. If you've already verified the computation for AverageScore2 , then you should be able to verify that AverageScore2 and AverageScore3 are identical. In the previous examples, we did not talk about what happens when one or more of the variables has missing values for a given case.
In fact, if there is a missing value for one or more of the input variables, SPSS assigns the new variable a missing value. That is, there must be valid values for each input variable in order for the computation to work. This is called listwise exclusion. Listwise exclusion can end up throwing out a lot of data, especially if you are computing a subscale from many variables. In SPSS, you can modify any function that takes a list of variables as arguments using the.
As long as a case has at least n valid values, the computation will be carried out using just the valid values. In the previous example, we used the built-in MEAN function to compute the average of the four placement test scores. Stated another way, a given case could have at most one missing test score and still be OK.
Alternatively, using the formula MEAN. If you click Paste after revising the formula, the following syntax will be written to the syntax editor window:.
A common scenario on health questionnaires is to have multiple questions about risk factors for a certain disease.
These questions may originally be coded as 0 absent and 1 present ; or 0 no and 1 yes. For example, on a questionnaire about ADHD, we may ask three questions about whether an individual's biological parents or siblings have been diagnosed with ADHD:. The function ANY is a convenient way to compute this indicator. The ANY function is designed to return the following:. The application we will demonstrate is intended to be used when you want to check for one specific value across many variables. For this example, we will use this tiny dataset.
Let's check that the ANY function produced the results that we expected. If you run the above code, you should get results that look like the following:.
What does this mean? If we go back to the ADHD example used at the start of this section, it implies that anyone whose mother, father, or biological sibling has been diagnosed with ADHD, is themselves considered to have a risk factor for ADHD.
It does not assign "extra risk" if someone has two or more relatives that have been diagnosed. When working with string variables -- and especially when working with text data that's been manually typed into the computer -- your data values may have variation in capitalization. If you want to use this type of variable in an analysis, you'll have to "standardize" the data values so that they all have the same patterns of capitalization, because SPSS considers each unique capitalization to be a different data value even if the strings are otherwise identical.
A common string transformation is to convert a string to all uppercase or all lowercase characters. In the sample dataset, the variable Major is a string variable containing open-ended, write-in responses asking for the person's college major. If we want to merge the otherwise-identical categories of "Art History" and "Art history", we'll need to transform this variable so that the characters are all uppercased or all lowercased.
Change the variable type to String, and set its length to This number comes from the length of the variable Major. Click Continue to confirm and return to the Compute Variable window. Notice that in the Compute Variable window, the box where the formulas are entered is now labeled "String Expression" instead of "Numeric Expression". After executing the transformation and rerunning the frequency table on the transformed variable, we should see that the counts and frequencies of the previously duplicated categories are now combined:.
While this variable is still not ready for analysis -- for example, several duplicated categories exist because of misspellings or minor variations in wording -- we have now completed the first step. Search this Guide Search.
Computing Variables Sometimes you may need to compute a new variable based on existing information from other variables in your data. For example, you may want to: Convert the units of a variable from feet to meters Use a subject's height and weight to compute their BMI Compute a subscale score from items on a survey Apply a computation conditionally, so that a new variable is only computed for cases where certain conditions are met In this tutorial, we'll discuss how to compute variables in SPSS using numeric expressions, built-in functions, and conditional logic.
The Compute Variable window will open where you will specify how to calculate your new variable. When writing an expression in the Compute Variables dialog window: SPSS is not case-sensitive with respect to variable names. Combine these elements as desired. In our example, seven variables are added to create the new scale: abscale.
To do this we have only to add the abortion variables, as shown above. Following these steps, you can realize any new variables math, your data, and interest can conceive, with an assurance that your intentions have been met.
From Wikibooks, open books for an open world. Computing variables in SPSS refers to computing with existing variables to create new variables. Namespaces Book Discussion. Views Read Edit Edit source View history.
0コメント