SPSS AnswerNet: Result
Solution ID: 100000524
Product: SPSS Base
Title:
DATA LIST FREE with commas in SPSS
Description:
Q.
I am running SPSS 8.0 for Windows. I noticed on the DATA LIST
documentation that multiple commas will not be interpreted as
multiple data element delimiters. If I read my data with DATA
LIST using the FREE keyword the data is not read correctly.
DATA LIST LIST will get me around this problem, but I have
many subjects with missing data and it will be extremely difficult
to get around the problem. Is there anything I can do?
A.
There is if the data are structured such that there are two commas to
denote a single vacant value and 3 to denote 2 sequential missing values,
etc. The basic idea is to read the entire record into one long string and
then use parsing techniques to slice up the record into small pieces.
**** CAUTIONS:
1. You may not have any embedded commas in the values.
2. You will need to modify the program if records are longer than 80 columns
(DATA must be in an external file)
3. If the record is longer than 255 characters then you will need to read
multiple long strings and parse them as well.
4. MODIFY program to reflect the number of variables YOU have.
5. All variables for a case must be on the same record.
DATA LIST / X 1-80 (A) .
BEGIN DATA
1,1,6
2, ,5
3,2,3
end data .
VECTOR NX(3,a8) .
COMPUTE #VAR=0 .
LOOP .
COMPUTE #VAR = #VAR + 1 .
COMPUTE #START = INDEX(X,',') .
COMPUTE NX(#VAR) = RTRIM(RTRIM(LTRIM(LTRIM(SUBSTR(X,1,#START - 1)),"'")),"'").
COMPUTE X=LTRIM(SUBSTR(X,#START+1,LENGTH(X)-#START -1)) .
END LOOP IF INDEX(X,',') = 0 .
COMPUTE NX(#VAR+1) = RTRIM(RTRIM(LTRIM(LTRIM(X,"'"))),"'").
EXE.