LOOP - END LOOP

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

LOOP - END LOOP

Shane S Allua

Hello,
I have the following data file:



varA varB varC varD
1 1 0 1000
1 2 0 0
1 3 0 0
1 4 1 100
1 5 0 0
2 1 0 0
2 2 0 1500
2 3 2 200

For each value of varB within varA, I need to add the values of varD only if varB=varC for any varB. So for varA = 1, I need to sum the values of varD where varB = 1 and varB=4 such that a new varE= 1000+100.

For varA=2, I need to sum the values of varD for varB =2 and varB =3 such that varE= 1500+200. Essentially, for each varB within varA, I need SPSS to loop through each value of varC and when varB=varC for any varB , sum the values of varD.

I have tried several iterations of LOOP - END LOOP but have only succeeded when for any ONE case, varB = varC.

If any one has any recommendations, your assistance would be greatly appreciated.



Thanks!

Shane

Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Art Kendall
please post the data file that you want to end up with.

Art


On 10/20/2010 8:36 AM, Shane S Allua wrote:

>
> Hello,
> I have the following data file:
>
>
>
> varA varB varC varD
> 1 1 0 1000
> 1 2 0 0
> 1 3 0 0
> 1 4 1 100
> 1 5 0 0
> 2 1 0 0
> 2 2 0 1500
> 2 3 2 200
>
> For each value of varB within varA, I need to add the values of varD
> only if varB=varC for any varB. So for varA = 1, I need to sum the
> values of varD where varB = 1 and varB=4 such that a new varE= 1000+100.
>
> For varA=2, I need to sum the values of varD for varB =2 and varB =3
> such that varE= 1500+200. Essentially, for each varB within varA, I
> need SPSS to loop through each value of varC and when varB=varC for
> any varB , sum the values of varD.
>
> I have tried several iterations of LOOP - END LOOP but have only
> succeeded when for any ONE case, varB = varC.
>
> If any one has any recommendations, your assistance would be greatly
> appreciated.
>
>
>
> Thanks!
>
> Shane
>

=====================
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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Robert Walker
In reply to this post by Shane S Allua

If you want the sums appended to each qualifying record...

 

DATA LIST FREE /varA varB varC varD.

BEGIN DATA

1 1 0 1000

1 2 0 0

1 3 0 0

1 4 1 100

1 5 0 0

2 1 0 0

2 2 0 1500

2 3 2 200

END DATA.

 

IF (varB=1 OR varB=4) do_math=1.

IF (varB=2 OR varB=3) do_math=2.

AGGREGATE /BREAK varA do_math/varE = SUM(varD).

 

Bob Walker

Surveys & Forecasts, LLC

www.safllc.com

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Shane S Allua
Sent: Wednesday, October 20, 2010 8:37 AM
To: [hidden email]
Subject: LOOP - END LOOP

 

Hello,
I have the following data file:



varA varB varC varD
1 1 0 1000
1 2 0 0
1 3 0 0
1 4 1 100
1 5 0 0
2 1 0 0
2 2 0 1500
2 3 2 200

For each value of varB within varA, I need to add the values of varD only if varB=varC for any varB. So for varA = 1, I need to sum the values of varD where varB = 1 and varB=4 such that a new varE= 1000+100.

For varA=2, I need to sum the values of varD for varB =2 and varB =3 such that varE= 1500+200. Essentially, for each varB within varA, I need SPSS to loop through each value of varC and when varB=varC for any varB , sum the values of varD.

I have tried several iterations of LOOP - END LOOP but have only succeeded when for any ONE case, varB = varC.

If any one has any recommendations, your assistance would be greatly appreciated.



Thanks!

Shane

Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Art Kendall
In reply to this post by Art Kendall
Are there a fixed number of values that can occur in varb and var and
varc?  e.g., either can run form 1 to 5 or either can run from 1 to 9, etc.

Art

=====================
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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Robert Walker
In reply to this post by Shane S Allua

Shane,

 

Re-thinking your issue, try this:

 

DATA LIST FREE /varA varB varC varD.

BEGIN DATA

1 1 0 1000

1 2 0 0

1 3 0 0

1 4 1 100

1 5 0 0

2 1 0 0

2 2 0 1500

2 3 2 200

END DATA.

SORT CASES BY varA varB varC.

DATASET NAME DATA1 WINDOW=FRONT.

AGGREGATE OUTFILE=* /BREAK varA varC /varC_hit = SUM(varD).

RENAME VARIABLES (varC = varB).

DATASET NAME DATA2 WINDOW=FRONT.

MATCH FILES FILE=DATA1 /TABLE=DATA2 /BY varA varB.

COMPUTE varE = varD+varC_hit.

EXECUTE.

DATASET CLOSE ALL.

 

Making some assumptions:

 

-       Within varA, varB always increments by 1 on each subsequent record

-       varC is always < varB

-       varE is appended to only the first record of the matching pair

-       You are not interested in totaling records that don’t have a match.

 

FYI, this captures possible multiple occurrences of varC.

 

HTH,

 

Bob

 

Surveys & Forecasts, LLC

2323 North Street

Fairfield, CT 06824

O 203.255.0505

M 203.685.8860

F 203.930.2230

www.safllc.com

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Shane S Allua
Sent: Wednesday, October 20, 2010 8:37 AM
To: [hidden email]
Subject: LOOP - END LOOP

 

Hello,
I have the following data file:



