Compute function to create variable that meets multiple conditions

classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|

Compute function to create variable that meets multiple conditions

rph523
I'm trying to write syntax in SPSS that uses the compute variable command to create a new variable that looks at 5 other scale variables and finds the minimum variable among the five with a value greater than or equal to 0.80.

For example,

            CAT1 CAT2 CAT3 CAT4 CAT5
CASE 1  0.75 0.82 0.85 0.90 0.95
CASE 2  0.65 0.75 0.82 0.87 0.95

For case 1, the computed variable would return a value of 2. For case 2, it would return a value of 3.

I'm stumped -- originally I thought it would work with a conditional MIN statement, but it's not working. Thinking it might need an IF/THEN, but I'm hoping someone has a more efficient solution. Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Bruce Weaver
Administrator
David Marso posted syntax to solve a similar problem in the thread linked below.  Take a look at his syntax, and see if you can figure out how to adapt it for your situation.

http://spssx-discussion.1045642.n5.nabble.com/Tweaking-Identify-3-Highest-Values-within-Each-Case-td5731751.html

HTH.


rph523 wrote
I'm trying to write syntax in SPSS that uses the compute variable command to create a new variable that looks at 5 other scale variables and finds the minimum variable among the five with a value greater than or equal to 0.80.

For example,

            CAT1 CAT2 CAT3 CAT4 CAT5
CASE 1  0.75 0.82 0.85 0.90 0.95
CASE 2  0.65 0.75 0.82 0.87 0.95

For case 1, the computed variable would return a value of 2. For case 2, it would return a value of 3.

I'm stumped -- originally I thought it would work with a conditional MIN statement, but it's not working. Thinking it might need an IF/THEN, but I'm hoping someone has a more efficient solution. Thanks.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Rich Ulrich
In reply to this post by rph523
The neatest logic avoids loops, etc., if you don't mind the file processing -
  Use CaseToVars and save the Index for the variables;
  SELECT to keep only values GE 0.80;
  GET FILE  *  and tag FIRST;
  SELECT to keep the "First"s.  The var-index is the var-number that was first.

--
Rich

> Date: Fri, 10 Jun 2016 14:37:16 -0700

> From: [hidden email]
> Subject: Compute function to create variable that meets multiple conditions
> To: [hidden email]
>
> I'm trying to write syntax in SPSS that uses the compute variable command to
> create a new variable that looks at 5 other scale variables and finds the
> minimum variable among the five with a value greater than or equal to 0.80.
>
> For example,
>
> CAT1 CAT2 CAT3 CAT4 CAT5
> CASE 1 0.75 0.82 0.85 0.90 0.95
> CASE 2 0.65 0.75 0.82 0.87 0.95
>
> For case 1, the computed variable would return a value of 2. For case 2, it
> would return a value of 3.
>
> I'm stumped -- originally I thought it would work with a conditional MIN
> statement, but it's not working. Thinking it might need an IF/THEN, but I'm
> hoping someone has a more efficient solution. Thanks.
>
>

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

David Marso
Administrator

 I believe you mean VARSTOCASES rather than CASESTOVARS ;-)
Something like untested:

VARSTOCASES / ID=CASENUM /MAKE v FROM CAT1 TO CAT5 / INDEX=varnum.
SELECT IF ( v GE 80 ).
SORT CASES BY CASENUM v.
MATCH FILES /FILE * /BY  CASENUM /FIRST=@TAG@.
 
Should work but my system is tied up right now so can't test (there is always the FM).


Rich Ulrich wrote
The neatest logic avoids loops, etc., if you don't mind the file processing -
  Use CaseToVars and save the Index for the variables;
  SELECT to keep only values GE 0.80;
  GET FILE  *  and tag FIRST;
  SELECT to keep the "First"s.  The var-index is the var-number that was first.

--
Rich

> Date: Fri, 10 Jun 2016 14:37:16 -0700
> From: [hidden email]
> Subject: Compute function to create variable that meets multiple conditions
> To: [hidden email]
>
> I'm trying to write syntax in SPSS that uses the compute variable command to
> create a new variable that looks at 5 other scale variables and finds the
> minimum variable among the five with a value greater than or equal to 0.80.
>
> For example,
>
>             CAT1 CAT2 CAT3 CAT4 CAT5
> CASE 1  0.75 0.82 0.85 0.90 0.95
> CASE 2  0.65 0.75 0.82 0.87 0.95
>
> For case 1, the computed variable would return a value of 2. For case 2, it
> would return a value of 3.
>
> I'm stumped -- originally I thought it would work with a conditional MIN
> statement, but it's not working. Thinking it might need an IF/THEN, but I'm
> hoping someone has a more efficient solution. Thanks.
>
>

     
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

rph523
Thanks for the ideas!

I tried running the syntax below, but it cleared all data from the set and returned nothing?

VARSTOCASES / ID=STU_ID /MAKE v FROM SEL5_PRE TO SEL1_PRE / INDEX=varnum.
SELECT IF ( v GE 80 ).
SORT CASES BY STU_ID v.
MATCH FILES /FILE * /BY STU_ID /FIRST=@TAG@.

Is there anything I should do before running it?
Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

rph523
In reply to this post by David Marso
I should mention, this is a multiple imputation set and contains many additional variables besides the 5 categories I'm trying to work with.  Not sure if that matters?
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Jon Peck
In reply to this post by Rich Ulrich
Here's an answer that does not require restructuring the data.  It returns sysmis if no value meets the threshold.

begin program.
def casemin(*args):
    z = sorted(zip(args, range(len(args))))
    for item in z:
        if item[0] >= .8:
            return item[1] + 1
    return None
end program.

spssinc trans result = minindex
/formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".

On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <[hidden email]> wrote:
The neatest logic avoids loops, etc., if you don't mind the file processing -
  Use CaseToVars and save the Index for the variables;
  SELECT to keep only values GE 0.80;
  GET FILE  *  and tag FIRST;
  SELECT to keep the "First"s.  The var-index is the var-number that was first.

--
Rich

> Date: Fri, 10 Jun 2016 14:37:16 -0700

> From: [hidden email]
> Subject: Compute function to create variable that meets multiple conditions
> To: [hidden email]
>
> I'm trying to write syntax in SPSS that uses the compute variable command to
> create a new variable that looks at 5 other scale variables and finds the
> minimum variable among the five with a value greater than or equal to 0.80.
>
> For example,
>
> CAT1 CAT2 CAT3 CAT4 CAT5
> CASE 1 0.75 0.82 0.85 0.90 0.95
> CASE 2 0.65 0.75 0.82 0.87 0.95
>
> For case 1, the computed variable would return a value of 2. For case 2, it
> would return a value of 3.
>
> I'm stumped -- originally I thought it would work with a conditional MIN
> statement, but it's not working. Thinking it might need an IF/THEN, but I'm
> hoping someone has a more efficient solution. Thanks.
>
>

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

