A loop and an increasing variable name.

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

A loop and an increasing variable name.

Gekko
Hi there,

I deleted my first question, since I thought, I have found it in the history, but I was wrong. (So sorry, if i ncase you get the mail a second time.)

Often, we have variables with increasing names like var1, var2, var3...
And sometimes we want to run the same procedure on everyone of these variables... until now, I always copied the old prod and changed just the variable name - but here is a more efficient way.

The only problem is, I dont know exacly the right syntax.

LOOP #I = 1 to 9.
- COMPUTE var[I]=value[I]+1.
END LOOP.


This is just a example, to explain the problem...and with the [I] it doesnt work, but does anybody know the correct syntax?


Thanks a lot for every kind of help.
Best greetings
Stefan
Reply | Threaded
Open this post in threaded view
|

Re: A loop and an increasing variable name.

Marks, Jim
Can you provide a small sample data set showing what you have and what
you want?

--jim

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Gekko
Sent: Tuesday, June 26, 2007 9:04 AM
To: [hidden email]
Subject: A loop and an increasing variable name.

Hi there,

I deleted my first question, since I thought, I have found it in the
history, but I was wrong. (So sorry, if i ncase you get the mail a
second
time.)

Often, we have variables with increasing names like var1, var2, var3...
And sometimes we want to run the same procedure on everyone of these
variables... until now, I always copied the old prod and changed just
the variable name - but here is a more efficient way.

The only problem is, I dont know exacly the right syntax.

LOOP #I = 1 to 9.
- COMPUTE var[I]=value[I]+1.
END LOOP.


This is just a example, to explain the problem...and with the [I] it
doesnt work, but does anybody know the correct syntax?


Thanks a lot for every kind of help.
Best greetings
Stefan
--
View this message in context:
http://www.nabble.com/A-loop-and-an-increasing-variable-name.-tf3982601.
html#a11306338
Sent from the SPSSX Discussion mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: A loop and an increasing variable name.

Simon Phillip Freidin
The loop needs to be proceeded with a vector command. Example from
command syntax manual.

DATA LIST FREE /V1 V2 V3 V4 V5 V6 V7 V8.
MISSING VALUES V1 TO V8 (99).
COMPUTE MEANSUB=MEAN(V1 TO V8).

VECTOR V=V1 TO V8.

LOOP #I=1 TO 8.
+ DO IF MISSING (V(#I)).
+ COMPUTE V(#I)=MEANSUB.
+ END IF.
END LOOP.
BEGIN DATA
1 99 2 3 5 6 7 8
2 3 4 5 6 7 8 9
2 3 5 5 6 7 8 99
END DATA.
LIST.

On 27/06/2007, at 7:45 AM, Marks, Jim wrote:

> Can you provide a small sample data set showing what you have and what
> you want?
>
> --jim
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On
> Behalf Of
> Gekko
> Sent: Tuesday, June 26, 2007 9:04 AM
> To: [hidden email]
> Subject: A loop and an increasing variable name.
>
> Hi there,
>
> I deleted my first question, since I thought, I have found it in the
> history, but I was wrong. (So sorry, if i ncase you get the mail a
> second
> time.)
>
> Often, we have variables with increasing names like var1, var2,
> var3...
> And sometimes we want to run the same procedure on everyone of these
> variables... until now, I always copied the old prod and changed just
> the variable name - but here is a more efficient way.
>
> The only problem is, I dont know exacly the right syntax.
>
> LOOP #I = 1 to 9.
> - COMPUTE var[I]=value[I]+1.
> END LOOP.
>
>
> This is just a example, to explain the problem...and with the [I] it
> doesnt work, but does anybody know the correct syntax?
>
>
> Thanks a lot for every kind of help.
> Best greetings
> Stefan
> --
> View this message in context:
> http://www.nabble.com/A-loop-and-an-increasing-variable-name.-
> tf3982601.
> html#a11306338
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: A loop and an increasing variable name.

Richard Ristow
In reply to this post by Gekko
At 09:44 AM 6/26/2007, Gekko wrote:

>I have a general problem (not onyl in this case). Often, variables are
>named like var1, var2, var3... and often we have to run the same
>procedures on variables 1 to n. Insead of copying every syntx and
>change the numbers manually, I would like to leave SPSS doing that for
>me in a loop -
>
>Here an example - and what it should look like, but in that way it
>doesnt
>work, so I think there has to be altered something in the [] or so..
>
>LOOP #I = 1 to 10.
>- COMPUTE var[I]=value[I]+1.
>END LOOP.

OK. You can't use part of the variable name as if it were a subscript.
More on that, below.

There are two ways to do what you want. I'm giving you syntax that only
works if var1,...,var10 are contiguous in the file; if they aren't, ask
again, and we'll do something more complicated. And since you don't
say, I'm assuming you have variables value1,...,value10; if THAT'S not
the case, post again.

Easier way:
DO REPEAT Var_List = var1   TO var10
          /Val_List = value1 TO value10.
.  COMPUTE Var_List = Val_List + 1.
END REPEAT.

Using LOOP and indexing. This adds some flexibility, but it's
flexibility you don't need, here.

I wrote, you can't use part of the variable name as if it were a
subscript. You can make a vector (a one-dimensional array) out of a
list of contiguous variables, and index into that:

VECTOR Var_List = var1   TO var10
       /Val_List = value1 TO value10.

LOOP #I = 1 to 10.
- COMPUTE Var_List(#I) = Val_List(#I)+1.
END LOOP.

Read *Command Syntax Reference* on DO REPEAT, VECTOR, and LOOP.

You can use any valid SPSS name where I've used 'Var_List' and
'Val_List'.

-Best of luck,
  Richard Ristow