Global Autorecode
A nice problem: autorecode many string variables where the recode formula (eg a=1,b=2, etc) is the same for all variables even though none of the variables have all possible values.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | * GlobalAutorecode.sps *. *(Q). I have data such as the following. * ID STR1 STR2 STR3 STR4 STRX 1 a b c d e 2 b d e 3 c c a c b 3 e c a 2 e b a 1 a b * and would like to use all of these string variables to do one autorecode, let's say something like an autorecode overall. * No variable has all strings, but all variables together have all strings which can appear. * What I need is one dictionary for all of my variables. * Do you know an easy way to do this? So that I get this result: ID STR1 STR2 STR3 STR4 STRX 1 1 2 3 4 5 2 2 4 5 3 3 3 1 3 2 3 5 3 1 2 5 2 1 1 1 2 "a"=1, "b"=2, "c"=3, "d"=4, "e"=5 *(A) By Raymald Levesque 2002/07/16. * Enter data example. DATA LIST LIST /id (F1.0) str1 str2 str3 str4 str5 (5A1). BEGIN DATA 1 a b c d e 2 b d e 3 c c a c b 4 e c a 5 e b a 6 a b END DATA. LIST. * Solution. First define macro then run it. *////////////////. DEFINE !apply (stem=!TOKENS(1) /nb=!TOKENS(1)) SAVE OUTFILE='c:\temp\original data.sav'. STRING s(A1). VECTOR s=!CONCAT(!stem,'1') TO !CONCAT(!stem,!nb). LOOP cnt=1 TO !nb. COMPUTE s=s(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 str(!nb F1). COMPUTE str(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. *////////////////. * Run macro. SET MPRINT=yes. !apply stem=str nb=5. SET MPRINT=no. |
P.S. Starting version SPSS 13.0 the similar result could be achieved through GROUP
subcommand of AUTORECODE
command. Way simpler! :-)
104 105 106 107 108 | AUTORECODE VARIABLES=str1 str2 str3 str4 str5 /INTO nstr1 nstr2 nstr3 nstr4 nstr5 /GROUP /BLANK=MISSING /PRINT. |
Related pages
...