Sort values stored in different variables

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

Sort values stored in different variables

Luigi Neri
Hi group,

I would like to know how to sort values stored in different variables in SPSS.
I try to make myself clearer :

Suppose I have the following variables : A, B, C, D, E and I have n records

      A     B      C     D     E 
1    a1    b1    c1    d1    e1
2    a2    b2    c2    d2    e2
3    a3    b3    c3    d3    e3
4    a4    b4    c4    d4    e4
5    a5    b5    c5    d5    e5
...                           
            
1    an    bn    cn    dn    en


I want to change the order of the values in each row. So let's consider row ith. Suppose that  this is the order :

bi > ci > ai > ei > di

I want to put in the variable A bi in the variable B ci in the variable C ai etc ...

I would like to use VECTORS and LOOPS and IF to do that but I cannot understand how those structures work in SPSS. I looked for resources on the internet but I cannot understand the syntax. Could you please help me ?

Many thanks

Luigi

Reply | Threaded
Open this post in threaded view
|

Re: Sort values stored in different variables

Jon K Peck
Here is a simple solution using programmability.  It requires that you install the Python Essentials and the SPSSINC TRANS extension command downloadable from the SPSS Community (www.ibm.com/developerworks/spssdevcentral).

* make some data.
data list free /A B C D E.
begin data.
1 4 6 8 10
10 2 3 5 1
end data.

* define the transformation function.
begin program.
def f(a,b,c,d,e):
    return sorted([a,b,c,d,e], reverse=True)  # this line must be indented
end program.

* tranform the data.  Note that the variable names are case sensitive.
SPSSINC TRANS RESULT=A B C D E
/FORMULA f(A,B,C,D,E).

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




From:        Luigi Neri <[hidden email]>
To:        [hidden email]
Date:        09/23/2011 09:53 AM
Subject:        [SPSSX-L] Sort values stored in different variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi group,

I would like to know how to sort values stored in different variables in SPSS.
I try to make myself clearer :

Suppose I have the following variables : A, B, C, D, E and I have n records

      A     B      C     D     E 
1    a1    b1    c1    d1    e1
2    a2    b2    c2    d2    e2
3    a3    b3    c3    d3    e3
4    a4    b4    c4    d4    e4
5    a5    b5    c5    d5    e5
...                           

            
1    an    bn    cn    dn    en


I want to change the order of the values in each row. So let's consider row ith. Suppose that  this is the order :

bi > ci > ai > ei > di

I want to put in the variable A bi in the variable B ci in the variable C ai etc ...

I would like to use VECTORS and LOOPS and IF to do that but I cannot understand how those structures work in SPSS. I looked for resources on the internet but I cannot understand the syntax. Could you please help me ?

Many thanks


Luigi

Reply | Threaded
Open this post in threaded view
|

Re: Sort values stored in different variables

Bruce Weaver
Administrator
In reply to this post by Luigi Neri
If you want a native SPSS solution, there's a bubble sort example on Raynald's site (posted by Mike Lacy) that could  be modified.  

http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingValuesWithinCases_Bubble-Sort.txt

For your example, I think it would be something like:

* Sort A to E into ascending order .
vector x = A to E .
loop #i = 1 to 4 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 5 .
      do if x(#i) GE x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

HTH.


Luigi Neri wrote
Hi group,

I would like to know how to sort values stored in different variables in
SPSS.
I try to make myself clearer :

Suppose I have the following variables : A, B, C, D, E and I have n records

      A     B      C     D     E
1    a1    b1    c1    d1    e1
2    a2    b2    c2    d2    e2
3    a3    b3    c3    d3    e3
4    a4    b4    c4    d4    e4
5    a5    b5    c5    d5    e5
...

1    an    bn    cn    dn    en


I want to change the order of the values in each row. So let's consider row
ith. Suppose that  this is the order :

bi > ci > ai > ei > di

I want to put in the variable A bi in the variable B ci in the variable C ai
etc ...

I would like to use VECTORS and LOOPS and IF to do that but I cannot
understand how those structures work in SPSS. I looked for resources on the
internet but I cannot understand the syntax. Could you please help me ?

Many thanks

Luigi
--
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: Sort values stored in different variables

David Marso
Administrator
Or if you just want a native KISS:
VARSTOCASES.
SORT.
CASESTOVARS.
Leaving it to OP to fill in the blanks.
--
Bruce Weaver wrote
If you want a native SPSS solution, there's a bubble sort example on Raynald's site (posted by Mike Lacy) that could  be modified.  

http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingValuesWithinCases_Bubble-Sort.txt

For your example, I think it would be something like:

* Sort A to E into ascending order .
vector x = A to E .
loop #i = 1 to 4 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 5 .
      do if x(#i) GE x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

HTH.


Luigi Neri wrote
Hi group,

I would like to know how to sort values stored in different variables in
SPSS.
I try to make myself clearer :

Suppose I have the following variables : A, B, C, D, E and I have n records

      A     B      C     D     E
1    a1    b1    c1    d1    e1
2    a2    b2    c2    d2    e2
3    a3    b3    c3    d3    e3
4    a4    b4    c4    d4    e4
5    a5    b5    c5    d5    e5
...

1    an    bn    cn    dn    en


I want to change the order of the values in each row. So let's consider row
ith. Suppose that  this is the order :

bi > ci > ai > ei > di

I want to put in the variable A bi in the variable B ci in the variable C ai
etc ...

I would like to use VECTORS and LOOPS and IF to do that but I cannot
understand how those structures work in SPSS. I looked for resources on the
internet but I cannot understand the syntax. Could you please help me ?

Many thanks

Luigi
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?"