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. |
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.
--
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/). |
In reply to this post by rph523
The neatest logic avoids loops, etc., if you don't mind the file processing -
=====================
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
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. > > |
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).
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?" |
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. |
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?
|
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:
|
Thank you!! This scenario worked perfectly. Very much appreciated.
|
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 ;-)))
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?" |
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 |
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 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/). |
In reply to this post by rph523
Assuming the var-names match your own var-names, ...
=====================
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
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. > > |
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. ----
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?" |
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 |
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
--
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/). |
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.
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?" |
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? |
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 |
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)]
--
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/). |
Free forum by Nabble | Edit this page |