Creation of variables based on value held in a single variable

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

Creation of variables based on value held in a single variable

Jim Arnold-3
Hello all,

I have failed to turn up a solution to my question by searching the list archives so I really hope someone can address this.  I've distilled the specifics of my data file down considerably for simplicity.

I have a data file that has four variables (task01->task04) that contain a value range from 1 to 10 (these are codes that pertain to 'tasks' the individual is exposed to).  From this I need to create, for each case, 10 new variables that contain a 0 (they had not been exposed to this particular task) or a 1 (they had been exposed to this task) but in a multivariate format using varstocases.  So if an individual had the following:
 

Id Task01 Task02 Task03 Task04
1 3 5 7 9
2 2 6 8 9

I need it to look like this (V1 -> V10 are newly created based on the above):

Id V1 V2 V3 V4 V5 V6 V7 V8 V9 V10



1 0 0 1 0 0 0 0 0 0 0



1 0 0 0 0 1 0 0 0 0 0



1 0 0 0 0 0 0 1 0 0 0



1 0 0 0 0 0 0 0 0 1 0



2 0 1 0 0 0 0 0 0 0 0
2 0 0 0 0 0 1 0 0 0 0
2 0 0 0 0 0 0 0 1 0 0
2 0 0 0 0 0 0 0 0 1 0
 

I've been able, through brute force, to create this output format but the amount of syntax and the complexity of the loop structure seemed way over the top (I am rather new with SPSS scripting).

Is there a way through SPSS syntax to address a variable range (V1->V10) based on the value held in another variable (Task01, for instance).    For id 1 Task01, create a 1 in V3 (because Task01=3) and so on?  

Any ideas would be hugely appreciated.

Jim
Reply | Threaded
Open this post in threaded view
|

Re: Creation of variables based on value held in a single variable

David Marso
Administrator
>Creation of variables based on value held in a single variable

>Hello all,
>I have failed to turn up a solution to my question by searching the list
>archives so I really hope someone can address this.  I've distilled the
>specifics of my data file down considerably for simplicity.

This sort of solution has been discussed in the archives ad nauseum
since the early 1990's or before ;-)

>I have a data file that has four variables (task01->task04) that contain a
>value range from 1 to 10 (these are codes that pertain to 'tasks' the
>individual is exposed to).  From this I need to create, for each case, 10
>new variables that contain a 0 (they had not been exposed to this particular
>task) or a 1 (they had been exposed to this task) but in a multivariate
>format using varstocases.  So if an individual had the following:


>  Id Task01 Task02 Task03 Task04  1 3 5 7 9  2 2 6 8 9

>I need it to look like this (V1 -> V10 are newly created based on the
>above):

 > Id V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
> 1 0 0 1 0 0 0 0 0 0 0
 >1 0 0 0 0 1 0 0 0 0 0
 >1 0 0 0 0 0 0 1 0 0 0
 >1 0 0 0 0 0 0 0 0 1 0

  >2 0 1 0 0 0 0 0 0 0 0 
  >2 0 0 0 0 0 1 0 0 0 0
  >2 0 0 0 0 0 0 0 1 0 0 
  >2 0 0 0 0 0 0 0 0 1 0

Thank you for the clear, concise explication of your existing data structure and desired results ;-).  You would not believe how many people skip this step ;-)

>I've been able, through brute force, to create this output format but the
>amount of syntax and the complexity of the loop structure seemed way over
>the top (I am rather new with SPSS scripting).

Would be nice to see the code you are struggling with in future posts if necessary.

>Is there a way through SPSS syntax to address a variable range (V1->V10)
>based on the value held in another variable (Task01, for instance).    For
>id 1 Task01, create a 1 in V3 (because Task01=3) and so on?

YES!!! See examples of VECTOR in the Syntax Manual.

>Any ideas would be hugely appreciated.

>Jim
---------------------------
This can be easily achieved with 10 lines of code or less
(less if you use VARSTOCASES.  I'm *OLD* school, but my
code works anywhere - back to version 4 on a mainframe-).

<UNTESTED specific example>

VECTOR TASK = Task1 TO Task4.
LOOP #=1 TO 4.
COMPUTE Value=TASK(#).
XSAVE OUTFILE="Temp.sav" / KEEP ID Value .
END LOOP.
EXECUTE.
GET FILE "Temp.sav" .

VECTOR V(10).
COMPUTE V(Value)=1.
RECODE V1 TO V10 (MISSING=0).
EXECUTE.
ERASE FILE "Temp.sav" .

HTH, David Marso



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?"