Date: Tue, 3 Dec 2002 10:13:52 -0500
Reply-To: "Johnson, Wendy SBCCOM(N)" <Wendy.Johnson@natick.army.mil>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Johnson, Wendy SBCCOM(N)" <Wendy.Johnson@natick.army.mil>
Subject: Re: String variables: breaking a list into it's parts
Content-Type: text/plain; charset="iso-8859-1"
Thanks to everyone who responded to my question. As a result, I've come up with syntax which will count the commas, then run a loop based on the number of commas. Because SPSS won't let me use a variable to define a vector, and because the length of the
string is known, I've made a ridiculous number of vector variables based on string length.
data list list (tab)/id (f4) list (a80).
3333 cheese, peanut butter
4213 beef curry, green eggs and ham, skittles
1125 cigarettes, beverage base, M&Ms, gatorade, Twix
1238 gum, peanuts, grape juice, coffee, potato chips
*COMPUTE THE NUMBER OF COMMAS .
compute n_commas = 0.
loop #next = 1 to 80.
if substr(list, (#next),1) eq "," n_commas = n_commas + 1.
*THE NUMBER OF ITEMS WILL BE ONE MORE THAN THE NUMBER OF COMMAS.
compute n_items = n_commas + 1 .
*BREAK THE LIST INTO INDIVIDUAL ITEMS.
vector list(80,a80) .
loop #next = 1 to n_items by 1.
COMPUTE comma = INDEX(list,',') .
do if (comma ne 0).
compute list(#next) = substr(list,1,comma-1).
compute list = substr(list,comma+2).
compute list(#next) = list .
end loop .
*MAKE A WIDE FILE LONG.
VARSTOCASES /MAKE items FROM list1 to list80
/INDEX = order (80)
/KEEP = id
/NULL = KEEP.
*REMOVE EXTRA LINES.
select if (items ne "").
compute items = lower(items).
freq /var items.