Sorting Variables Within Case

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

Sorting Variables Within Case

Edgar F. Johns
I'm trying to sort a set of six variables within a case and compute a new
variable that records the sorted position (in descending order) of the six
variable.



I found the following code (in Raynald's SPSS Tools) below that will sort
the variables within a case. I tried it and it does sort but, it re-arranges
the values of the variables.



From:
(http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingVal
uesWithinCases_Bubble-Sort.txt)



* Sort X1 to X10 into ascending order .
vector x = x1 to x10 .
loop #i = 1 to 9 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 10 .
      do if x(#i) >= x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



The author alludes to being able to sort multiple variables within a case. I
tried a few ways to accomplish this with no luck. Can someone show me how to
sort on one variable and record the order in a separate variable as
described in the following:

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
new set of variables, First.Sixth, that tells me which Pred variable was
highest, second highest, etc.






Pred1

Pred2

Pred3

Pred4

Pred5

Pred6


S1

121.63

123.92

121.49

115.15

116.58

118.22


S2

96.335

109.041

108.981

89.012

104.545

116.293



















FIRST

SECOND

THIRD

FOURTH

FIFTH

SIXTH


S1

2

1

3

6

5

4


S2

6

2

3

5

1

4



Thanks.

Edgar

---

Discover Technologies

42020 Koppernick Rd.

Suite 204

Canton, MI 48187

(734) 564-4964

(734) 468-0800 fax
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Variables Within Case

Peck, Jon
It would be easy to do this with programmability.  Do you have at least SPSS 14 with the programmability plug-in installed?

And, if so, what do you want the new variables to have for values - the names of the input variables in rank order, an index to the variable names, or names related to the originals but containing the rank within case?

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edgar F. Johns
Sent: Wednesday, February 07, 2007 7:27 AM
To: [hidden email]
Subject: [SPSSX-L] Sorting Variables Within Case

I'm trying to sort a set of six variables within a case and compute a new
variable that records the sorted position (in descending order) of the six
variable.



I found the following code (in Raynald's SPSS Tools) below that will sort
the variables within a case. I tried it and it does sort but, it re-arranges
the values of the variables.



From:
(http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingVal
uesWithinCases_Bubble-Sort.txt)



* Sort X1 to X10 into ascending order .
vector x = x1 to x10 .
loop #i = 1 to 9 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 10 .
      do if x(#i) >= x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



The author alludes to being able to sort multiple variables within a case. I
tried a few ways to accomplish this with no luck. Can someone show me how to
sort on one variable and record the order in a separate variable as
described in the following:

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
new set of variables, First.Sixth, that tells me which Pred variable was
highest, second highest, etc.






Pred1

Pred2

Pred3

Pred4

Pred5

Pred6


S1

121.63

123.92

121.49

115.15

116.58

118.22


S2

96.335

109.041

108.981

89.012

104.545

116.293



















FIRST

SECOND

THIRD

FOURTH

FIFTH

SIXTH


S1

2

1

3

6

5

4


S2

6

2

3

5

1

4



Thanks.

Edgar

---

Discover Technologies

42020 Koppernick Rd.

Suite 204

Canton, MI 48187

(734) 564-4964

(734) 468-0800 fax
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Variables Within Case

Edgar F. Johns
In reply to this post by Edgar F. Johns
Jon,

I have SPSS 15. I don't have the programmability plug-in installed. I
presume I can do so, though.

I'd like the new values to be the rank ordered position. Unsorted, it would
look like 1 2 3 4 5 6; sorted might be 6 3 4 2 1 5. I guess that would be an
index to the variable names.

Thanks,
Edgar
---
[snip]

Subject: Re: Sorting Variables Within Case

It would be easy to do this with programmability.  Do you have at least SPSS
14 with the programmability plug-in installed?

And, if so, what do you want the new variables to have for values - the
names of the input variables in rank order, an index to the variable names,
or names related to the originals but containing the rank within case?

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Edgar F. Johns
Sent: Wednesday, February 07, 2007 7:27 AM
To: [hidden email]
Subject: [SPSSX-L] Sorting Variables Within Case

I'm trying to sort a set of six variables within a case and compute a new
variable that records the sorted position (in descending order) of the six
variable.



I found the following code (in Raynald's SPSS Tools) below that will sort
the variables within a case. I tried it and it does sort but, it re-arranges
the values of the variables.



From:
(http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingVal
uesWithinCases_Bubble-Sort.txt)



* Sort X1 to X10 into ascending order .
vector x = x1 to x10 .
loop #i = 1 to 9 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 10 .
      do if x(#i) >= x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



The author alludes to being able to sort multiple variables within a case. I
tried a few ways to accomplish this with no luck. Can someone show me how to
sort on one variable and record the order in a separate variable as
described in the following:

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
new set of variables, First.Sixth, that tells me which Pred variable was
highest, second highest, etc.






Pred1

Pred2

Pred3

Pred4

Pred5

Pred6


S1

121.63

123.92

121.49

115.15

116.58

118.22


S2

96.335

109.041

108.981

89.012

104.545

116.293



















FIRST

SECOND

THIRD

FOURTH

FIFTH

SIXTH


S1

2

1

3

6

5

4


S2

6

2

3

5

1

4



Thanks.

Edgar

---

Discover Technologies

42020 Koppernick Rd.

Suite 204

Canton, MI 48187

(734) 564-4964

(734) 468-0800 fax
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Variables Within Case

Peck, Jon
Since this sort of manipulation has come up a number of times, I have posted an update to the spssaux2 module on SPSS Developer Central that provides a general function for this named rankvarsincase.  Here is a usage example based on the 1991 General Social Survey sav file, which is assumed to be the active dataset.

begin program.
import spss, spssaux2
spssaux2.rankvarsincase("hlth1 hlth2 hlth3 hlth4")
end program.

This produces four new variables with values between 0 and 3 giving the rank of each variable within the set in ascending order.  The new variable names are the same as the input names but suffixed with "_rank".  (A different suffix can be set by the second parameter to this function,  named suffix.)

The variable list, which must match the case of the variable names in SPSS, can be a simple string as above or a Python list or an spssaux dictionary.  New variable names must not already exist in the dataset.  Missing values are ranked low.

To use this function, you need
- at least SPSS 15 (15.0.1 preferred)
- the SPSS 15/15.0.1 Python programmability Plug-In and Python 2.4.
- modules spssaux, spssdata, and spssaux2 saved to a place where Python can find them (easiest is c:/python24/lib/site-packages in a standard installation).

The SPSS materials can be downloaded from SPSS Developer Central (www.spss.com/devcentral) and Python from www.python.org.  Be sure to get version 2.4, not 2.5.

-Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edgar F. Johns
Sent: Wednesday, February 07, 2007 8:27 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Sorting Variables Within Case

Jon,

I have SPSS 15. I don't have the programmability plug-in installed. I
presume I can do so, though.

I'd like the new values to be the rank ordered position. Unsorted, it would
look like 1 2 3 4 5 6; sorted might be 6 3 4 2 1 5. I guess that would be an
index to the variable names.

Thanks,
Edgar
---
[snip]

Subject: Re: Sorting Variables Within Case

It would be easy to do this with programmability.  Do you have at least SPSS
14 with the programmability plug-in installed?

And, if so, what do you want the new variables to have for values - the
names of the input variables in rank order, an index to the variable names,
or names related to the originals but containing the rank within case?

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Edgar F. Johns
Sent: Wednesday, February 07, 2007 7:27 AM
To: [hidden email]
Subject: [SPSSX-L] Sorting Variables Within Case

I'm trying to sort a set of six variables within a case and compute a new
variable that records the sorted position (in descending order) of the six
variable.



I found the following code (in Raynald's SPSS Tools) below that will sort
the variables within a case. I tried it and it does sort but, it re-arranges
the values of the variables.



From:
(http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingVal
uesWithinCases_Bubble-Sort.txt)



* Sort X1 to X10 into ascending order .
vector x = x1 to x10 .
loop #i = 1 to 9 .  /* top index is 1 less than number of variables
   loop #j = #i+1 to 10 .
      do if x(#i) >= x(#j) .
         compute #temp = x(#j) .
         compute x(#j) = x(#i) .
         compute x(#i) = #temp .
      end if .
   end loop .
end loop .
exe .

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



The author alludes to being able to sort multiple variables within a case. I
tried a few ways to accomplish this with no luck. Can someone show me how to
sort on one variable and record the order in a separate variable as
described in the following:

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X.  This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
new set of variables, First.Sixth, that tells me which Pred variable was
highest, second highest, etc.






Pred1

Pred2

Pred3

Pred4

Pred5

Pred6


S1

121.63

123.92

121.49

115.15

116.58

118.22


S2

96.335

109.041

108.981

89.012

104.545

116.293



















FIRST

SECOND

THIRD

FOURTH

FIFTH

SIXTH


S1

2

1

3

6

5

4


S2

6

2

3

5

1

4



Thanks.

Edgar

---

Discover Technologies

42020 Koppernick Rd.

Suite 204

Canton, MI 48187

(734) 564-4964

(734) 468-0800 fax
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Variables Within Case

Marta García-Granero
In reply to this post by Edgar F. Johns
Hi Edgar

Although my code doesn't do exactly what you want, it is close enough
(I hope), or you could use it as a starting point for your goal:

* Your sample data *.
DATA LIST LIST/ Pred1 TO Pred6 (6 F8.3).
BEGIN DATA
121.63 123.92  121.49  115.15 116.58  118.22
96.335 109.041 108.981 89.012 104.545 116.293
END DATA.

CACHE.
EXECUTE.
MATRIX.
GET data
 /VARIABLES=Pred1 TO Pred6
 /NAMES=vname
 /MISSING OMIT.
PRINT data
 /FORMAT='F8.3'
 /CNAMES=vname.
COMPUTE k=NCOL(data).
COMPUTE n=NROW(data).
COMPUTE pos=MAKE(n,k,0).
LOOP i=1 TO n.
- COMPUTE pos(i,:)=k+1-GRADE(data(i,:)).
END LOOP.
PRINT pos
 /CNAMES=vname.
SAVE pos /OUTFILE='c:\temp\Ranked.sav'.
END MATRIX.

* To add the case positions (descending) to the dataset *.
MATCH FILES /FILE=*
 /FILE='C:\Temp\Ranked.sav'.
EXECUTE.

EFJ> I'm trying to sort a set of six variables within a case and compute a new
EFJ> variable that records the sorted position (in descending order) of the six
EFJ> variable.


EFJ> As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
EFJ> new set of variables, First.Sixth, that tells me which Pred variable was
EFJ> highest, second highest, etc.


EFJ> Pred1

EFJ> Pred2

EFJ> Pred3

EFJ> Pred4

EFJ> Pred5

EFJ> Pred6


EFJ> S1

EFJ> 121.63

EFJ> 123.92

EFJ> 121.49

EFJ> 115.15

EFJ> 116.58

EFJ> 118.22


EFJ> S2

EFJ> 96.335

EFJ> 109.041

EFJ> 108.981

EFJ> 89.012

EFJ> 104.545

EFJ> 116.293


EFJ> FIRST

EFJ> SECOND

EFJ> THIRD

EFJ> FOURTH

EFJ> FIFTH

EFJ> SIXTH


EFJ> S1

EFJ> 2

EFJ> 1

EFJ> 3

EFJ> 6

EFJ> 5

EFJ> 4


EFJ> S2

EFJ> 6

EFJ> 2

EFJ> 3

EFJ> 5

EFJ> 1

EFJ> 4




--
Regards,
Dr. Marta García-Granero,PhD           mailto:[hidden email]
Statistician

---
"It is unwise to use a statistical procedure whose use one does
not understand. SPSS syntax guide cannot supply this knowledge, and it
is certainly no substitute for the basic understanding of statistics
and statistical thinking that is essential for the wise choice of
methods and the correct interpretation of their results".

(Adapted from WinPepi manual - I'm sure Joe Abrahmson will not mind)
Reply | Threaded
Open this post in threaded view
|

Re: Sorting Variables Within Case

King Douglas
In reply to this post by Peck, Jon
Great, Jon!

  You're the best.

  King Douglas
  American Airlines Customer Reseach

"Peck, Jon" <[hidden email]> wrote:
  Since this sort of manipulation has come up a number of times, I have posted an update to the spssaux2 module on SPSS Developer Central that provides a general function for this named rankvarsincase. Here is a usage example based on the 1991 General Social Survey sav file, which is assumed to be the active dataset.

begin program.
import spss, spssaux2
spssaux2.rankvarsincase("hlth1 hlth2 hlth3 hlth4")
end program.

This produces four new variables with values between 0 and 3 giving the rank of each variable within the set in ascending order. The new variable names are the same as the input names but suffixed with "_rank". (A different suffix can be set by the second parameter to this function, named suffix.)

The variable list, which must match the case of the variable names in SPSS, can be a simple string as above or a Python list or an spssaux dictionary. New variable names must not already exist in the dataset. Missing values are ranked low.

To use this function, you need
- at least SPSS 15 (15.0.1 preferred)
- the SPSS 15/15.0.1 Python programmability Plug-In and Python 2.4.
- modules spssaux, spssdata, and spssaux2 saved to a place where Python can find them (easiest is c:/python24/lib/site-packages in a standard installation).

The SPSS materials can be downloaded from SPSS Developer Central (www.spss.com/devcentral) and Python from www.python.org. Be sure to get version 2.4, not 2.5.

-Jon Peck


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Edgar F. Johns
Sent: Wednesday, February 07, 2007 8:27 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Sorting Variables Within Case

Jon,

I have SPSS 15. I don't have the programmability plug-in installed. I
presume I can do so, though.

I'd like the new values to be the rank ordered position. Unsorted, it would
look like 1 2 3 4 5 6; sorted might be 6 3 4 2 1 5. I guess that would be an
index to the variable names.

Thanks,
Edgar
---
[snip]

Subject: Re: Sorting Variables Within Case

It would be easy to do this with programmability. Do you have at least SPSS
14 with the programmability plug-in installed?

And, if so, what do you want the new variables to have for values - the
names of the input variables in rank order, an index to the variable names,
or names related to the originals but containing the rank within case?

-Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Edgar F. Johns
Sent: Wednesday, February 07, 2007 7:27 AM
To: [hidden email]
Subject: [SPSSX-L] Sorting Variables Within Case

I'm trying to sort a set of six variables within a case and compute a new
variable that records the sorted position (in descending order) of the six
variable.



I found the following code (in Raynald's SPSS Tools) below that will sort
the variables within a case. I tried it and it does sort but, it re-arranges
the values of the variables.



From:
(http://www.spsstools.net/Syntax/RankingLargestValSortingGrouping/SortingVal
uesWithinCases_Bubble-Sort.txt)



* Sort X1 to X10 into ascending order .
vector x = x1 to x10 .
loop #i = 1 to 9 . /* top index is 1 less than number of variables
loop #j = #i+1 to 10 .
do if x(#i) >= x(#j) .
compute #temp = x(#j) .
compute x(#j) = x(#i) .
compute x(#i) = #temp .
end if .
end loop .
end loop .
exe .

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X. This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



The author alludes to being able to sort multiple variables within a case. I
tried a few ways to accomplish this with no luck. Can someone show me how to
sort on one variable and record the order in a separate variable as
described in the following:

It is possible, of course, to sort multiple variables within a case this
way,
i.e., swap values of Y,Z,W, depending on X. This would allow keeping a
record
indicating that X1, the smallest value, originally was variable 6; X2,
the second smallest value, might originally have been variable 3, etc.



As an example, I have six scores (Pred1 to Pred6) and I'd like to create a
new set of variables, First.Sixth, that tells me which Pred variable was
highest, second highest, etc.






Pred1

Pred2

Pred3

Pred4

Pred5

Pred6


S1

121.63

123.92

121.49

115.15

116.58

118.22


S2

96.335

109.041

108.981

89.012

104.545

116.293



















FIRST

SECOND

THIRD

FOURTH

FIFTH

SIXTH


S1

2

1

3

6

5

4


S2

6

2

3

5

1

4



Thanks.

Edgar

---

Discover Technologies

42020 Koppernick Rd.

Suite 204

Canton, MI 48187

(734) 564-4964

(734) 468-0800 fax