Despite using SPSS for years I've yet clarified for myself how to
specify multiple criteria for an if statement... I would like to choose a number of string values for a given variable in a Select statement. Up to now I been using syntax such as : Select if (Var1='string1' or Var1='string2' or Var1='string3'.....) Is there a way to shorten this so I don't have to specify the variable each time? Jerry Weinberg, Ph.D Director, Office of Institutional Research _____________________________ St. Thomas University 16401 NW 37th Avenue Miami Gardens, FL 33054 (305) 474-6886 [hidden email] |
Try :
SELECT IF ANY(Var1,"string1","string2",.................) Edward. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of Weinberg, Ph.D. J. Sent: Tuesday, October 17, 2006 2:51 PM To: [hidden email] Subject: MULTIPLE IF STATEMENTS Despite using SPSS for years I've yet clarified for myself how to specify multiple criteria for an if statement... I would like to choose a number of string values for a given variable in a Select statement. Up to now I been using syntax such as : Select if (Var1='string1' or Var1='string2' or Var1='string3'.....) Is there a way to shorten this so I don't have to specify the variable each time? Jerry Weinberg, Ph.D Director, Office of Institutional Research _____________________________ St. Thomas University 16401 NW 37th Avenue Miami Gardens, FL 33054 (305) 474-6886 [hidden email] |
In reply to this post by Weinberg, Jerry
Hi Jerry,
You wrote : JW> Thanks much for your quick reply, which happily works and is the answer. JW> Just so that I might be totally up to speed, is there an analogous JW> statement if I wanted to specify a number of 'and' rather then 'or' JW> criteria? I guess somebody on the list may be able to help you with the later part of your question. Thanks. Edward. -----Original Message----- From: Weinberg, Ph.D. J. [mailto:[hidden email]] Sent: Tuesday, October 17, 2006 3:01 PM To: Edward Boadi Subject: RE: Re: MULTIPLE IF STATEMENTS Thanks much for your quick reply, which happily works and is the answer. Just so that I might be totally up to speed, is there an analogous statement if I wanted to specify a number of 'and' rather then 'or' criteria? -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edward Boadi Sent: Tuesday, October 17, 2006 2:54 PM To: [hidden email] Subject: Re: MULTIPLE IF STATEMENTS Try : SELECT IF ANY(Var1,"string1","string2",.................) Edward. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of Weinberg, Ph.D. J. Sent: Tuesday, October 17, 2006 2:51 PM To: [hidden email] Subject: MULTIPLE IF STATEMENTS Despite using SPSS for years I've yet clarified for myself how to specify multiple criteria for an if statement... I would like to choose a number of string values for a given variable in a Select statement. Up to now I been using syntax such as : Select if (Var1='string1' or Var1='string2' or Var1='string3'.....) Is there a way to shorten this so I don't have to specify the variable each time? Jerry Weinberg, Ph.D Director, Office of Institutional Research _____________________________ St. Thomas University 16401 NW 37th Avenue Miami Gardens, FL 33054 (305) 474-6886 [hidden email] |
In reply to this post by Weinberg, Jerry
At 02:51 PM 10/17/2006, Weinberg, Ph.D. J. wrote:
>I would like to choose [any of] a number of string values for a >variable in a Select statement. I been using syntax such as : >Select if (Var1='string1' or Var1='string2' or Var1='string3'.....) As 02:54 PM 10/17/2006, Edward Boadi answered: >SELECT IF ANY(Var1,"string1","string2",.................) Follow-up (passed on from an off-list response), >JW> Is there an analogous statement if I wanted to specify a number of >'and' rather then 'or' criteria? I'm afraid there's no ALL function, to match ANY. SELECT IF ALL(Var1,"string1","string2",...) wouldn't be any use; it would never be true unless all the "strings" were the same. But testing whether all variables have a selected value, like SELECT IF ALL("testval",VAR1 TO VARn) could be useful. One trick (thanks, Jan Spousta) is SELECT IF MAX(VAR1 TO VARn) EQ "testval" AND MIN(VAR1 TO VARn) EQ "testval". ("MAX" and "MIN" work for string variables.) Another is a DO REPEAT construct. This looks strange; it works because the multiple SELECT IF statements it generates, 'and' together. COMPUTE #TAKE_IT = 1. DO REPEAT TESTVAR = VAR1 TO VARn . SELECT IF TESTVAR EQ "testval". END IF. Demonstration - SPSS draft output: * ....................................... . GET FILE=TESTDATA. LIST. List |-----------------------------|---------------------------| |Output Created |18-OCT-2006 22:19:22 | |-----------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents\Temporary\SPSS \2006-10-17 Weinberg - MULTIPLE IF STATEMENTS.SAV CaseID Value1 Value2 Value3 Alpha 1 2 3 Beta 3 2 3 Gamma 3 3 3 Delta 1 3 3 Epsilon 3 3 3 Number of cases read: 5 Number of cases listed: 5 * The "Max/min" method: ........ . SELECT IF MAX(Value1 TO Value3) EQ 3 AND MIN(Value1 TO Value3) EQ 3. LIST. List |-----------------------------|---------------------------| |Output Created |18-OCT-2006 22:19:22 | |-----------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents\Temporary\SPSS \2006-10-17 Weinberg - MULTIPLE IF STATEMENTS.SAV CaseID Value1 Value2 Value3 Gamma 3 3 3 Epsilon 3 3 3 Number of cases read: 2 Number of cases listed: 2 * The "DO REPEAT" method: ........ . GET FILE=TESTDATA. DO REPEAT TESTVAR = Value1 To Value3. . SELECT IF TESTVAR EQ 3. END REPEAT. LIST. List |-----------------------------|---------------------------| |Output Created |18-OCT-2006 22:19:23 | |-----------------------------|---------------------------| C:\Documents and Settings\Richard\My Documents\Temporary\SPSS \2006-10-17 Weinberg - MULTIPLE IF STATEMENTS.SAV CaseID Value1 Value2 Value3 Gamma 3 3 3 Epsilon 3 3 3 Number of cases read: 2 Number of cases listed: 2 |
-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Richard Ristow Sent: Wednesday, October 18, 2006 9:28 PM To: [hidden email] Subject: Re: [SPSSX-L] MULTIPLE IF STATEMENTS [>>>Peck, Jon] [snip] I'm afraid there's no ALL function, to match ANY. SELECT IF ALL(Var1,"string1","string2",...) wouldn't be any use; it would never be true unless all the "strings" were the same. But testing whether all variables have a selected value, like SELECT IF ALL("testval",VAR1 TO VARn) could be useful. [>>>Peck, Jon] In the Bonus Pack for SPSS 15, which will be generally available on Developer Central (www.spss.com/devcentral) in November, there is a programmability function that does this and more. The matchcount function tests its first argument against each of the other arguments and returns the number that are equal. It also returns the number of tests, so using the equivalent of TO or ALL doesn't require you to know how many variables were involved. In this particular example, it is equivalent to the COUNT command, but it is more general, since the value to count (to test for equality with) can be a variable, and Python expressions can be used as well. Here is a little example. begin program. import spss, spssaux, trans, extendedTransforms vardict = spssaux.VariableDict() tproc = trans.Tfunction() tproc.append(extendedTransforms.matchcount, ("matches","tests"), ("f", "f"), [1, vardict.range("startvar", "endvar")]) tproc.execute() end program. compute allmatched = matches eq tests. First this example gets the SPSS variable dictionary, then it iterates over the cases counting the number of variables startvar TO endvar with value 1 and saves that count and the number of variables in the range as new variables. If instead of the value 1, it used a variable there, it would be checking for equality of that variable to each of the ones in the range. The allmatched value is true if all variables were 1. matchcount is also a generalization of the ANY function, since it returns the count of matches, not just true or false. SYSMIS values never match anything in this function: using the equivalent of SYSMIS as the first argument would cause the returned value always to be zero. However, the Tfunction class allows for optional listwise deletion of cases with missings, so such cases would get SYSMIS values for the new variables if that option is used. Regards, Jon Peck |
Free forum by Nabble | Edit this page |