Fitz
Fitz

Reputation: 21

easier use of loops and vectors in spss to combine variables

I have a student who has gathered data in a survey online whereby each response was given a variable, rather than the variable having whatever the response was. We need a scoring algorithm which reads the statements and integrates. I can do this with IF statements per item, e.g.,

if Q1_1=1 var1=1.
if Q1_2=1 var1=2.
if Q1_3=1 var1=3.
if Q1_4=1 var1=4.

Doing this for a 200 item survey (now more like 1000) will be a drag and subject to many typos unless automated. I have no experience of vectors and loops in SPSS, but some reading suggests this is the way to approach the problem.

I would like to run if statements as something like (pseudocode):

for items=1 1 to 30
for responses=1 to 4
  if Q1_2_1=1 a=1.
  if Q1_2=1 a=2.
  if Q1_3=1 a=3.
  if Q1_4=1 a=4.
compute newitem(items)=a.
next response.
next item.

Which I would hope would produce a new variable (newitem1 to 30) which has one of the 4 responses for it's original corresponding 4 variable information.

Never written serious spss code before: please advise!

Upvotes: 2

Views: 558

Answers (1)

eli-k
eli-k

Reputation: 11310

This will do the Job:

* creating some sample data.    
data list free (",")/Item1_1 to Item1_4 Item2_1 to Item2_4 Item3_1 to Item3_4.
begin data
1,,,,,1,,,,,1,,
,1,,,1,,,,1,,,,
,,,1,,,1,,,,,1,
end data.

* now looping over the items and constructing the "NewItems".
do repeat Item1=Item1_1 to Item1_4
    /Item2=Item2_1 to Item2_4
    /Item3=Item3_1 to Item3_4
    /Val=1 to 4.
  if Item1=1 NewItem1=Val.
  if Item2=1 NewItem2=Val.
  if Item3=1 NewItem3=Val.
end repeat.
execute.

In this way you run all you loops simultaneously.

Note that "ItemX_1 to ItemX_4" will only work if these four variables are consecutive in the dataset. If they aren't, you have to name each of them separately - "ItemX_1 ItemX_2 ItemX_3 itemX_4".

Now if you have many such item sets, all named regularly as in the example, the following macro can shorten the process:

define !DoItems (ItemList=!cmdend)
 !do !Item !in (!ItemList)
   do repeat !Item=!concat(!Item,"_1") !concat(!Item,"_2") !concat(!Item,"_3") !concat(!Item,"_4")/Val=1 2 3 4.
     if !item=1 !concat("New",!Item)=Val.
   end repeat.
 !doend
 execute.
!enddefine.

* now you just have to call the macro and list all your Item names:
!DoItems ItemList=Item1 Item2 Item3.

The macro will work with any item name, as long as the variables are named ItemName_1, ItemName_2 etc'.

Upvotes: 1

Related Questions