Get all possible crossproducts of pairs of 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 | *(Q) I need to create all possible combinations of crossproducts from a range of variables. That is to say, I need to multiply each variable by all the other variables and save the products as new variables. The matrix is contiguous so I figure I can use a range function in SPSS. I am having difficulty figuring out how to do this. As there are 168 column vectors in this data-set, the task is too formidable to attempt one variable at a time. * Can anyone suggest a solution. *(A) Posted by rlevesque@videotron.ca to SPSSX-L on 2002/01/27. *****************. * PART A. * The following answers the question: new variables are created for each product. * The problem is that it is very difficult to use the resulting variables because they do not have descriptive labels. * PART B below fills that gap. *****************. * Create a dummy data file of 5 variables. * The method works for up to the maximum number of variables supported by SPSS which is about 32, 000. * In terms of the initial number of variables, this means around 250 variables. * You will have to change the numbers 4,5 and 10 in the syntax to the numbers corresponding to your actual data file. DATA LIST LIST /agecateg var2 crit19 cntry lastvarn. BEGIN DATA 1 2 3 4 5 6 2 5 1 3 6 4 2 5 1 4 7 3 2 5 8 9 6 1 END DATA. LIST. SAVE OUTFILE='c:\\temp\\mydata.sav'. * given n variables, there are (n-1)*n/2 crossproducts. * When n=5, there are 10 cross products, hence we need cp1 to cp10 to hold the cross products. VECTOR v=agecateg TO lastvarn /cp(10F8.0). COMPUTE #idx=1. LOOP #cnt1=1 TO 4. LOOP #cnt2=#cnt1 +1 TO 5. COMPUTE cp(#idx)=v(#cnt1)*v(#cnt2). COMPUTE #idx=#idx+1. END LOOP. END LOOP. EXECUTE. SAVE OUTFILE='c:\\temp\\mydata2.sav'. * That's it. *****************. * PART B. * Let's see now how we can add meaningful labels to cp variables. *****************. GET FILE='c:\\temp\\mydata.sav'. * at this point we only need to work with the variable names, so we lmit the data file to a single case. N OF CASES 1. FLIP. * The variable names are now in a string variables, let's number them. COMPUTE id=$CASENUM. * We need the variable names to be in the same case. Define a vector to hold the names. *STRING vn1 TO vn5(A8). VECTOR vn(5A8). * Copy each label to its required column. COMPUTE vn(id)=case_lbl. COMPUTE nobreak=1. * Then collapse all cases into a single case. AGGREGATE /OUTFILE=* /BREAK=nobreak /vn1 TO vn5 = MAX(vn1 TO vn5). * Build the variable label definitions. VECTOR v=vn1 TO vn5. STRING #deflab(A50). COMPUTE #idx=1. LOOP #cnt1=1 TO 4. LOOP #cnt2=#cnt1 +1 TO 5. COMPUTE #deflab=CONCAT( 'VARIABLE LABEL cp',LTRIM(STRING(#idx,F8.0)),' "', v(#cnt1),' * ',v(#cnt2),'".'). * Create a syntax file which will define the variable labels. WRITE OUTFILE='c:\\temp\\define var labels.SPS' /#deflab. COMPUTE #idx=#idx+1. END LOOP. END LOOP. EXECUTE. GET FILE='c:\\temp\\mydata2.sav'. INCLUDE 'c:\\temp\\define var labels.SPS'. FREQ ALL. |
Related pages
...