SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
Expand all | Collapse all

Can I put a function as the first value in an ANY or RANGE function.

  • 1.  Can I put a function as the first value in an ANY or RANGE function.

    Posted Fri January 22, 2021 02:11 PM

    If I have the following syntax:

    IF (any(1, v1 to v10)) vnew = 1.

    SPSS returns a 1 for every case, where there is a 1 between v1 and v10.

    However, if I want a 1 returned for every 1, 2, 3 or 4 between v1 and v10 I have to write that line four times for it to work.

    Because this does not work:

    IF (any(range(1, 4), v1 to v10)) vnew = 1.

    Is there a way to do this? Similarly with for example "<1" as the first value or a range of variables, for example something like:

    IF (any((<1), v1 to v10)) vnew = 0

    or

    IF (any((v1 to v10), 1 to 4)) vnew = 1.

    does not work.

    I tried with the range function as well, but I can't seem to find a way to combine or nest functions.






    #SPSSStatistics
    #Support
    #SupportMigration


  • 2.  RE: Can I put a function as the first value in an ANY or RANGE function.

    Posted Fri January 22, 2021 02:36 PM

    range requires a variable as the first argument. In general functions can be nested, but these examples don't work. You can do this with a loop, however. (This example is really a doubly nested loop.)

    compute inrange = 0.

    loop #v=1 to 4.

    compute inrange = inrange or any(#v, x1 to x10).

    end loop.

    If you need to do this a lot, you could use the SPSSINC TRANS extension command with a small Python function. You would just define the function once and then invoke it as needed as the formula in SPSSINC TRANS. I can expand on this if you want to go that way.






    #SPSSStatistics
    #Support
    #SupportMigration


  • 3.  RE: Can I put a function as the first value in an ANY or RANGE function.

    Posted Fri January 22, 2021 06:22 PM

    Thanks so much, I think the loop will work. I just have to edit some syntaxes for work and have never worked with SPSS. Just trying to pick up context clues and google my way from there. This will help a lot.






    #SPSSStatistics
    #Support
    #SupportMigration