Paired compute looping help

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

Paired compute looping help

Bubba K
I need some help simplifying some syntax.
I have a set of 18 variables (v1 to v18) that I need to get the pared sum of
all possible combinations between those 18 variables... so for example,
v1+v2,  v1+v3, v1+v4, ... v17+v18. but that would require 153 different
compute statements.    I figure that there is a way through looping or
something like that to make this task a bit simpler, but unfortunately, I do
not have much experience with looping.  Can someone please help?

So for example, lets say that I have a dataset with 4 variables as below
(Except I actually have 18).

DATA LIST LIST /v1 v2 v3 v4.
BEGIN DATA
1  0  1  0
0  1  1  1
0  0  1  0
1  1  0  1
END DATA.

And I need something that looks like this.


v1 v2 v3 v4 v1_2 v1_3 v1_4 v2_3 v2_4 v3_4
1  0  1  0  1     2    1   1    0     1
0  1  1  1  1     1    1   2    2     2
0  0  1  0  0     1    0   1    0     1
1  1  0  1  2     1    2   2    2     1


Thanks

BK
Reply | Threaded
Open this post in threaded view
|

Re: Paired compute looping help

Maguin, Eugene
Bubba,

Here one method for doing most of what you want to do. Let v1 to v18 be your
variables.

Vector sum(153).
Vector v=v1 to v18.
Loop #i=1 to 17.
Loop #j=#i+1 to 18.
+  compute #k=18*(#i-1)+#j.
+  compute sum(#k)=v(#i)+v(#j).
End loop.
End loop.

The thing I can't do is figure out how to your resulting variables as you'd
like them named, i.e., v17_v18. It may be possible to do this with a rename
command embedded in a macro but I am not sure and, even if a macro is the
right way to go, can't help you with that. Perhaps somebody else can or
knows a better method. Perhaps it can be done with Python.

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

Re: Paired compute looping help

Bubba K
Many thanks Gene...

On 5/15/07, Gene Maguin <[hidden email]> wrote:

>
> Bubba,
>
> Here one method for doing most of what you want to do. Let v1 to v18 be
> your
> variables.
>
> Vector sum(153).
> Vector v=v1 to v18.
> Loop #i=1 to 17.
> Loop #j=#i+1 to 18.
> +  compute #k=18*(#i-1)+#j.
> +  compute sum(#k)=v(#i)+v(#j).
> End loop.
> End loop.
>
> The thing I can't do is figure out how to your resulting variables as
> you'd
> like them named, i.e., v17_v18. It may be possible to do this with a
> rename
> command embedded in a macro but I am not sure and, even if a macro is the
> right way to go, can't help you with that. Perhaps somebody else can or
> knows a better method. Perhaps it can be done with Python.
>
> Gene Maguin
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Paired compute looping help

Bob Walker-2
In reply to this post by Bubba K
Hi Bubba,

Here's one approach that creates the variable names that Gene Maguin had alluded to in his post...

SET MEXPAND ON / MPRINT ON / PRI ON.
DATA LIST LIST /V1 TO V5.
BEGIN DATA
0 1 0 1 1
1 1 1 0 1
0 1 0 1 0
1 0 1 1 1
END DATA.

DEFINE !BUBBA()
!LET !MIN=1 !LET !MAX=5
VECTOR V=!CONCAT("V",!MIN) TO !CONCAT("V",!MAX).

!DO !A=!MIN !TO !MAX !DO !B=!A !TO !MAX
!IF (!A<>!B) !THEN
COMPUTE !CONCAT("V_",!A,!B)=V(!A)+V(!B).
!IFEND !DOEND !DOEND
EXECUTE.
DATASET NAME BUBBA WINDOW=FRONT.

!ENDDEFINE.
!BUBBA.

Note that the variable names are simply the combination of the variable numbers, so V_23 is the sum of V2 and V3. HTH,

Bob Walker
Surveys & Forecasts, LLC
www.safllc.com

----- Original Message -----
From: Bubba K
Date: Tuesday, May 15, 2007 1:20 pm
Subject: Paired compute looping help
To: [hidden email]

> I need some help simplifying some syntax.
> I have a set of 18 variables (v1 to v18) that I need to get the
> pared sum of
> all possible combinations between those 18 variables... so for
> example,v1+v2, v1+v3, v1+v4, ... v17+v18. but that would
> require 153 different
> compute statements. I figure that there is a way through
> looping or
> something like that to make this task a bit simpler, but
> unfortunately, I do
> not have much experience with looping. Can someone please help?
>
> So for example, lets say that I have a dataset with 4 variables
> as below
> (Except I actually have 18).
>
> DATA LIST LIST /v1 v2 v3 v4.
> BEGIN DATA
> 1 0 1 0
> 0 1 1 1
> 0 0 1 0
> 1 1 0 1
> END DATA.
>
> And I need something that looks like this.
>
>
> v1 v2 v3 v4 v1_2 v1_3 v1_4 v2_3 v2_4 v3_4
> 1 0 1 0 1 2 1 1 0 1
> 0 1 1 1 1 1 1 2 2 2
> 0 0 1 0 0 1 0 1 0 1
> 1 1 0 1 2 1 2 2 2 1
>
>
> Thanks
>
> BK
>

 Bob Walker
Surveys & Forecasts, LLC
www.safllc.com
Reply | Threaded
Open this post in threaded view
|

Re: Paired compute looping help

Richard Ristow
At 10:47 PM 5/15/2007, Bob Walker wrote:

[test data omitted; see cited posting]

>Here's one approach:
>
>DEFINE !BUBBA()
>!LET !MIN=1 !LET !MAX=5
>VECTOR V=!CONCAT("V",!MIN) TO !CONCAT("V",!MAX).
>
>!DO !A=!MIN !TO !MAX !DO !B=!A !TO !MAX
>!IF (!A<>!B) !THEN
>COMPUTE !CONCAT("V_",!A,!B)=V(!A)+V(!B).
>!IFEND !DOEND !DOEND
>EXECUTE.
>DATASET NAME BUBBA WINDOW=FRONT.
>
>!ENDDEFINE.

Now, that is a SWEET little piece of macro writing.

Curious: since you're unrolling the COMPUTEs in a macro loop anyway,
why not generate the arguments to the COMPUTEs by catenation, rather
than vector indexing? Like this (WRR-not saved separately):

[test data]

DEFINE !BUBBA()
!LET !MIN=1 !LET !MAX=5

!DO !A=!MIN !TO !MAX !DO !B=!A !TO !MAX
!IF (!A<>!B) !THEN
COMPUTE !CONCAT("V_",!A,!B)=!CONCAT("V",!A) + !CONCAT("V",!B).
!IFEND !DOEND !DOEND
!ENDDEFINE.

PRESERVE.
SET MPRINT ON.
!BUBBA.
  144 M>
  145 M>  .
  146 M>  COMPUTE V_12 = V1 + V2.
  147 M>  COMPUTE V_13 = V1 + V3.
  148 M>  COMPUTE V_14 = V1 + V4.
  149 M>  COMPUTE V_15 = V1 + V5.
  150 M>  COMPUTE V_23 = V2 + V3.
  151 M>  COMPUTE V_24 = V2 + V4.
  152 M>  COMPUTE V_25 = V2 + V5.
  153 M>  COMPUTE V_34 = V3 + V4.
  154 M>  COMPUTE V_35 = V3 + V5.
  155 M>  COMPUTE V_45 = V4 + V5.
  156 M>  .
RESTORE.
  157 M>  RESTORE.

DATASET NAME BUBBA WINDOW=FRONT.

LIST.
[data listing]
Reply | Threaded
Open this post in threaded view
|

Re: Paired compute looping help

Bob Walker-2
Richard,

Nice upgrade, as usual. Using vector was "mental scrap" left after attempting to build upon Gene Maguin's loop indexing, but you're absolutely right -- it's superfluous if we use !CONCAT throughout. Thanks!

Bob Walker
Surveys & Forecasts, LLC
www.safllc.com


----- Original Message -----
From: Richard Ristow
Date: Tuesday, May 15, 2007 11:39 pm
Subject: Re: Paired compute looping help
To: [hidden email], [hidden email]

> At 10:47 PM 5/15/2007, Bob Walker wrote:
>
> [test data omitted; see cited posting]
>
> >Here's one approach:
> >
> >DEFINE !BUBBA()
> >!LET !MIN=1 !LET !MAX=5
> >VECTOR V=!CONCAT("V",!MIN) TO !CONCAT("V",!MAX).
> >
> >!DO !A=!MIN !TO !MAX !DO !B=!A !TO !MAX
> >!IF (!A<>!B) !THEN
> >COMPUTE !CONCAT("V_",!A,!B)=V(!A)+V(!B).
> >!IFEND !DOEND !DOEND
> >EXECUTE.
> >DATASET NAME BUBBA WINDOW=FRONT.
> >
> >!ENDDEFINE.
>
> Now, that is a SWEET little piece of macro writing.
>
> Curious: since you're unrolling the COMPUTEs in a macro loop
> anyway,
> why not generate the arguments to the COMPUTEs by catenation,
> rather
> than vector indexing? Like this (WRR-not saved separately):
>
> [test data]
>
> DEFINE !BUBBA()
> !LET !MIN=1 !LET !MAX=5
>
> !DO !A=!MIN !TO !MAX !DO !B=!A !TO !MAX
> !IF (!A<>!B) !THEN
> COMPUTE !CONCAT("V_",!A,!B)=!CONCAT("V",!A) + !CONCAT("V",!B).
> !IFEND !DOEND !DOEND
> !ENDDEFINE.
>
> PRESERVE.
> SET MPRINT ON.
> !BUBBA.
> 144 M>
> 145 M> .
> 146 M> COMPUTE V_12 = V1 + V2.
> 147 M> COMPUTE V_13 = V1 + V3.
> 148 M> COMPUTE V_14 = V1 + V4.
> 149 M> COMPUTE V_15 = V1 + V5.
> 150 M> COMPUTE V_23 = V2 + V3.
> 151 M> COMPUTE V_24 = V2 + V4.
> 152 M> COMPUTE V_25 = V2 + V5.
> 153 M> COMPUTE V_34 = V3 + V4.
> 154 M> COMPUTE V_35 = V3 + V5.
> 155 M> COMPUTE V_45 = V4 + V5.
> 156 M> .
> RESTORE.
> 157 M> RESTORE.
>
> DATASET NAME BUBBA WINDOW=FRONT.
>
> LIST.
> [data listing]
>
>

 Bob Walker
Surveys & Forecasts, LLC
www.safllc.com