rph523
Thank you!!  This scenario worked perfectly.  Very much appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

David Marso
Administrator
In reply to this post by Jon Peck

One of my basic bitching sessions begins with WHY DON'T PYTHON programmers believe in commenting their code.  I see this crap all the time!!!  Is it somehow self evident WTF is going on?  Maybe throw in a line which states what zip does? OTOH, Why do python for this simple task?  I resort to it only when metadata are required.   Some people seem to go to python as a default without considering basic shit ;-)))

Jon Peck wrote
Here's an answer that does not require restructuring the data.  It returns
sysmis if no value meets the threshold.

begin program.
def casemin(*args):
    z = sorted(zip(args, range(len(args))))
    for item in z:
        if item[0] >= .8:
            return item[1] + 1
    return None
end program.

spssinc trans result = minindex
/formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".

On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <[hidden email]> wrote:

> The neatest logic avoids loops, etc., if you don't mind the file
> processing -
>   Use CaseToVars and save the Index for the variables;
>   SELECT to keep only values GE 0.80;
>   GET FILE  *  and tag FIRST;
>   SELECT to keep the "First"s.  The var-index is the var-number that was
> first.
>
> --
> Rich
>
> > Date: Fri, 10 Jun 2016 14:37:16 -0700
> > From: [hidden email]
> > Subject: Compute function to create variable that meets multiple
> conditions
> > To: [hidden email]
> >
> > I'm trying to write syntax in SPSS that uses the compute variable
> command to
> > create a new variable that looks at 5 other scale variables and finds the
> > minimum variable among the five with a value greater than or equal to
> 0.80.
> >
> > For example,
> >
> > CAT1 CAT2 CAT3 CAT4 CAT5
> > CASE 1 0.75 0.82 0.85 0.90 0.95
> > CASE 2 0.65 0.75 0.82 0.87 0.95
> >
> > For case 1, the computed variable would return a value of 2. For case 2,
> it
> > would return a value of 3.
> >
> > I'm stumped -- originally I thought it would work with a conditional MIN
> > statement, but it's not working. Thinking it might need an IF/THEN, but
> I'm
> > hoping someone has a more efficient solution. Thanks.
> >
> >
>
> ===================== To manage your subscription to SPSSX-L, send a
> message to [hidden email] (not to SPSSX-L), with no body text
> except the command. To leave the list, send the command SIGNOFF SPSSX-L For
> a list of commands to manage subscriptions, send the command INFO REFCARD




--
Jon K Peck
[hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Jon Peck
Python programmers would not document zip for the same reason that someone writing Statistics syntax would not document COMPUTE: it's a basic tool of the language.  In this particular case, using Python avoids the violence of restructuring the data, and, yes, for anyone with a basic knowledge of Python, the code is self evident.

On Mon, Jun 13, 2016 at 5:38 AM, David Marso <[hidden email]> wrote:
One of my basic bitching sessions begins with WHY DON'T PYTHON programmers
believe in commenting their code.  I see this crap all the time!!!  Is it
somehow self evident WTF is going on?  Maybe throw in a line which states
what zip does? OTOH, Why do python for this simple task?  I resort to it
only when metadata are required.   Some people seem to go to python as a
default without considering basic shit ;-)))


Jon Peck wrote
> Here's an answer that does not require restructuring the data.  It returns
> sysmis if no value meets the threshold.
>
> begin program.
> def casemin(*args):
>     z = sorted(zip(args, range(len(args))))
>     for item in z:
>         if item[0] >= .8:
>             return item[1] + 1
>     return None
> end program.
>
> spssinc trans result = minindex
> /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
>
> On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich &lt;

> rich-ulrich@

> &gt; wrote:
>
>> The neatest logic avoids loops, etc., if you don't mind the file
>> processing -
>>   Use CaseToVars and save the Index for the variables;
>>   SELECT to keep only values GE 0.80;
>>   GET FILE  *  and tag FIRST;
>>   SELECT to keep the "First"s.  The var-index is the var-number that was
>> first.
>>
>> --
>> Rich
>>
>> > Date: Fri, 10 Jun 2016 14:37:16 -0700
>> > From:

> ryan.hudes@

>> > Subject: Compute function to create variable that meets multiple
>> conditions
>> > To:

> SPSSX-L@.UGA

>> >
>> > I'm trying to write syntax in SPSS that uses the compute variable
>> command to
>> > create a new variable that looks at 5 other scale variables and finds
>> the
>> > minimum variable among the five with a value greater than or equal to
>> 0.80.
>> >
>> > For example,
>> >
>> > CAT1 CAT2 CAT3 CAT4 CAT5
>> > CASE 1 0.75 0.82 0.85 0.90 0.95
>> > CASE 2 0.65 0.75 0.82 0.87 0.95
>> >
>> > For case 1, the computed variable would return a value of 2. For case
>> 2,
>> it
>> > would return a value of 3.
>> >
>> > I'm stumped -- originally I thought it would work with a conditional
>> MIN
>> > statement, but it's not working. Thinking it might need an IF/THEN, but
>> I'm
>> > hoping someone has a more efficient solution. Thanks.
>> >
>> >
>>
>> ===================== To manage your subscription to SPSSX-L, send a
>> message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text
>> except the command. To leave the list, send the command SIGNOFF SPSSX-L
>> For
>> a list of commands to manage subscriptions, send the command INFO REFCARD
>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Bruce Weaver
Administrator
In reply to this post by Bruce Weaver
* Variation on David's syntax in this thread:
* http://spssx-discussion.1045642.n5.nabble.com/Tweaking-Identify-3-Highest-Values-within-Each-Case-td5731751.html.

* Read in the sample data provided by the OP.

DATA LIST FREE / Case (F5.0) CAT1 CAT2 CAT3 CAT4 CAT5 (5F5.2).
BEGIN DATA
1  0.75 0.82 0.85 0.90 0.95
2  0.65 0.75 0.82 0.87 0.95
END DATA.

MISSING VALUES CAT1 to CAT5 (lo thru 0.8).
COMPUTE WHAT=MIN(CAT1 to CAT5).
EXECUTE.
MISSING VALUES CAT1 to CAT5 ().

