Create a new variable for each combination of 2 variables
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | **************************************************. ** Question. **************************************************. I'm having a terrible headache with the following macro. --------------- Start ------------------------- define !compint (!pos !tokens(1) /!pos !tokens(1)). compute nobreak = 1. exec. aggregate outfile ='c:\\windows\\temp\\SPSS_002.tmp' /break= nobreak /minv1 = min(!1) /maxv1 = max(!1) /minv2 = min(!2) /maxv2 = max(!2). match files /file=* /table= 'c:\\windows\\temp\\SPSS_002.tmp' /by= nobreak. do if ($casenum=1). write outfile= 'c:\\windows\\temp\\minmax.sps' /'define min1() ' minv1 ' !enddefine.' /'define max1() ' maxv1 ' !enddefine.' /'define min2() ' minv2 ' !enddefine.' /'define max2() ' maxv2 ' !enddefine.'. end if. execute. include file='c:\\windows\\temp\\minmax.sps'. !do !i = min1 !to max1. !do !j = min2 !to max2. !let !varnam = !concat(i,!1,!2,!i,!j). numeric !varnam (f1). !let !lab = !quote(!concat(!1, ' cat-',!i,' & ',!2,' cat-',!j)). variable label !varnam !lab. !doend. exec. !doend. match files /file=* /drop= nobreak minv1 minv2 maxv1 maxv2. exec. !enddefine. data list free / x z. begin data 1 2 1 3 1 1 2 3 2 3 2 1 1 2 end data. !compint x z. ---------------- end macro/syntaxis ----------------------------------- The above macro try to create new variables with the combination of all pairs of categories for the variables X and Z. The result should be (new variables): ixz11 ixz12 ixz13 ixz21 ixz22 ixz23 But when I run the macro, an error appears (Error # 1. Command name: !ERROR_MACRO ...) What I'm doing wrong ? **************************************************. ** Solution posted to SPSSX-L on 2001/09/26 by Ray. **************************************************. It is always tricky to 1)write a macro, 2)include it and 3)use it, all in the SAME macro. * Splitting the job in 2 macros and using !EVAL when referring to the dynamically defined macros solved the problem. * Here is the revised syntax. SET MPRINT=no. *////////////////////. define !compint (!pos !tokens(1) /!pos !tokens(1)) compute nobreak = 1. aggregate outfile ='c:\\temp\\SPSS_002.tmp' /break= nobreak /minv1 = min(!1) /maxv1 = max(!1) /minv2 = min(!2) /maxv2 = max(!2). match files /file=* /table= 'c:\\temp\\SPSS_002.tmp' /by= nobreak. do if ($casenum=1). write outfile= 'c:\\temp\\minmax.sps' /'define min1() ' minv1 ' !enddefine.' /'define max1() ' maxv1 ' !enddefine.' /'define min2() ' minv2 ' !enddefine.' /'define max2() ' maxv2 ' !enddefine.'. end if. execute. include file='c:\\temp\\minmax.sps'. EXECUTE. !compin2 !1 !2. !enddefine. *////////////////////. *////////////////////. define !compin2(!pos=!tokens(1) /!pos=!tokens(1)) !do !i = !EVAL(min1) !to !EVAL(max1) !do !j = !EVAL(min2) !to !EVAL(max2) !let !varnam = !concat(i,!1,!2,!i,!j) numeric !varnam (f1). !let !lab = !quote(!concat(!1, ' cat-',!i,' & ',!2,' cat-',!j)) variable label !varnam !lab. !doend. exec. !doend. match files /file=* /drop= nobreak minv1 minv2 maxv1 maxv2. exec. !enddefine. *////////////////////. data list free / x z. begin data 1 2 5 8 1 1 2 3 2 3 2 1 1 2 end data. FORMATS x z (F1.0). SET MPRINT=yes. !compint x z. |