varA varB varC varD
1 1 0 1000
1 2 0 0
1 3 0 0
1 4 1 100
1 5 0 0
2 1 0 0
2 2 0 1500
2 3 2 200

For each value of varB within varA, I need to add the values of varD only if varB=varC for any varB. So for varA = 1, I need to sum the values of varD where varB = 1 and varB=4 such that a new varE= 1000+100.

For varA=2, I need to sum the values of varD for varB =2 and varB =3 such that varE= 1500+200. Essentially, for each varB within varA, I need SPSS to loop through each value of varC and when varB=varC for any varB , sum the values of varD.

I have tried several iterations of LOOP - END LOOP but have only succeeded when for any ONE case, varB = varC.

If any one has any recommendations, your assistance would be greatly appreciated.



Thanks!

Shane

Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Richard Ristow
In reply to this post by Shane S Allua
At 08:36 AM 10/20/2010, Shane S Allua wrote:

>For each value of varB within varA [in my file], I need to add the
>values of varD only if varB=varC for any varB.
>
>I have tried several iterations of LOOP - END LOOP but have only
>succeeded when for any ONE case, varB = varC.

Others on the list are working on understanding your needs.

One point about solutions: LOOP/END LOOP works within a
transformation program. It loops within each case, not across cases.
SPSS has excellent tools for computing across cases, including LAG
and AGGREGATE; other respondents are exploring those with you.

=====================
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: LOOP - END LOOP

David Marso
Administrator
In reply to this post by Shane S Allua
Your description might seem clear to you, but you really should post an
example of what the resulting data file needs to look like.
The following MIGHT be what you are after but YMMV.
data list free /varA varB varC varD.
begin data
1  1  0  1000
1  2  0  0
1  3  0  0
1  4  1  100
1  5  0  0
2  1  0  0
2  2  0  1500
2  3  2  200
end data.
COMPUTE link=varc.
AGGREGATE OUTFILE "C:\temp\junk.sav"
          /BREAK vara
          /link=max(link)
          /vare=SUM(vard).
COMPUTE link=varb.
MATCH FILES
     / FILE *
     / FILE "C:\temp\junk.sav"
     / BY vara link.
FORMATS ALL (F4.0).
LIST.

VARA VARB VARC VARD LINK VARE

   1    1    0 1000    1 1100
   1    2    0    0    2    .
   1    3    0    0    3    .
   1    4    1  100    4    .
   1    5    0    0    5    .
   2    1    0    0    1    .
   2    2    0 1500    2 1700
   2    3    2  200    3    .


Number of cases read:  8    Number of cases listed:  8





On Wed, 20 Oct 2010 08:36:42 -0400, Shane S Allua <[hidden email]> wrote:

>
>Hello,
>I have the following data file:
>
>
>
>varA            varB            varC            varD
>1               1               0               1000
>1               2               0               0
>1               3               0               0
>1               4               1               100
>1               5               0               0
>2               1               0               0
>2               2               0               1500
>2               3               2               200
>
>For each value of varB within varA, I need to add the values of varD only
>if varB=varC for any varB. So for varA = 1, I need to sum the values of
>varD where varB = 1 and varB=4 such that a new varE= 1000+100.
>
> For varA=2, I need to sum the values of varD for varB =2 and varB =3 such
>that varE= 1500+200.  Essentially, for each varB within varA, I need SPSS
>to loop through each value of varC and when varB=varC for any varB , sum
>the values of varD.
>
>I have tried several iterations of LOOP - END LOOP but have only succeeded
>when for any ONE case, varB = varC.
>
>If any one has any recommendations, your assistance would be greatly
>appreciated.
>
>
>
>Thanks!
>
>Shane

=====================
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
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?"
Reply | Threaded
Open this post in threaded view
|

Re: LOOP - END LOOP

Richard Ristow
In reply to this post by Shane S Allua
At 08:36 AM 10/20/2010, Shane S Allua wrote:

I have the following data file:
|-----------------------------|---------------------------|
|Output Created               |21-OCT-2010 18:26:41       |
|-----------------------------|---------------------------|
varA varB varC  varD

  1    1    0   1000
  1    2    0      0
  1    3    0      0
  1    4    1    100
  1    5    0      0

  2    1    0      0
  2    2    0   1500
  2    3    2    200

Number of cases read:  8    Number of cases listed:  8


For each value of varB within varA, I need to add the values of varD only if varB=varC for any varB. So for varA = 1, I need to sum the values of varD where varB = 1 and varB=4 such that a new varE= 1000+100.

I can understand you add the value where varB=1; but why, where varB=4? That doesn't match any value of varC.

For varA=2, I need to sum the values of varD for varB=2 and varB=3

But again, it doesn't look like varB=3 matches anything in varC.

Now, does Bob Walker's solution do what you want? If so, congratulations, Bob! Here's what I get from Bob's code:
|-----------------------------|---------------------------|
|Output Created               |21-OCT-2010 21:41:48       |
|-----------------------------|---------------------------|
    varA     varB     varC     varD varC_hit     varE

    1.00     1.00      .00  1000.00   100.00  1100.00
    1.00     2.00      .00      .00      .        .
    1.00     3.00      .00      .00      .        .
    1.00     4.00     1.00   100.00      .        .
    1.00     5.00      .00      .00      .        .
    2.00     1.00      .00      .00      .        .
    2.00     2.00      .00  1500.00   200.00  1700.00
    2.00     3.00     2.00   200.00      .        .

Number of cases read:  8    Number of cases listed:  8

===================== 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