VECTOR X=CAT1 TO CAT5.
COMPUTE #FOUND=0.
LOOP #i = 1 TO 5.
+ DO IF X(#i)= WHAT AND NOT (#FOUND).
+   COMPUTE WHERE=#i.
+   COMPUTE #FOUND=1.
+ END IF.
END LOOP IF #Found.
FORMATS WHAT (F5.2) WHERE (F2.0).
LIST.

OUTPUT:

 Case   CAT1   CAT2   CAT3   CAT4   CAT5  WHAT WHERE
 
    1    .75    .82    .85    .90    .95   .82    2
    2    .65    .75    .82    .87    .95   .82    3
 
Number of cases read:  2    Number of cases listed:  2


Bruce Weaver wrote
David Marso posted syntax to solve a similar problem in the thread linked below.  Take a look at his syntax, and see if you can figure out how to adapt it for your situation.

http://spssx-discussion.1045642.n5.nabble.com/Tweaking-Identify-3-Highest-Values-within-Each-Case-td5731751.html

HTH.


rph523 wrote
I'm trying to write syntax in SPSS that uses the compute variable command to create a new variable that looks at 5 other scale variables and finds the minimum variable among the five with a value greater than or equal to 0.80.

For example,

            CAT1 CAT2 CAT3 CAT4 CAT5
CASE 1  0.75 0.82 0.85 0.90 0.95
CASE 2  0.65 0.75 0.82 0.87 0.95

For case 1, the computed variable would return a value of 2. For case 2, it would return a value of 3.

I'm stumped -- originally I thought it would work with a conditional MIN statement, but it's not working. Thinking it might need an IF/THEN, but I'm hoping someone has a more efficient solution. Thanks.
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Rich Ulrich
In reply to this post by rph523
Assuming the var-names match your own var-names, ...
the cutoff value of 80 is not your own cutoff value of 0.80, so
no cases would qualify.

--
Rich Ulrich

> Date: Fri, 10 Jun 2016 17:15:48 -0700

> From: [hidden email]
> Subject: Re: Compute function to create variable that meets multiple conditions
> To: [hidden email]
>
> Thanks for the ideas!
>
> I tried running the syntax below, but it cleared all data from the set and
> returned nothing?
>
> VARSTOCASES / ID=STU_ID /MAKE v FROM SEL5_PRE TO SEL1_PRE / INDEX=varnum.
> SELECT IF ( v GE 80 ).
> SORT CASES BY STU_ID v.
> MATCH FILES /FILE * /BY STU_ID /FIRST=@TAG@.
>
> Is there anything I should do before running it?
> Thanks.
>
>
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

David Marso
Administrator
In reply to this post by Jon Peck

But this isn't a PYTHON list is it.  Maybe a few comments for python illiterate.
OTOH:  For clients I comment everything so there is no confusion.
Didn't address my question of why bother with python in this simple case which can be addressed by any number of SIMPLE approaches with BASIC SPSS code ;-)
Is python always your GOTO solution Jon?
Don't necessarily need to butcher the data with V2C but VECTOR/LOOP goes way over the head of most noobs.
----


Jon Peck wrote
Python programmers would not document zip for the same reason that someone
writing Statistics syntax would not document COMPUTE: it's a basic tool of
the language.  In this particular case, using Python avoids the violence of
restructuring the data, and, yes, for anyone with a basic knowledge of
Python, the code is self evident.

On Mon, Jun 13, 2016 at 5:38 AM, David Marso <[hidden email]> wrote:

> One of my basic bitching sessions begins with WHY DON'T PYTHON programmers
> believe in commenting their code.  I see this crap all the time!!!  Is it
> somehow self evident WTF is going on?  Maybe throw in a line which states
> what zip does? OTOH, Why do python for this simple task?  I resort to it
> only when metadata are required.   Some people seem to go to python as a
> default without considering basic shit ;-)))
>
>
> Jon Peck wrote
> > Here's an answer that does not require restructuring the data.  It
> returns
> > sysmis if no value meets the threshold.
> >
> > begin program.
> > def casemin(*args):
> >     z = sorted(zip(args, range(len(args))))
> >     for item in z:
> >         if item[0] >= .8:
> >             return item[1] + 1
> >     return None
> > end program.
> >
> > spssinc trans result = minindex
> > /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
> >
> > On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <
>
> > rich-ulrich@
>
> > > wrote:
> >
> >> The neatest logic avoids loops, etc., if you don't mind the file
> >> processing -
> >>   Use CaseToVars and save the Index for the variables;
> >>   SELECT to keep only values GE 0.80;
> >>   GET FILE  *  and tag FIRST;
> >>   SELECT to keep the "First"s.  The var-index is the var-number that was
> >> first.
> >>
> >> --
> >> Rich
> >>
> >> > Date: Fri, 10 Jun 2016 14:37:16 -0700
> >> > From:
>
> > ryan.hudes@
>
> >> > Subject: Compute function to create variable that meets multiple
> >> conditions
> >> > To:
>
> > SPSSX-L@.UGA
>
> >> >
> >> > I'm trying to write syntax in SPSS that uses the compute variable
> >> command to
> >> > create a new variable that looks at 5 other scale variables and finds
> >> the
> >> > minimum variable among the five with a value greater than or equal to
> >> 0.80.
> >> >
> >> > For example,
> >> >
> >> > CAT1 CAT2 CAT3 CAT4 CAT5
> >> > CASE 1 0.75 0.82 0.85 0.90 0.95
> >> > CASE 2 0.65 0.75 0.82 0.87 0.95
> >> >
> >> > For case 1, the computed variable would return a value of 2. For case
> >> 2,
> >> it
> >> > would return a value of 3.
> >> >
> >> > I'm stumped -- originally I thought it would work with a conditional
> >> MIN
> >> > statement, but it's not working. Thinking it might need an IF/THEN,
> but
> >> I'm
> >> > hoping someone has a more efficient solution. Thanks.
> >> >
> >> >
> >>
> >> ===================== To manage your subscription to SPSSX-L, send a
> >> message to
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text
> >> except the command. To leave the list, send the command SIGNOFF SPSSX-L
> >> For
> >> a list of commands to manage subscriptions, send the command INFO
> REFCARD
> >
> >
> >
> >
> > --
> > Jon K Peck
>
> > jkpeck@
>
> >
> > =====================
> > To manage your subscription to SPSSX-L, send a message to
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text except the
> > command. To leave the list, send the command
> > SIGNOFF SPSSX-L
> > For a list of commands to manage subscriptions, send the command
> > INFO REFCARD
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>



