Finding the Lowest Value

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

Finding the Lowest Value

John Norton
Hi List,
 
I have a battery of 4 repeated measures taken over a period of 9 weeks (so there are 36 variables).  I need to locate the week in which the lowest or minimum measure occurs.  I know I can write 36 separate "IF" statements, but that hardly seems efficient.
 
The idea is thus:  
 
IF (MIN(wbc1 TO wbc9) = wbc1) lowest_week = 1.
IF (MIN(wbc1 TO wbc9) = wbc2) lowest_week = 2.
IF (MIN(wbc1 TO wbc9) = wbc3) lowest_week = 3.
etc.
 
But as you can see, that's just inefficient.  (Granted, I could probably just copy and paste the IF statements and edit them all, and it would also take less time than it would to complete this request for assistance..  But that's just so..  inelegant.
 
Any suggestions are appreciated.
 
Thanks,
 
John
Reply | Threaded
Open this post in threaded view
|

Re: Finding the Lowest Value

emaguin
John,

If i understand you correctly, the way to do this is to use Loop. In the
code that follows, i assume that your variables (MA1 to MA9, MB1 to MB9,
MC1 to MC9, and MD1 to MD9, the second character is the measure descriptor)
are organized in the following manner in your dataset: MA1 to MA9, MB1 to
MB9, MC1 to MC9, and MD1 to MD9. If they are not organized that way, you'll
have to get them that way for this to work.

Vector Measure=MA1 to MA9.
Compute MAWeek=0.
Compute #first=MA1.
Loop #i=2 to 9.
+  Do if (measure(#i) lt #first).
+     Compute MAWeek=#i.
+     Compute #first=measure(#i).
+  End if.
End loop.

You'll need to repeat this for each of the variable sets. There may be a
way to do this all at once if a Loop-end loop can be embedded in a Do
repeat. Maybe somebody with more experience can comment.

Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: Finding the Lowest Value

Richard Ristow
At 02:49 PM 7/21/2006, Gene Maguin wrote a very nice solution:

>The way to do this is to use Loop. [discussion omitted]
>
>Vector Measure=MA1 to MA9.
>Compute MAWeek=0.
>Compute #first=MA1.
>Loop #i=2 to 9.
>+  Do if (measure(#i) lt #first).
>+     Compute MAWeek=#i.
>+     Compute #first=measure(#i).
>+  End if.
>End loop.
>
>You'll need to repeat this for each of variable set. There may be a
>way to do this all at once if a Loop-end loop can be embedded in a Do
>repeat. Maybe somebody with more experience can comment.

It surprised me most pleasantly, but yes, it can. Here's from a
long-ago posting(*):

I hardly dared hope that THIS would work -- but a variable list on a DO
REPEAT, represented by a stand-in name, *can* be a list of vectors.
Tested code; SPSS draft output:

NEW FILE.
INPUT PROGRAM.
NUMERIC CASE_ID (F4).
.  COMPUTE CASE_ID = 1.
.  END CASE.
END FILE.
END INPUT PROGRAM.

VECTOR A,B,C (3,F2).
DO REPEAT VCTR =  A  B  C
          /PLUS = 20 30 40.
-  LOOP #IDX = 1 TO 3.
.     COMPUTE VCTR(#IDX) = PLUS + #IDX.
-  END LOOP.
END REPEAT.

LIST.
List
CASE_ID A1 A2 A3 B1 B2 B3 C1 C2 C3

      1  21 22 23 31 32 33 41 42 43

Number of cases read:  1    Number of cases listed:  1


------------------
Citation:
Date:         Sat, 11 Dec 2004 19:56:52 -0500
From:         Richard Ristow <[hidden email]>
Subject:      DO REPEAT with vectors
To:           [hidden email]
X-ELNK-AV: 0
Reply | Threaded
Open this post in threaded view
|

Re: Finding the Lowest Value

John Norton
In reply to this post by John Norton
Hi List,
 
Last week I requested help with locating the lowest value across several variables.  The heart of my solution below rests with comparing whether the value of measure(#i) = MIN(wbc1 TO wbc9).  I use a loop such that when that argument is true, I record the iteration (the week) to the target variable.  Placed within a macro, I can call the macro as many times as I have measurements.
 
DEFINE low_week (first = !CHAREND(';')/last = !CHAREND(':') /target = !CMDEND).
VECTOR measure = !first TO !last.
LOOP #i = 1 TO 9.
DO IF (measure(#i) = MIN(!first TO !last)).
!CONCAT('COMPUTE ',!target,' = #i.').
END IF.
END LOOP.
EXE.
!ENDDEFINE.
 
low_week first = WBC1; last = WBC9: target = wbc_lowest_week.
low_week first = gran1; last = gran9: target = gran_lowest_week.

My thanks to Gene Maguin for his suggested solutions from last week.
 
Best,
 
 
 
John Norton
Biostatistician
Oncology Institute
Loyola University Medical Center
 
(708) 327-3095
[hidden email]
 
"Absence of evidence
      is not evidence of absence"