Chapter 9

Using Functions

In This Chapter

arrowGetting acquainted with the Compute Variable menu

arrowFocusing on functions

arrowUsing the LENGTH function

arrowWorking with the ANY function

arrowMaking the MEAN function work for you

The Compute Variable dialog box (shown in Figure 9-1) is found in the Transform main menu and is one of the most important dialog boxes in all of SPSS. Why? Inside this dialog box are dozens and dozens of different functions that perform all kinds of different calculations. This is where you go to make new variables out of existing ones. If you don’t have much programming experience, it may seem a little tricky, at first. But don’t worry — in this chapter, we give you the information you need.

image

Figure 9-1: The Compute Variable dialog box.

On the right side of Figure 9-1, you see a series of menus with lists and lists of functions. If you use spreadsheet software like Microsoft Excel or Apple Numbers, you may have encountered functions much like these. Here’s just a sampling of the kinds of functions that you’ll find:

  • Arithmetic functions
  • Statistical functions
  • String functions
  • String/numeric conversion functions
  • Date and time functions
  • Random variable and distribution functions
  • Missing value functions
  • Logical functions

When you click a function group, like All (refer to Figure 9-1), you see a list of the functions that belong to that function family. In this chapter, we investigate just three of these functions, but a diverse three. The idea is to give you a sense of the different kinds of things you can do, and to introduction you to the function help. For all three examples, we use the Web Survey.sav dataset.

The LENGTH Function

At the end of the Web Survey in Web Survey.sav, there is an open-ended comment. These kinds of questions often prompt a generic answer like “Not at this time” or “None,” but sometimes the responses can be quite useful. The goal is to identify, as simply as possible, if the respondent provided a comment at all. That way, we won’t clog up our report with lots of blank rows. When analyzing the answers, we can easily select only those folks who provided a comment. The LENGTH function is pretty easy, so it’s a good place to start.

  1. Open the Web Survey.sav dataset.

    You may stumble upon Web Survey 2.sav. That’s what the dataset should look like at the end of the chapter.

  2. Choose Transform  ⇒  Compute Variable.
  3. Choose String in the Function Group area in the upper right, and then choose the LENGTH function.
  4. Either drag LENGTH into position in the editing area at the top, or click the up-arrow button on the right.

    The window should look like Figure 9-2.

  5. Either drag Comment into position, replacing the question mark, or click the right-arrow button on the left.

    Making progress. You could just click OK, but let’s take a moment to read the function Help at the bottom of the screen. This is visible whenever you click the Function name (as in Figure 9-2). Onscreen you have to scroll to read it, but the complete text is as follows:

    LENGTH. LENGTH(strexpr). Numeric. Returns the length of strexpr in bytes, which must be a string expression. For string variables, in Unicode mode this is the number of bytes in each value, excluding trailing blanks, but in code page mode this is the defined variable length, including trailing blanks. To get the length (in bytes) without trailing blanks in code page mode, use LENGTH(RTRIM(strexpr)).

    Whoa! That’s a handful, but it all means something. The first word is the name of the function. The second part — LENGTH(strexpr) — is not being repetitive. It’s trying to show you the grammar of the command. You have to give it a string expression in parentheses.

    Next, the word Numeric means that what it’s going to give back to you is, well, a numeric. This little pattern is repeated in every single function. The rest, in the form of sentences, isn’t always easy to read, but the first little part is half the battle.

    So, in this case, we give it a word, and it gives us a number. Let’s try it.

  6. Give the new variable the new name Length and verify that your screen looks like Figure 9-3.
  7. Click OK.

    In the data window, you should find a new variable, Length, listing several numbers. Glance at the original Comment variable. Doug, Jack, and Georgia did not leave comments, so they should have a zero value.

image

Figure 9-2: The Compute Variable dialog box with the LENGTH function chosen.

image

Figure 9-3: The completed LENGTH function Compute Variable dialog box.

The ANY Function