--
Jon K Peck
[hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Jon Peck
Let me state my logic about commenting Python examples.

If I post a Python solution, I will generally only explain the assumptions and what it does.  If someone is already familiar with Python - and lots are - explaining the code is generally redundant.  If there is something particularly subtle about the code, I would generally mention it.

If a user wants to use the Python solution, they can apply it without studying through the code, but it they have a bias against Python, explaining the code won't help.  The effort would be wasted.

If they want to go the Python route and want to go deeper into the code, I'm always happy to explain it.  But it's up to the user to ask.

And I do post Python solutions in situations where standard syntax would work if I think the Python solution is clearer, simpler, or more general.

On Mon, Jun 13, 2016 at 12:24 PM, David Marso <[hidden email]> wrote:
But this isn't a PYTHON list is it.  Maybe a few comments for python
illiterate.
OTOH:  For clients I comment everything so there is no confusion.
Didn't address my question of why bother with python in this simple case
which can be addressed by any number of SIMPLE approaches with BASIC SPSS
code ;-)
Is python always your GOTO solution Jon?
Don't necessarily need to butcher the data with V2C but VECTOR/LOOP goes way
over the head of most noobs.
----



Jon Peck wrote
> Python programmers would not document zip for the same reason that someone
> writing Statistics syntax would not document COMPUTE: it's a basic tool of
> the language.  In this particular case, using Python avoids the violence
> of
> restructuring the data, and, yes, for anyone with a basic knowledge of
> Python, the code is self evident.
>
> On Mon, Jun 13, 2016 at 5:38 AM, David Marso &lt;

> david.marso@

> &gt; wrote:
>
>> One of my basic bitching sessions begins with WHY DON'T PYTHON
>> programmers
>> believe in commenting their code.  I see this crap all the time!!!  Is it
>> somehow self evident WTF is going on?  Maybe throw in a line which states
>> what zip does? OTOH, Why do python for this simple task?  I resort to it
>> only when metadata are required.   Some people seem to go to python as a
>> default without considering basic shit ;-)))
>>
>>
>> Jon Peck wrote
>> > Here's an answer that does not require restructuring the data.  It
>> returns
>> > sysmis if no value meets the threshold.
>> >
>> > begin program.
>> > def casemin(*args):
>> >     z = sorted(zip(args, range(len(args))))
>> >     for item in z:
>> >         if item[0] >= .8:
>> >             return item[1] + 1
>> >     return None
>> > end program.
>> >
>> > spssinc trans result = minindex
>> > /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
>> >
>> > On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich &lt;
>>
>> > rich-ulrich@
>>
>> > &gt; wrote:
>> >
>> >> The neatest logic avoids loops, etc., if you don't mind the file
>> >> processing -
>> >>   Use CaseToVars and save the Index for the variables;
>> >>   SELECT to keep only values GE 0.80;
>> >>   GET FILE  *  and tag FIRST;
>> >>   SELECT to keep the "First"s.  The var-index is the var-number that
>> was
>> >> first.
>> >>
>> >> --
>> >> Rich
>> >>
>> >> > Date: Fri, 10 Jun 2016 14:37:16 -0700
>> >> > From:
>>
>> > ryan.hudes@
>>
>> >> > Subject: Compute function to create variable that meets multiple
>> >> conditions
>> >> > To:
>>
>> > SPSSX-L@.UGA
>>
>> >> >
>> >> > I'm trying to write syntax in SPSS that uses the compute variable
>> >> command to
>> >> > create a new variable that looks at 5 other scale variables and
>> finds
>> >> the
>> >> > minimum variable among the five with a value greater than or equal
>> to
>> >> 0.80.
>> >> >
>> >> > For example,
>> >> >
>> >> > CAT1 CAT2 CAT3 CAT4 CAT5
>> >> > CASE 1 0.75 0.82 0.85 0.90 0.95
>> >> > CASE 2 0.65 0.75 0.82 0.87 0.95
>> >> >
>> >> > For case 1, the computed variable would return a value of 2. For
>> case
>> >> 2,
>> >> it
>> >> > would return a value of 3.
>> >> >
>> >> > I'm stumped -- originally I thought it would work with a conditional
>> >> MIN
>> >> > statement, but it's not working. Thinking it might need an IF/THEN,
>> but
>> >> I'm
>> >> > hoping someone has a more efficient solution. Thanks.
>> >> >
>> >> >
>> >>
>> >> ===================== To manage your subscription to SPSSX-L, send a
>> >> message to
>>
>> > LISTSERV@.UGA
>>
>> >  (not to SPSSX-L), with no body text
>> >> except the command. To leave the list, send the command SIGNOFF
>> SPSSX-L
>> >> For
>> >> a list of commands to manage subscriptions, send the command INFO
>> REFCARD
>> >
>> >
>> >
>> >
>> > --
>> > Jon K Peck
>>
>> > jkpeck@
>>
>> >
>> > =====================
>> > To manage your subscription to SPSSX-L, send a message to
>>
>> > LISTSERV@.UGA
>>
>> >  (not to SPSSX-L), with no body text except the
>> > command. To leave the list, send the command
>> > SIGNOFF SPSSX-L
>> > For a list of commands to manage subscriptions, send the command
>> > INFO REFCARD
>>
>>
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> View this message in context:
>> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
>> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
>> command. To leave the list, send the command
>> SIGNOFF SPSSX-L
>> For a list of commands to manage subscriptions, send the command
>> INFO REFCARD
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732393.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Bruce Weaver
Administrator
I think what David is suggesting (in his inimitable fashion) is that if Python code was routinely commented (even when it is very straightforward for accomplished Pythonistas), some of us Python ignoramuses (both biased and unbiased) might actually learn a thing or two about Python--even if not intentionally.  ;-)  I think that's a fair point, and would also prefer routine commenting.  

Cheers,
Bruce


Jon Peck wrote
Let me state my logic about commenting Python examples.

If I post a Python solution, I will generally only explain the assumptions
and what it does.  If someone is already familiar with Python - and lots
are - explaining the code is generally redundant.  If there is something
particularly subtle about the code, I would generally mention it.

If a user wants to use the Python solution, they can apply it without
studying through the code, but it they have a bias against Python,
explaining the code won't help.  The effort would be wasted.

If they want to go the Python route and want to go deeper into the code,
I'm always happy to explain it.  But it's up to the user to ask.

And I do post Python solutions in situations where standard syntax would
work if I think the Python solution is clearer, simpler, or more general.

On Mon, Jun 13, 2016 at 12:24 PM, David Marso <[hidden email]> wrote:

