2
votes

I have data like this:

Person Region Grapes Apples Bananas
Bob    IV     4      2      1
Jane   II     0      1      2

I need to restructure (vars to cases?) to return one record for each number of fruits the person had, like this:

Person Region Fruit
Bob    IV     Grapes
Bob    IV     Grapes
Bob    IV     Grapes
Bob    IV     Grapes
Bob    IV     Apples
Bob    IV     Apples
Bob    IV     Bananas
Jane   II     Apples
Jane   II     Bananas
Jane   II     Bananas

The code below will return this, which is a step closer but not quite there:

Person Region Fruit   Count
Bob    IV     Grapes  4
Bob    IV     Apples  2
Bob    IV     Bananas 1
Jane   II     Apples  1
Jane   II     Bananas 2

VARSTOCASES /MAKE Count FROM Grapes Apples Bananas /INDEX=Fruit(Count) /KEEP=Person Region /NULL=KEEP.

Any suggestions?

1

1 Answers

2
votes

Yes, that's the first step for the code! Next, just add this piece:

loop # = 1 to Count.
xsave outfile = 'c:/personal/disaggregated.sav' / drop count.
end loop.

That's all you need - it will loop through and repeat the values based on your Count variable.

Cheers, Valeria