minimum value by order for several variable

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

minimum value by order for several variable

Fahad
Hi,

I have a data like this

variable: X1   X2   X3  X4  X5  X6  X7  X8
case1 :     21   23         14        30

for those variables that don't have any number we can consider them missing.

what I'm trying to do is to creat new variables that will rank the values from the minimum so the result should be like this

the new variables that will be created is x9 x10 x11 x12 because we have only 4 values in case1

x9  x10  x11  x12
14  21    23    30

so the final result should be like this

variable: X1   X2   X3  X4  X5  X6  X7  X8  x9  x10  x11  x12
case1 :   21   23         14        30              14  21    23    30


Thanks to you all for helping.

Regards


 
Reply | Threaded
Open this post in threaded view
|

Re: minimum value by order for several variable

Ruben Geert van den Berg
Dear Fahad,

If you have the Python essentials installed, you could try the code below. For each variable you specify, it will create a new variabe whose name is (a prefix + the old variable name). So if you'd specify "s_" as a prefix and "x1 to x8" as variables, it will create "s_x1 to s_x8" as 8 new variables. Please make sure these do not already exist. The new variables will hold the sorted values of x1-x8. Both user missing and system missing values are moved to the end.

HTH,

Ruben

begin program.
variables='v1 to v5' # Specify your range of variables here
pref='s_' # Specify your prefix here
import spssaux,spss
def tkey(x):
    return(x if x else float("inf"))
mydict=spssaux.VariableDict()
varlist=mydict.expand(variables)
first=mydict.VariableIndex(varlist[0])
last=mydict.VariableIndex(varlist[-1])
curs=spss.Cursor(accessType="w")
curs.SetVarNameAndType([pref+var for var in varlist],[0 for num in range(len(varlist))])
curs.CommitDictionary()
for a in range(curs.GetCaseCount()):
    case = sorted(curs.fetchone()[first:last+1],key=tkey)
    for num in range(len(varlist)):
        if case[num]:
            curs.SetValueNumeric(pref+varlist[num],case[num])
    curs.CommitCase()
curs.close()
end program.

> Date: Tue, 6 Nov 2012 23:24:37 -0800

> From: [hidden email]
> Subject: minimum value by order for several variable
> To: [hidden email]
>
> Hi,
>
> I have a data like this
>
> variable: X1 X2 X3 X4 X5 X6 X7 X8
> case1 : 21 23 14 30
>
> for those variables that don't have any number we can consider them missing.
>
> what I'm trying to do is to creat new variables that will rank the values
> from the minimum so the result should be like this
>
> the new variables that will be created is x9 x10 x11 x12 because we have
> only 4 values in case1
>
> x9 x10 x11 x12
> 14 21 23 30
>
> so the final result should be like this
>
> variable: X1 X2 X3 X4 X5 X6 X7 X8 x9 x10 x11 x12
> case1 : 21 23 14 30 14 21 23 30
>
>
> Thanks to you all for helping.
>
> Regards
>
>
>
>
>
>
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/minimum-value-by-order-for-several-variable-tp5716067.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
Reply | Threaded
Open this post in threaded view
|

Re: minimum value by order for several variable

Fahad

Dear Ruben,

Thanks for your replay, unfortunately  I don't have Python essentials installed and there for I think I can't use the code that you provided.

I thought that it will be easy an easy syntax as I could do it in Excel using only one function (SMALL) but the data that I have is big.

Is there any other was to use spss syntax.

Thanks again for your help.

Fahad
Reply | Threaded
Open this post in threaded view
|

Re: minimum value by order for several variable

David Marso
Administrator
See VARSTOCASES ....SORT ... CASESTOVARS ... MATCH
Leaving it to you to explore!
Fahad wrote
Dear Ruben,

Thanks for your replay, unfortunately  I don't have Python essentials installed and there for I think I can't use the code that you provided.

I thought that it will be easy an easy syntax as I could do it in Excel using only one function (SMALL) but the data that I have is big.

Is there any other was to use spss syntax.

Thanks again for your help.

Fahad
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: minimum value by order for several variable

Jon K Peck
In reply to this post by Fahad
The Python Essentials are free and can be downloaded from the SPSS Community site at www.ibm.com/developerworks/spssdevcentral
if desired.

Here's a very simple Python solution using the SPSSINC TRANS extension command also available from that site.

spssinc trans result = X9 to X16
/formula "sorted([X1,X2,X3,X4,X5,X6,X7,X8])".

In this case, missing values would be placed at the start of the list.  To force them to be at the end, you could do this.

begin program.
def sort(*args):
    return sorted([item for item in args if not item is None])
end program.

spssinc trans result = X9 to X16
/formula "sort(X1,X2,X3,X4,X5,X6,X7,X8)".

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Fahad <[hidden email]>
To:        [hidden email],
Date:        11/07/2012 04:46 AM
Subject:        Re: [SPSSX-L] minimum value by order for several variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Dear Ruben,

Thanks for your replay, unfortunately  I don't have Python essentials
installed and there for I think I can't use the code that you provided.

I thought that it will be easy an easy syntax as I could do it in Excel
using only one function (SMALL) but the data that I have is big.

Is there any other was to use spss syntax.

Thanks again for your help.

Fahad




--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/minimum-value-by-order-for-several-variable-tp5716067p5716074.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