> But this isn't a PYTHON list is it.  Maybe a few comments for python
> illiterate.
> OTOH:  For clients I comment everything so there is no confusion.
> Didn't address my question of why bother with python in this simple case
> which can be addressed by any number of SIMPLE approaches with BASIC SPSS
> code ;-)
> Is python always your GOTO solution Jon?
> Don't necessarily need to butcher the data with V2C but VECTOR/LOOP goes
> way
> over the head of most noobs.
> ----
>
>
>
> Jon Peck wrote
> > Python programmers would not document zip for the same reason that
> someone
> > writing Statistics syntax would not document COMPUTE: it's a basic tool
> of
> > the language.  In this particular case, using Python avoids the violence
> > of
> > restructuring the data, and, yes, for anyone with a basic knowledge of
> > Python, the code is self evident.
> >
> > On Mon, Jun 13, 2016 at 5:38 AM, David Marso <
>
> > david.marso@
>
> > > wrote:
> >
> >> One of my basic bitching sessions begins with WHY DON'T PYTHON
> >> programmers
> >> believe in commenting their code.  I see this crap all the time!!!  Is
> it
> >> somehow self evident WTF is going on?  Maybe throw in a line which
> states
> >> what zip does? OTOH, Why do python for this simple task?  I resort to it
> >> only when metadata are required.   Some people seem to go to python as a
> >> default without considering basic shit ;-)))
> >>
> >>
> >> Jon Peck wrote
> >> > Here's an answer that does not require restructuring the data.  It
> >> returns
> >> > sysmis if no value meets the threshold.
> >> >
> >> > begin program.
> >> > def casemin(*args):
> >> >     z = sorted(zip(args, range(len(args))))
> >> >     for item in z:
> >> >         if item[0] >= .8:
> >> >             return item[1] + 1
> >> >     return None
> >> > end program.
> >> >
> >> > spssinc trans result = minindex
> >> > /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
> >> >
> >> > On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <
> >>
> >> > rich-ulrich@
> >>
> >> > > wrote:
> >> >
> >> >> The neatest logic avoids loops, etc., if you don't mind the file
> >> >> processing -
> >> >>   Use CaseToVars and save the Index for the variables;
> >> >>   SELECT to keep only values GE 0.80;
> >> >>   GET FILE  *  and tag FIRST;
> >> >>   SELECT to keep the "First"s.  The var-index is the var-number that
> >> was
> >> >> first.
> >> >>
> >> >> --
> >> >> Rich
> >> >>
> >> >> > Date: Fri, 10 Jun 2016 14:37:16 -0700
> >> >> > From:
> >>
> >> > ryan.hudes@
> >>
> >> >> > Subject: Compute function to create variable that meets multiple
> >> >> conditions
> >> >> > To:
> >>
> >> > SPSSX-L@.UGA
> >>
> >> >> >
> >> >> > I'm trying to write syntax in SPSS that uses the compute variable
> >> >> command to
> >> >> > create a new variable that looks at 5 other scale variables and
> >> finds
> >> >> the
> >> >> > minimum variable among the five with a value greater than or equal
> >> to
> >> >> 0.80.
> >> >> >
> >> >> > For example,
> >> >> >
> >> >> > CAT1 CAT2 CAT3 CAT4 CAT5
> >> >> > CASE 1 0.75 0.82 0.85 0.90 0.95
> >> >> > CASE 2 0.65 0.75 0.82 0.87 0.95
> >> >> >
> >> >> > For case 1, the computed variable would return a value of 2. For
> >> case
> >> >> 2,
> >> >> it
> >> >> > would return a value of 3.
> >> >> >
> >> >> > I'm stumped -- originally I thought it would work with a
> conditional
> >> >> MIN
> >> >> > statement, but it's not working. Thinking it might need an IF/THEN,
> >> but
> >> >> I'm
> >> >> > hoping someone has a more efficient solution. Thanks.
> >> >> >
> >> >> >
> >> >>
> >> >> ===================== To manage your subscription to SPSSX-L, send a
> >> >> message to
> >>
> >> > LISTSERV@.UGA
> >>
> >> >  (not to SPSSX-L), with no body text
> >> >> except the command. To leave the list, send the command SIGNOFF
> >> SPSSX-L
> >> >> For
> >> >> a list of commands to manage subscriptions, send the command INFO
> >> REFCARD
> >> >
> >> >
> >> >
> >> >
> >> > --
> >> > Jon K Peck
> >>
> >> > jkpeck@
> >>
> >> >
> >> > =====================
> >> > To manage your subscription to SPSSX-L, send a message to
> >>
> >> > LISTSERV@.UGA
> >>
> >> >  (not to SPSSX-L), with no body text except the
> >> > command. To leave the list, send the command
> >> > SIGNOFF SPSSX-L
> >> > For a list of commands to manage subscriptions, send the command
> >> > INFO REFCARD
> >>
> >>
> >>
> >>
> >>
> >> -----
> >> Please reply to the list and not to my personal email.
> >> Those desiring my consulting or training services please feel free to
> >> email me.
> >> ---
> >> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
> >> porcos
> >> ne forte conculcent eas pedibus suis."
> >> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
> >> in
> >> abyssum?"
> >> --
> >> View this message in context:
> >>
> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
> >> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
> >>
> >> =====================
> >> To manage your subscription to SPSSX-L, send a message to
> >>
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text except the
> >> command. To leave the list, send the command
> >> SIGNOFF SPSSX-L
> >> For a list of commands to manage subscriptions, send the command
> >> INFO REFCARD
> >>
> >
> >
> >
> > --
> > Jon K Peck
>
> > jkpeck@
>
> >
> > =====================
> > To manage your subscription to SPSSX-L, send a message to
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text except the
> > command. To leave the list, send the command
> > SIGNOFF SPSSX-L
> > For a list of commands to manage subscriptions, send the command
> > INFO REFCARD
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732393.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>



--
Jon K Peck
[hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

David Marso
Administrator
Wow Bruce,
I couldn't have stated it better ;-)
SPSS coded could be better commented as well -Guilty, but my code is so logical and straight forward that it really doesn't need any comments -Actually my production code for paying clients is impeccably documented  and robust as a rock.
Chuckle, David

On Mon, Jun 13, 2016 at 8:42 PM, Bruce Weaver [via SPSSX Discussion] <[hidden email]> wrote:
I think what David is suggesting (in his inimitable fashion) is that if Python code was routinely commented (even when it is very straightforward for accomplished Pythonistas), some of us Python ignoramuses (both biased and unbiased) might actually learn a thing or two about Python--even if not intentionally.  ;-)  I think that's a fair point, and would also prefer routine commenting.  

Cheers,
Bruce


Jon Peck wrote
Let me state my logic about commenting Python examples.

