Read ASCII file with fixed and free data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | SPSS AnswerNet: Result Solution ID: 100000680 Product: SPSS Base Title: Fixed and Free Data in the same ASCII data set Description: Q. I have data which consist of four records. The first three records have been entered in fixed column format. The fourth record is a problem, however, because it has been entered in free format. Not only is the record in free format, but I sometimes have situations whereby a person has a compound first and last name. I thought ahead and decided to use a space to delimit the pieces of the compound field and to use two spaces to delimit the individual compound fields. However I am still having trouble getting SPSS to correctly read the data. What can I do short of reentering all the data for the fourth record? A. You can read your data with the DATA LIST command, treating the free format record as a single alphanumeric record, then parsing the pieces as needed. The key to the properly unpacking the compound fields lies in the correct selection of the search string on the index function inside the loop (in this case ' '). DATA LIST RECORDS 4 / 1 v11 TO V15 11-20 / 2 v211 TO V215 11-25 / 3 v221 TO V225 21-35 / 4 #ALPHA 1-80 (A). COMPUTE #I=0. VECTOR #AL(5,A20). STRING ssn(A15) / fname(A20) / lname(A20) / sex(A1)/ A5(A5). LOOP . COMPUTE #I=#I+1. COMPUTE #X= INDEX(#ALPHA,' '). COMPUTE #AL(#I) = UPCASE(SUBSTR(#ALPHA,1,#X-1)). COMPUTE #ALPHA=LTRIM(SUBSTR(#ALPHA,#X+1,LENGTH(#ALPHA)-#X-1)). END LOOP IF INDEX(#ALPHA,' ')=1. COMPUTE ssn = SUBSTR(#AL(1),1,15). COMPUTE fname = SUBSTR(#AL(2),1,20). COMPUTE lname = SUBSTR(#AL(3),1,20). COMPUTE sex = SUBSTR(#AL(4),1,1 ). COMPUTE A5 = SUBSTR(#AL(5),1,5 ). BEGIN DATA. 001 1112131415 002 211212213214215 221222223224225 112-456-78928 jonathon pierre van der hickerson M blah 001 1112131415 002 211212213214215 221222223224225 456-289-15562 mary doe F blahs END DATA. LIST. Here are the results of the LIST command. V11: 11 12 13 14 15 211 212 213 214 215 221 222 223 224 225 112-456-78928 FNAME: JONATHON PIERRE VAN DER HICKERSON M BLAH V11: 11 12 13 14 15 211 212 213 214 215 221 222 223 224 225 456-289-15562 FNAME: MARY DOE F BLAHS Number of cases read: 2 Number of cases listed: 2 Created on: 01/10/2000 |
Related pages
...