Looping through variables

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

Looping through variables

Dimitri Liakhovitski
Hello!
In my data set I have a set of variables (next to each other) that start with the same string:

VAR_1, VAR_2, VAR_3, etc. until VAR_100

Now, I would like to create a new set of variables (NEW_1, NEW_2, NEW_3, etc. until NEW_100) based on the existing variables such that:

RECODE
  VAR_1
  (3=1)  (2=2)  (1=3)  INTO  NEW_1 .
EXECUTE .
RECODE
  VAR_2
  (3=1)  (2=2)  (1=3)  INTO  NEW_2 .
EXECUTE .
etc.

Is it possible to loop through all variables that start with VAR_ and recode each of them into a new variable with the same number (NEW_)?

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Looping through variables

Graham Wright-2
It's really simple, especially since the vars are right next to each
other. You just create a vector that holds all the variables and another
one of 100 new variables and then loop through each, coding one into the
other. Only thing is you can't use the recode command in a vector so you
have to use compute/if statements instead. The "F#.#" in the vector
command tells SPSS how long you want the new variables to be, so if the
old variables were length 4 with no decimals it would be F4.0
here's the code:

vector
OLD=VAR_1 to VAR_100/
NEW(100,F#.#).

loop #var=1 to 100.
if old(#var)=1 new(#var)=3.
if old(#var)=2 new(#var)=2.
if old(#var)=3 new(#var)=1.
end loop.

execute.


-Graham Wright

dl7631 wrote:

> Hello!
> In my data set I have a set of variables (next to each other) that start
> with the same string:
>
> VAR_1, VAR_2, VAR_3, etc. until VAR_100
>
> Now, I would like to create a new set of variables (NEW_1, NEW_2, NEW_3,
> etc. until NEW_100) based on the existing variables such that:
>
> RECODE
>   VAR_1
>   (3=1)  (2=2)  (1=3)  INTO  NEW_1 .
> EXECUTE .
> RECODE
>   VAR_2
>   (3=1)  (2=2)  (1=3)  INTO  NEW_2 .
> EXECUTE .
> etc.
>
> Is it possible to loop through all variables that start with VAR_ and recode
> each of them into a new variable with the same number (NEW_)?
>
> Thank you!
> --
> View this message in context: http://www.nabble.com/Looping-through-variables-tf3966002.html#a11256757
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: Looping through variables

Art Kendall-2
In reply to this post by Dimitri Liakhovitski
recode var_1 to var_100 (3=1)(2=2)(1=3) (else=copy) into new_1 to new_100.


Art Kendall
Social Research Consultants

dl7631 wrote:

> Hello!
> In my data set I have a set of variables (next to each other) that start
> with the same string:
>
> VAR_1, VAR_2, VAR_3, etc. until VAR_100
>
> Now, I would like to create a new set of variables (NEW_1, NEW_2, NEW_3,
> etc. until NEW_100) based on the existing variables such that:
>
> RECODE
>   VAR_1
>   (3=1)  (2=2)  (1=3)  INTO  NEW_1 .
> EXECUTE .
> RECODE
>   VAR_2
>   (3=1)  (2=2)  (1=3)  INTO  NEW_2 .
> EXECUTE .
> etc.
>
> Is it possible to loop through all variables that start with VAR_ and recode
> each of them into a new variable with the same number (NEW_)?
>
> Thank you!
> --
> View this message in context: http://www.nabble.com/Looping-through-variables-tf3966002.html#a11256757
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
>
>