If I post a Python solution, I will generally only explain the assumptions
and what it does.  If someone is already familiar with Python - and lots
are - explaining the code is generally redundant.  If there is something
particularly subtle about the code, I would generally mention it.

If a user wants to use the Python solution, they can apply it without
studying through the code, but it they have a bias against Python,
explaining the code won't help.  The effort would be wasted.

If they want to go the Python route and want to go deeper into the code,
I'm always happy to explain it.  But it's up to the user to ask.

And I do post Python solutions in situations where standard syntax would
work if I think the Python solution is clearer, simpler, or more general.

On Mon, Jun 13, 2016 at 12:24 PM, David Marso <[hidden email]> wrote:

> But this isn't a PYTHON list is it.  Maybe a few comments for python
> illiterate.
> OTOH:  For clients I comment everything so there is no confusion.
> Didn't address my question of why bother with python in this simple case
> which can be addressed by any number of SIMPLE approaches with BASIC SPSS
> code ;-)
> Is python always your GOTO solution Jon?
> Don't necessarily need to butcher the data with V2C but VECTOR/LOOP goes
> way
> over the head of most noobs.
> ----
>
>
>
> Jon Peck wrote
> > Python programmers would not document zip for the same reason that
> someone
> > writing Statistics syntax would not document COMPUTE: it's a basic tool
> of
> > the language.  In this particular case, using Python avoids the violence
> > of
> > restructuring the data, and, yes, for anyone with a basic knowledge of
> > Python, the code is self evident.
> >
> > On Mon, Jun 13, 2016 at 5:38 AM, David Marso <
>
> > david.marso@
>

> > > wrote:
> >
> >> One of my basic bitching sessions begins with WHY DON'T PYTHON
> >> programmers
> >> believe in commenting their code.  I see this crap all the time!!!  Is
> it
> >> somehow self evident WTF is going on?  Maybe throw in a line which
> states
> >> what zip does? OTOH, Why do python for this simple task?  I resort to it
> >> only when metadata are required.   Some people seem to go to python as a
> >> default without considering basic shit ;-)))
> >>
> >>
> >> Jon Peck wrote
> >> > Here's an answer that does not require restructuring the data.  It
> >> returns
> >> > sysmis if no value meets the threshold.
> >> >
> >> > begin program.
> >> > def casemin(*args):
> >> >     z = sorted(zip(args, range(len(args))))
> >> >     for item in z:
> >> >         if item[0] >= .8:
> >> >             return item[1] + 1
> >> >     return None
> >> > end program.
> >> >
> >> > spssinc trans result = minindex
> >> > /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
> >> >
> >> > On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <
> >>
> >> > rich-ulrich@
> >>

> >> > > wrote:
> >> >
> >> >> The neatest logic avoids loops, etc., if you don't mind the file
> >> >> processing -
> >> >>   Use CaseToVars and save the Index for the variables;
> >> >>   SELECT to keep only values GE 0.80;
> >> >>   GET FILE  *  and tag FIRST;
> >> >>   SELECT to keep the "First"s.  The var-index is the var-number that
> >> was
> >> >> first.
> >> >>
> >> >> --
> >> >> Rich
> >> >>
> >> >> > Date: Fri, 10 Jun 2016 14:37:16 -0700
> >> >> > From:
> >>
> >> > ryan.hudes@
> >>
> >> >> > Subject: Compute function to create variable that meets multiple
> >> >> conditions
> >> >> > To:
> >>
> >> > SPSSX-L@.UGA
> >>
> >> >> >
> >> >> > I'm trying to write syntax in SPSS that uses the compute variable
> >> >> command to
> >> >> > create a new variable that looks at 5 other scale variables and
> >> finds
> >> >> the
> >> >> > minimum variable among the five with a value greater than or equal
> >> to
> >> >> 0.80.
> >> >> >
> >> >> > For example,
> >> >> >
> >> >> > CAT1 CAT2 CAT3 CAT4 CAT5
> >> >> > CASE 1 0.75 0.82 0.85 0.90 0.95
> >> >> > CASE 2 0.65 0.75 0.82 0.87 0.95
> >> >> >
> >> >> > For case 1, the computed variable would return a value of 2. For
> >> case
> >> >> 2,
> >> >> it
> >> >> > would return a value of 3.
> >> >> >
> >> >> > I'm stumped -- originally I thought it would work with a
> conditional
> >> >> MIN
> >> >> > statement, but it's not working. Thinking it might need an IF/THEN,
> >> but
> >> >> I'm
> >> >> > hoping someone has a more efficient solution. Thanks.
> >> >> >
> >> >> >
> >> >>
> >> >> ===================== To manage your subscription to SPSSX-L, send a
> >> >> message to
> >>
> >> > LISTSERV@.UGA
> >>
> >> >  (not to SPSSX-L), with no body text
> >> >> except the command. To leave the list, send the command SIGNOFF
> >> SPSSX-L
> >> >> For
> >> >> a list of commands to manage subscriptions, send the command INFO
> >> REFCARD
> >> >
> >> >
> >> >
> >> >
> >> > --
> >> > Jon K Peck
> >>
> >> > jkpeck@
> >>
> >> >
> >> > =====================
> >> > To manage your subscription to SPSSX-L, send a message to
> >>
> >> > LISTSERV@.UGA
> >>
> >> >  (not to SPSSX-L), with no body text except the
> >> > command. To leave the list, send the command
> >> > SIGNOFF SPSSX-L
> >> > For a list of commands to manage subscriptions, send the command
> >> > INFO REFCARD
> >>
> >>
> >>
> >>
> >>
> >> -----
> >> Please reply to the list and not to my personal email.
> >> Those desiring my consulting or training services please feel free to
> >> email me.
> >> ---
> >> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
> >> porcos
> >> ne forte conculcent eas pedibus suis."
> >> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
> >> in
> >> abyssum?"
> >> --
> >> View this message in context:
> >>
> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
> >> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
> >>
> >> =====================
> >> To manage your subscription to SPSSX-L, send a message to
> >>
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text except the
> >> command. To leave the list, send the command
> >> SIGNOFF SPSSX-L
> >> For a list of commands to manage subscriptions, send the command
> >> INFO REFCARD
> >>
> >
> >
> >
> > --
> > Jon K Peck
>
> > jkpeck@
>
> >
> > =====================
> > To manage your subscription to SPSSX-L, send a message to
>
> > LISTSERV@.UGA
>
> >  (not to SPSSX-L), with no body text except the
> > command. To leave the list, send the command
> > SIGNOFF SPSSX-L
> > For a list of commands to manage subscriptions, send the command
> > INFO REFCARD
>
>
>
>
>
> -----
> Please reply to the list and not to my personal email.
> Those desiring my consulting or training services please feel free to
> email me.
> ---
> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos
> ne forte conculcent eas pedibus suis."
> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in
> abyssum?"
> --
> View this message in context:
> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732393.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>