When analyzing data, it’s often useful to identify people who meet certain criteria. For example, you may want to identify employees who have high performance ratings (to keep them in mind for future job openings) or you may want to identify customers who gave your company a low satisfaction rating (to see how you can improve their experience). The ANY function allows you to identify cases that meet the criteria you specify across a series of variables.

  1. Open the Web Survey.sav dataset.
  2. Choose Transform  ⇒  Compute Variable.
  3. Choose Search in the Function Group area in the upper right, and then choose the ANY function.
  4. Either drag ANY into position in the editing area at the top, or click the up-arrow button on the right.

    The window should look like Figure 9-4.

  5. Review the Function definition at the bottom of the menu.

    Let’s take a moment to read the function Help for ANY at the bottom of the screen:

    ANY(test,value[,value,…]). Logical. Returns 1 or true if the value of test matches any of the subsequent values; returns 0 or false otherwise. This function requires two or more arguments. For example, ANY(var1, 1, 3, 5) returns 1 if the value of var1 is 1, 3, or 5 and 0 for other values. ANY can also be used to scan a list of variables or expressions for a value. For example, ANY(1, var1, var2, var3) returns 1 if any of the three specified variables has a value of 1 and 0 if all three variables have values other than 1.

    Most people don’t like these definitions very much, but remember: It all means something. The first word is the name of the function. The second part — ANY(test,value[,value,…]) — is warning us that this one is more complicated than LENGTH. It has more moving parts. The next word — Logical — tells us that we’re going to get a True or False result, which SPSS does with 1 and 0.

    For this one, we give it a test value and several variable names, and it gives us a 1 or a 0. Let’s try it.

  6. Give the new variable the new name Any_Ones .
  7. Type 1 immediately after the parenthesis in place of the question mark.

    It looks like you have room for only one more variable because you have only one more question mark, but you can add as many as you like.

  8. Drag all the “Sat” variables (Sat1, Sat2, Sat3, Sat4, and Sat5) with commas separating them just like Figure 9-5.
  9. Click OK.

    In the data window, you should find a new variable, Any_Ones, which is 1 (True) or 0 (False), depending on whether the respondent even gave a 1 (the lowest score). You could easily imagine that management at this company wants to know if anyone is upset enough to give a score of 1 on any question.

image

Figure 9-4: The Compute Variable dialog box with the ANY function chosen.

image

Figure 9-5: The completed ANY function Compute Variable dialog box.

The MEAN Function and Missing Data

Now, we’re going to calculate the mean of the five satisfaction variables. There is a twist, though — we can include or exclude rows with some missing data. If the respondent didn’t answer any of the questions, we don’t have much choice, but what if they answer some and not others? That’s actually a little tricky. Here’s how to do it two different ways:

  1. Open the Web Survey.sav dataset.
  2. Choose Transform  ⇒  Compute Variable.
  3. Choose Statistical in the Function Group area in the upper right, and then choose the MEAN function.
  4. Either drag MEAN into position in the editing area at the top, or click the up-arrow button on the right.
  5. Review the function definition at the bottom of the menu.

    Here’s what it says:

    MEAN. MEAN(numexpr,numexpr[,..]). Numeric. Returns the arithmetic mean of its arguments that have valid, nonmissing values. This function requires two or more arguments, which must be numeric. You can specify a minimum number of valid arguments for this function to be evaluated.

    The first word is the name of the function. The second part — MEAN(numexpr,numexpr[,..]) — is a little easier than LENGTH. We have to provide at least two variables, and three dots tell us we can do more than two, if needed. The next word — Numeric — tells us that we’re going to get a number as a result.

    We give it several variable names (or numbers), and it gives us a number back.

  6. Give the new variable the new name Mean_Sat .
  7. Drag all the Sat variables (Sat1, Sat2, Sat3, Sat4, and Sat5) with commas separating them just like Figure 9-6.
  8. Click OK.

    In the data window, you should find a new variable, Mean_Sat, which should be populated for everyone even though Frank didn’t answer all the questions. It’s basing his average on the answers he did provide.

  9. Return to the Compute Variable menu — our work is still there.
  10. Give the new variable the new name Mean_Sat2 .
  11. Keep all the Sat variables (Sat1, Sat2, Sat3, Sat4, and Sat5) with commas separating them just like Figure 9-6.
  12. Add two extra characters to the Function so that it reads MEAN.5.

    The .5 tells SPSS Statistics that in order to compute a mean, each case must provide at least five valid values. It should look like Figure 9-7.

  13. Click OK.

    In the data window, you should find a new variable, Mean_Sat2, which should be populated for everyone except Frank because he didn’t answer all the questions.

image

Figure 9-6: The completed MEAN function Computer Variable dialog box.

image

Figure 9-7: The completed MEAN function Compute Variable dialog box.

This is powerful stuff. Mastering missing values is one of the things that will mark you as an expert in SPSS Statistics.

The finished product can be found in a dataset called Web Survey 2.sav. Only one version of Mean Sat appears in the dataset, however.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.3.111