Top 3 highest numbers in a range of variables.

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

Top 3 highest numbers in a range of variables.

Mark Webb-5
Hi all
Max gives the highest number in a range of variables.
Compute MaxNumber=max(var1 to var10).
I'm looking for a neat way to determine the the 2nd & 3rd highest numbers as well.
Any suggestions?
--
Mark Webb

Line +27 (21) 786 4379
Cell +27 (72) 199 1000
Fax to email +27 (86) 5513075
Skype  webbmark
Email  [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: Top 3 highest numbers in a range of variables.

John F Hall
Mark
 
freq var1 to var10 .
 
At least it doesn't waste paper these days, but if you have hundreds of values you might get RSI scrolling down to find the last three values in each of ten frequency counts!  On second thoughts:
 
freq var1 to var10 /for dfreq .
 
. . . will save you scrolling.  When you've found the values you want, you can always use something like
 
count topthree = var1 to var10 (top1, top2, top3) .
 
I'm sure someone else will supply something more effective, but also much more complex.
 
----- Original Message -----
Sent: Thursday, August 05, 2010 9:41 AM
Subject: Top 3 highest numbers in a range of variables.

Hi all
Max gives the highest number in a range of variables.
Compute MaxNumber=max(var1 to var10).
I'm looking for a neat way to determine the the 2nd & 3rd highest numbers as well.
Any suggestions?
--
Mark Webb

Line +27 (21) 786 4379
Cell +27 (72) 199 1000
Fax to email +27 (86) 5513075
Skype  webbmark
Email  [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
|

Problem with loops in macros

Joost van Ginkel
Dear list,
 
I'm trying to do run the following macro:
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!DO !J = 1 !TO I!*2 .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
When running this, Loop !J ignores the multiplication by 2. Probably SPSS sees everything behind the asterisk as comments so I tried this:
 
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!LET !END = !I * 2 .
!DO !J = 1 !TO !END .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
But this didn't work either because now I got an error message. Apparently SPSS sees !END as a string and you can't loop over a string. Does anyone know how to solve this problem? Thank you very much in advance!
 
Best regards,
 
Joost van Ginkel

**********************************************************************

This email and any files transmitted with it are confidential and

intended solely for the use of the individual or entity to whom they

are addressed. If you have received this email in error please notify

the system manager.

**********************************************************************

 

Reply | Threaded
Open this post in threaded view
|

Re: Top 3 highest numbers in a range of variables.

Mike
In reply to this post by John F Hall
A simpler procedure than John's which generate less output is:
 
desc var=var1 to var10/stat=MAX/sort=MAX(D).
 
The descriptives procedure about will provide one line of output
for each variable and reorder the sequence of variables to have
the variable with the greatest maximum value first, next greatest
maximum value third, and so on.
 
But the original request was really for something like
 
Compute Max1=max(var1 to var10).  /* Greatest Maximum value.
compute Max2=max2(var1 to var10.  /* 2nd Greatest Max value.
compute Max3=max3(var1 to var10). /*3rd Greatest Max value
I think this requires one to be able to sort or rank the values of
var1 to var10 for each subject which can probably be done by
vectors and loops.  Perhaps some interested SPSS syntax writer
can work out the code (I'm a little busy right now).
 
-Mike Palij
New York University
 
----- Original Message -----
Sent: Thursday, August 05, 2010 7:59 AM
Subject: Re: Top 3 highest numbers in a range of variables.

Mark
 
freq var1 to var10 .
 
At least it doesn't waste paper these days, but if you have hundreds of values you might get RSI scrolling down to find the last three values in each of ten frequency counts!  On second thoughts:
 
freq var1 to var10 /for dfreq .
 
. . . will save you scrolling.  When you've found the values you want, you can always use something like
 
count topthree = var1 to var10 (top1, top2, top3) .
 
I'm sure someone else will supply something more effective, but also much more complex.
 
----- Original Message -----
Sent: Thursday, August 05, 2010 9:41 AM
Subject: Top 3 highest numbers in a range of variables.

Hi all
Max gives the highest number in a range of variables.
Compute MaxNumber=max(var1 to var10).
I'm looking for a neat way to determine the the 2nd & 3rd highest numbers as well.
Any suggestions?
--
Mark Webb

Line +27 (21) 786 4379
Cell +27 (72) 199 1000
Fax to email +27 (86) 5513075
Skype  webbmark
Email  [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: Top 3 highest numbers in a range of variables.

Jon K Peck
In reply to this post by Mark Webb-5

Using the SPSSINC TRANS extension command available from SPSS Developer Central (www.spss.com/devcentral) (just 5 variables in this example).  SPSSINC TRANS applies the function f to all the cases, creating three new variables, v1, v2, v3.

data list free/x1 to x5.
begin data
1 3 5 7 9
9 8 7 6 5
1 4 10 -2 2
end data.

begin program.
import spss
def f(x):
  return(sorted(x,reverse=True)[:3])
end program.

spssinc trans result=v1 to v3
/formula "f([x1,x2,x3,x4,x5])".

HTH,
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Mark Webb <[hidden email]>
To: [hidden email]
Date: 08/05/2010 04:22 AM
Subject: [SPSSX-L] Top 3 highest numbers in a range of variables.
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi all
Max gives the highest number in a range of variables.

Compute MaxNumber=max(var1 to var10).

I'm looking for a neat way to determine the the 2nd & 3rd highest numbers as well.
Any suggestions?

--
Mark Webb

Line +27 (21) 786 4379
Cell +27 (72) 199 1000
Fax to email +27 (86) 5513075
Skype  webbmark
Email  
targetlinkmark@...
===================== 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: Problem with loops in macros

Kooij, A.J. van der
In reply to this post by Joost van Ginkel
Hi Joost,
 
This is what I use:
 

/* Create empty data set with 1 case 1 var; needed in LOOPi */.

INPUT PROGRAM.

VECTOR V(1).

LOOP #I = 1 TO 1.

LOOP #J = 1 TO 1.

END LOOP.

END CASE.

END LOOP.

END FILE.

END INPUT PROGRAM.

execute.

 

/* Define LOOPi Macro */.

DEFINE LOOPi ().

!DO !I = 1 !TO 2 .

compute tmpval = !i * 2.

write out='tmp.sav' 

 /"define !loopjval() " tmpval " !enddefine.".

exe.

include file='tmp.sav' .

/* Call LOOPj Macro */.

Loopj

 loopjval = !loopjval.

!DOEND .

!ENDDEFINE .

 

/* Define LOOPj Macro */.

DEFINE LOOPj (loopjval = !TOKENS(1) ).

!DO !J = 1 !TO !loopjval .

!DOEND .

!ENDDEFINE .

 

/* Call LOOPi Macro */.

LOOPi.

 



From: SPSSX(r) Discussion on behalf of Ginkel, Joost van
Sent: Thu 05-Aug-10 14:34
To: [hidden email]
Subject: Problem with loops in macros

Dear list,
 
I'm trying to do run the following macro:
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!DO !J = 1 !TO I!*2 .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
When running this, Loop !J ignores the multiplication by 2. Probably SPSS sees everything behind the asterisk as comments so I tried this:
 
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!LET !END = !I * 2 .
!DO !J = 1 !TO !END .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
But this didn't work either because now I got an error message. Apparently SPSS sees !END as a string and you can't loop over a string. Does anyone know how to solve this problem? Thank you very much in advance!
 
Best regards,
 
Joost van Ginkel

**********************************************************************

This email and any files transmitted with it are confidential and

intended solely for the use of the individual or entity to whom they

are addressed. If you have received this email in error please notify

the system manager.

**********************************************************************

 

Reply | Threaded
Open this post in threaded view
|

Re: Top 3 highest numbers in a range of variables.

Bruce Weaver
Administrator
In reply to this post by Mark Webb-5
Mark Webb-5 wrote
 
    Hi all
    Max gives the highest number in a range of variables.
    Compute MaxNumber=max(var1 to var10).
    I'm looking for a neat way to determine the the 2nd & 3rd
    highest numbers as well.
    Any suggestions?
    --
Mark Webb

Line +27 (21) 786 4379
Cell +27 (72) 199 1000
Fax to email +27 (86) 5513075
Skype  webbmark
Email  targetlinkmark@gmail.com
For some BP solutions, see items 11 and 14 here:

   http://www.spsstools.net/SampleSyntax.htm#RankingLargestValuesSortingGrouping

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/).
Reply | Threaded
Open this post in threaded view
|

Re: Problem with loops in macros

Albert-Jan Roskam
In reply to this post by Kooij, A.J. van der
Hi Joost,

From http://www.spsstools.net/MacroTutorial.htm:

It is not possible to (directly) do arithmetic with macro variables. For instance, given the following 2 macro statements: !LET !a=1.
!LET !b=2.

the following is not a valid command:
!LET !c=!a + !b.

The following is an indirect method of obtaining the sum of !a and !b.
!LET !c=!LENGTH(!CONCAT(!BLANK(!a),!BLANK(!b)).
This is not a very friendly method... Try to do a division (or calculate the log of !b) with a similar method!


But I find these indirect methods downright ugly. You get more powerful, readable code if you use python.



Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 8/5/10, Kooij, A.J. van der <[hidden email]> wrote:

From: Kooij, A.J. van der <[hidden email]>
Subject: Re: [SPSSX-L] Problem with loops in macros
To: [hidden email]
Date: Thursday, August 5, 2010, 4:26 PM

Hi Joost,
 
This is what I use:
 

/* Create empty data set with 1 case 1 var; needed in LOOPi */.

INPUT PROGRAM.

VECTOR V(1).

LOOP #I = 1 TO 1.

LOOP #J = 1 TO 1.

END LOOP .

END CASE.

END LOOP .

END FILE.

END INPUT PROGRAM.

execute.

 

/* Define LOOPi Macro */.

DEFINE LOOPi ().

!DO !I = 1 !TO 2 .

compute tmpval = !i * 2.

write out='tmp.sav' 

 /"define !loopjval() " tmpval " !enddefine.".

exe.

include file='tmp.sav' .

/* Call LOOPj Macro */.

Loopj

 loopjval = !loopjval.

!DOEND .

!ENDDEFINE .

 

/* Define LOOPj Macro */.

DEFINE LOOPj (loopjval = !TOKENS(1) ).

!DO !J = 1 !TO !loopjval .

!DOEND .

!ENDDEFINE .

 

/* Call LOOPi Macro */.

LOOPi.

 



From: SPSSX(r) Discussion on behalf of Ginkel, Joost van
Sent: Thu 05-Aug-10 14:34
To: [hidden email]
Subject: Problem with loops in macros

Dear list,
 
I'm trying to do run the following macro:
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!DO !J = 1 !TO I!*2 .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
When running this, Loop !J ignores the multiplication by 2. Probably SPSS sees everything behind the asterisk as comments so I tried this:
 
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!LET !END = !I * 2 .
!DO !J = 1 !TO !END .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
But this didn't work either because now I got an error message. Apparently SPSS sees !END as a string and you can't loop over a string. Does anyone know how to solve this problem? Thank you very much in advance!
 
Best regards,
 
Joost van Ginkel

**********************************************************************

This email and any files transmitted with it are confidential and

intended solely for the use of the individual or entity to whom they

are addressed. If you have received this email in error please notify

the system manager.

**********************************************************************

 


Reply | Threaded
Open this post in threaded view
|

Re: Problem with loops in macros

Joost van Ginkel
Dear Albert-Jan,
 
Thank you! I will give that a try.
 
Best regards,
 
Joost


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Albert-Jan Roskam
Sent: vrijdag 6 augustus 2010 10:14
To: [hidden email]
Subject: Re: Problem with loops in macros

Hi Joost,

From http://www.spsstools.net/MacroTutorial.htm:

It is not possible to (directly) do arithmetic with macro variables. For instance, given the following 2 macro statements: !LET !a=1.
!LET !b=2.

the following is not a valid command:
!LET !c=!a + !b.

The following is an indirect method of obtaining the sum of !a and !b.
!LET !c=!LENGTH(!CONCAT(!BLANK(!a),!BLANK(!b)).
This is not a very friendly method... Try to do a division (or calculate the log of !b) with a similar method!


But I find these indirect methods downright ugly. You get more powerful, readable code if you use python.



Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 8/5/10, Kooij, A.J. van der <[hidden email]> wrote:

From: Kooij, A.J. van der <[hidden email]>
Subject: Re: [SPSSX-L] Problem with loops in macros
To: [hidden email]
Date: Thursday, August 5, 2010, 4:26 PM

Hi Joost,
 
This is what I use:
 

/* Create empty data set with 1 case 1 var; needed in LOOPi */.

INPUT PROGRAM.

VECTOR V(1).

LOOP #I = 1 TO 1.

LOOP #J = 1 TO 1.

END LOOP .

END CASE.

END LOOP .

END FILE.

END INPUT PROGRAM.

execute.

 

/* Define LOOPi Macro */.

DEFINE LOOPi ().

!DO !I = 1 !TO 2 .

compute tmpval = !i * 2.

write out='tmp.sav' 

 /"define !loopjval() " tmpval " !enddefine.".

exe.

include file='tmp.sav' .

/* Call LOOPj Macro */.

Loopj

 loopjval = !loopjval.

!DOEND .

!ENDDEFINE .

 

/* Define LOOPj Macro */.

DEFINE LOOPj (loopjval = !TOKENS(1) ).

!DO !J = 1 !TO !loopjval .

!DOEND .

!ENDDEFINE .

 

/* Call LOOPi Macro */.

LOOPi.

 



From: SPSSX(r) Discussion on behalf of Ginkel, Joost van
Sent: Thu 05-Aug-10 14:34
To: [hidden email]
Subject: Problem with loops in macros

Dear list,
 
I'm trying to do run the following macro:
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!DO !J = 1 !TO I!*2 .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
When running this, Loop !J ignores the multiplication by 2. Probably SPSS sees everything behind the asterisk as comments so I tried this:
 
 
DEFINE !LOOP ( ) .
!DO !I = 1 !TO 2 .
!LET !END = !I * 2 .
!DO !J = 1 !TO !END .
{CODE}
!DOEND .
!DOEND .
!ENDDEFINE .
!LOOP .
 
But this didn't work either because now I got an error message. Apparently SPSS sees !END as a string and you can't loop over a string. Does anyone know how to solve this problem? Thank you very much in advance!
 
Best regards,
 
Joost van Ginkel

**********************************************************************

This email and any files transmitted with it are confidential and

intended solely for the use of the individual or entity to whom they

are addressed. If you have received this email in error please notify

the system manager.

**********************************************************************