--
Jon K Peck
[hidden email]

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.



To unsubscribe from Compute function to create variable that meets multiple conditions, click here.
NAML

Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Andy W
In reply to this post by Bruce Weaver
A few points:

 - It is fine to for people to post multiple solutions to the same problem.
 - You will not learn python from anyone documenting basic functions. Same as you wouldn't learn SPSS syntax if you replicate the help for functions like COMPUTE, AGGREGATE, VARSTOCASES, etc.
 - For the most part, none of the regular posters document short code snippets. (This is IMO ok, we aren't building empires, just helping people with specific problems.)
 - If you have a question about the code - feel free to ask for elaboration

The best way to learn what the python snippets are doing is to re-run them yourself. Ditto for the SPSS code snippets. And no, for neophytes none of the code snippets we post are innately obvious, python or SPSS syntax. Thinking otherwise is just kidding yourself.

If you want to play around with python right from SPSS, simply do something like:

BEGIN PROGRAM Python.
args = ['a','b','c']
two = range(len(args))
print two
print zip(args,two)
END PROGRAM.

That gets you about half-way to knowing how Jon's python function works. The other half is understanding how the sorted() function works, and then how the for loop with return in the inside of it works. None of this is rocket science, but it takes actual effort to read and understand it.

Now would documenting that zip takes its inputs and returns a list of nested tuples made of consecutive items in those lists make you understand python any better?
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Jon Peck
In reply to this post by Bruce Weaver
My belief is that the anti-Pythonistas on this list just route anything that mentions Python straight to the bit bucket.  If readers would like more information, they can and do ask questions.

If code - Python, R, Basic, or traditional syntax were to be more fully commented in postings, that raises the question of what documentation or comments would be appropriate.  Production code does not have the pedagogical aspect that might fit posted examples, so it is not documented in the same way that small examples such as might be posted on this list might benefit from.  But it wouldn't make sense to explain any of these technologies from the very beginning, either.  So, what should be assumed about the reader?

If you look at code posted on other similar lists, commenting is typically very sparse to nonexistent other than to explain specific, interesting constructs that are the subject of the post'

There are, of course, vast resources on- and off-line that explain all of these technologies.  I'm willing to explain things where it is useful, but where to draw the line?

On Mon, Jun 13, 2016 at 6:42 PM, Bruce Weaver <[hidden email]> wrote:
I think what David is suggesting (in his inimitable fashion) is that if
Python code was routinely commented (even when it is very straightforward
for accomplished Pythonistas), some of us Python ignoramuses (both biased
and unbiased) might actually learn a thing or two about Python--even if not
intentionally.  ;-)  I think that's a fair point, and would also prefer
routine commenting.

Cheers,
Bruce



Jon Peck wrote
> Let me state my logic about commenting Python examples.
>
> If I post a Python solution, I will generally only explain the assumptions
> and what it does.  If someone is already familiar with Python - and lots
> are - explaining the code is generally redundant.  If there is something
> particularly subtle about the code, I would generally mention it.
>
> If a user wants to use the Python solution, they can apply it without
> studying through the code, but it they have a bias against Python,
> explaining the code won't help.  The effort would be wasted.
>
> If they want to go the Python route and want to go deeper into the code,
> I'm always happy to explain it.  But it's up to the user to ask.
>
> And I do post Python solutions in situations where standard syntax would
> work if I think the Python solution is clearer, simpler, or more general.
>
> On Mon, Jun 13, 2016 at 12:24 PM, David Marso <

> david.marso@

