Parse comma separated strings then autorecode results
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | DATA LIST FIXED /caseid 1-2 (F) a 3-255 (A). BEGIN DATA 01spain, russia, canada, soviet union 02Spain,USA, France,CANADA 03 04UK,Germany,Russia, Mexico,France,canada END DATA. * There will be problems if a country name has more than 17 letters or if there are more than 15 countries listed. set mprint=no. STRING #delim(A1) #tmp(A255) c1 TO c15 (A17). VECTOR c=c1 TO c15. COMPUTE #delim=",". COMPUTE #tmp=CONCAT(RTRIM(a),#delim). COMPUTE cnt=1. LOOP IF INDEX(#tmp,#delim)>1. COMPUTE c(cnt)=LOWER(LTRIM(SUBSTR(#tmp,1,INDEX(#tmp,#delim)-1))). COMPUTE cnt=cnt + 1. COMPUTE #tmp=SUBSTR(#tmp,INDEX(#tmp,#delim)+1). END LOOP. *////////////////. DEFINE !apply (id=!TOKENS(1) /stem=!TOKENS(1) /nb=!TOKENS(1) /fmt=!TOKENS(1)) SAVE OUTFILE='c:\\temp\\original data.sav'. STRING s(!fmt). VECTOR sv=!CONCAT(!stem,'1') TO !CONCAT(!stem,!nb). LOOP cnt=1 TO !nb. COMPUTE s=sv(cnt). XSAVE OUTFILE='c:\\temp\\data1.sav' /KEEP=!id cnt s. END LOOP. EXECUTE. GET FILE='c:\\temp\\data1.sav'. SELECT IF RTRIM(s)<>"". AUTORECODE s /INTO srec. N OF CASES 1. SAVE OUTFILE='c:\\temp\\label definitions.sav'. GET FILE='c:\\temp\\data1.sav'. SELECT IF RTRIM(s)<>"". SORT CASES BY s. AGGREGATE /OUTFILE=* /BREAK=s /!id = N(!id). COMPUTE recnb=$CASENUM. SAVE OUTFILE='c:\\temp\\data2.sav'. GET FILE='c:\\temp\\data1.sav'. SORT CASES BY s. MATCH FILES FILE=* /TABLE='c:\\temp\\data2.sav' /BY=s. SORT CASES BY !id cnt. VECTOR !stem (!nb F1). COMPUTE !stem(cnt)=recnb. AGGREGATE /OUTFILE=* /BREAK=!id /!CONCAT(!stem,'1') TO !CONCAT(!stem,!nb) = FIRST(!CONCAT(!stem,'1') TO !CONCAT(!stem,!nb)). !DO !cnt=1 !TO !nb RENAME VARIABLE (!CONCAT(!stem,!cnt)=srec). APPLY DICTIONARY FROM='c:\\temp\\label definitions.sav'. RENAME VARIABLE (srec=!CONCAT(!stem,!cnt)). !DOEND !ENDDEFINE. *////////////////. SET MPRINT=yes. * in next macro call, variables c1 to c15 must be globally autorecoded where the format of each var is a15 the case id variable is named caseid. !apply id=caseid stem=c nb=15 fmt=a15. SET MPRINT=no. |
Related pages
...