> > wrote:
>
>> But this isn't a PYTHON list is it.  Maybe a few comments for python
>> illiterate.
>> OTOH:  For clients I comment everything so there is no confusion.
>> Didn't address my question of why bother with python in this simple case
>> which can be addressed by any number of SIMPLE approaches with BASIC SPSS
>> code ;-)
>> Is python always your GOTO solution Jon?
>> Don't necessarily need to butcher the data with V2C but VECTOR/LOOP goes
>> way
>> over the head of most noobs.
>> ----
>>
>>
>>
>> Jon Peck wrote
>> > Python programmers would not document zip for the same reason that
>> someone
>> > writing Statistics syntax would not document COMPUTE: it's a basic tool
>> of
>> > the language.  In this particular case, using Python avoids the
>> violence
>> > of
>> > restructuring the data, and, yes, for anyone with a basic knowledge of
>> > Python, the code is self evident.
>> >
>> > On Mon, Jun 13, 2016 at 5:38 AM, David Marso <
>>
>> > david.marso@
>>
>> > > wrote:
>> >
>> >> One of my basic bitching sessions begins with WHY DON'T PYTHON
>> >> programmers
>> >> believe in commenting their code.  I see this crap all the time!!!  Is
>> it
>> >> somehow self evident WTF is going on?  Maybe throw in a line which
>> states
>> >> what zip does? OTOH, Why do python for this simple task?  I resort to
>> it
>> >> only when metadata are required.   Some people seem to go to python as
>> a
>> >> default without considering basic shit ;-)))
>> >>
>> >>
>> >> Jon Peck wrote
>> >> > Here's an answer that does not require restructuring the data.  It
>> >> returns
>> >> > sysmis if no value meets the threshold.
>> >> >
>> >> > begin program.
>> >> > def casemin(*args):
>> >> >     z = sorted(zip(args, range(len(args))))
>> >> >     for item in z:
>> >> >         if item[0] >= .8:
>> >> >             return item[1] + 1
>> >> >     return None
>> >> > end program.
>> >> >
>> >> > spssinc trans result = minindex
>> >> > /formula "casemin(CAT1, CAT2, CAT3, CAT4, CAT5)".
>> >> >
>> >> > On Fri, Jun 10, 2016 at 5:28 PM, Rich Ulrich <
>> >>
>> >> > rich-ulrich@
>> >>
>> >> > > wrote:
>> >> >
>> >> >> The neatest logic avoids loops, etc., if you don't mind the file
>> >> >> processing -
>> >> >>   Use CaseToVars and save the Index for the variables;
>> >> >>   SELECT to keep only values GE 0.80;
>> >> >>   GET FILE  *  and tag FIRST;
>> >> >>   SELECT to keep the "First"s.  The var-index is the var-number
>> that
>> >> was
>> >> >> first.
>> >> >>
>> >> >> --
>> >> >> Rich
>> >> >>
>> >> >> > Date: Fri, 10 Jun 2016 14:37:16 -0700
>> >> >> > From:
>> >>
>> >> > ryan.hudes@
>> >>
>> >> >> > Subject: Compute function to create variable that meets multiple
>> >> >> conditions
>> >> >> > To:
>> >>
>> >> > SPSSX-L@.UGA
>> >>
>> >> >> >
>> >> >> > I'm trying to write syntax in SPSS that uses the compute variable
>> >> >> command to
>> >> >> > create a new variable that looks at 5 other scale variables and
>> >> finds
>> >> >> the
>> >> >> > minimum variable among the five with a value greater than or
>> equal
>> >> to
>> >> >> 0.80.
>> >> >> >
>> >> >> > For example,
>> >> >> >
>> >> >> > CAT1 CAT2 CAT3 CAT4 CAT5
>> >> >> > CASE 1 0.75 0.82 0.85 0.90 0.95
>> >> >> > CASE 2 0.65 0.75 0.82 0.87 0.95
>> >> >> >
>> >> >> > For case 1, the computed variable would return a value of 2. For
>> >> case
>> >> >> 2,
>> >> >> it
>> >> >> > would return a value of 3.
>> >> >> >
>> >> >> > I'm stumped -- originally I thought it would work with a
>> conditional
>> >> >> MIN
>> >> >> > statement, but it's not working. Thinking it might need an
>> IF/THEN,
>> >> but
>> >> >> I'm
>> >> >> > hoping someone has a more efficient solution. Thanks.
>> >> >> >
>> >> >> >
>> >> >>
>> >> >> ===================== To manage your subscription to SPSSX-L, send
>> a
>> >> >> message to
>> >>
>> >> > LISTSERV@.UGA
>> >>
>> >> >  (not to SPSSX-L), with no body text
>> >> >> except the command. To leave the list, send the command SIGNOFF
>> >> SPSSX-L
>> >> >> For
>> >> >> a list of commands to manage subscriptions, send the command INFO
>> >> REFCARD
>> >> >
>> >> >
>> >> >
>> >> >
>> >> > --
>> >> > Jon K Peck
>> >>
>> >> > jkpeck@
>> >>
>> >> >
>> >> > =====================
>> >> > To manage your subscription to SPSSX-L, send a message to
>> >>
>> >> > LISTSERV@.UGA
>> >>
>> >> >  (not to SPSSX-L), with no body text except the
>> >> > command. To leave the list, send the command
>> >> > SIGNOFF SPSSX-L
>> >> > For a list of commands to manage subscriptions, send the command
>> >> > INFO REFCARD
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> -----
>> >> Please reply to the list and not to my personal email.
>> >> Those desiring my consulting or training services please feel free to
>> >> email me.
>> >> ---
>> >> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> >> porcos
>> >> ne forte conculcent eas pedibus suis."
>> >> Cum es damnatorum possederunt porcos iens ut salire off sanguinum
>> cliff
>> >> in
>> >> abyssum?"
>> >> --
>> >> View this message in context:
>> >>
>> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732389.html
>> >> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>> >>
>> >> =====================
>> >> To manage your subscription to SPSSX-L, send a message to
>> >>
>>
>> > LISTSERV@.UGA
>>
>> >  (not to SPSSX-L), with no body text except the
>> >> command. To leave the list, send the command
>> >> SIGNOFF SPSSX-L
>> >> For a list of commands to manage subscriptions, send the command
>> >> INFO REFCARD
>> >>
>> >
>> >
>> >
>> > --
>> > Jon K Peck
>>
>> > jkpeck@
>>
>> >
>> > =====================
>> > To manage your subscription to SPSSX-L, send a message to
>>
>> > LISTSERV@.UGA
>>
>> >  (not to SPSSX-L), with no body text except the
>> > command. To leave the list, send the command
>> > SIGNOFF SPSSX-L
>> > For a list of commands to manage subscriptions, send the command
>> > INFO REFCARD
>>
>>
>>
>>
>>
>> -----
>> Please reply to the list and not to my personal email.
>> Those desiring my consulting or training services please feel free to
>> email me.
>> ---
>> "Nolite dare sanctum canibus neque mittatis margaritas vestras ante
>> porcos
>> ne forte conculcent eas pedibus suis."
>> Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff
>> in
>> abyssum?"
>> --
>> View this message in context:
>> http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732393.html
>> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>>
>> =====================
>> To manage your subscription to SPSSX-L, send a message to
>>

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
>> command. To leave the list, send the command
>> SIGNOFF SPSSX-L
>> For a list of commands to manage subscriptions, send the command
>> INFO REFCARD
>>
>
>
>
> --
> Jon K Peck

> jkpeck@

>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD





-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.

--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Compute-function-to-create-variable-that-meets-multiple-conditions-tp5732380p5732395.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD



--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Compute function to create variable that meets multiple conditions

Bruce Weaver
Administrator
In reply to this post by Andy W
I know that some subscribers don't actually have access to SPSS.  For their benefit, here is Andy's program again with the output it generates.

BEGIN PROGRAM Python.
args = ['a','b','c']
two = range(len(args))
print two
print zip(args,two)  
END PROGRAM.

OUTPUT:

[0, 1, 2]
[('a', 0), ('b', 1), ('c', 2)]



Andy W wrote
A few points:

 - It is fine to for people to post multiple solutions to the same problem.
 - You will not learn python from anyone documenting basic functions. Same as you wouldn't learn SPSS syntax if you replicate the help for functions like COMPUTE, AGGREGATE, VARSTOCASES, etc.
 - For the most part, none of the regular posters document short code snippets. (This is IMO ok, we aren't building empires, just helping people with specific problems.)
 - If you have a question about the code - feel free to ask for elaboration

The best way to learn what the python snippets are doing is to re-run them yourself. Ditto for the SPSS code snippets. And no, for neophytes none of the code snippets we post are innately obvious, python or SPSS syntax. Thinking otherwise is just kidding yourself.

If you want to play around with python right from SPSS, simply do something like:

BEGIN PROGRAM Python.
args = ['a','b','c']
two = range(len(args))
print two
print zip(args,two)
END PROGRAM.

That gets you about half-way to knowing how Jon's python function works. The other half is understanding how the sorted() function works, and then how the for loop with return in the inside of it works. None of this is rocket science, but it takes actual effort to read and understand it.

Now would documenting that zip takes its inputs and returns a list of nested tuples made of consecutive items in those lists make you understand python any better?
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).