Count number of distinct values across 400 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 | *(Q) How can I count the number of unique values across about 400 variables. *(A) Raynald Levesque 20002/08/16. * http://pages.infinit.net/rlevesqu/index.htm. DATA LIST LIST /var1 var2 var3 varn. BEGIN DATA 1 2 3 4 1 1 1 1 1 1 2 4 2 3 2 2 . 1 . 1 END DATA. LIST. *///////////////. DEFINE !nb(nbvars=!TOKENS(1) /v1=!TOKENS(1) /v2=!TOKENS(1)) VECTOR v=!v1 TO !v2 /val(!nbvars). LOOP #cnt=1 TO !nbvars. COMPUTE val(#cnt)=v(#cnt). END LOOP. LOOP #cnt1 = 1 TO !nbvars - 1. DO IF ~MISSING(val(#cnt1)). LOOP #cnt2 = #cnt1 + 1 TO !nbvars. IF val(#cnt2)=v(#cnt1) val(#cnt2)=$SYSMIS. END LOOP. END IF. END LOOP. COMPUTE distinct=!nbvars - NMISS(val1 TO !CONCAT('val',!nbvars)). * Clean up. ADD FILES FILE=* /DROP=val1 TO !CONCAT('val',!nbvars). !ENDDEFINE. *///////////////. * Call macro. SET MPRINT=yes. !nb nbvars=4 v1=var1 v2=varn. SET MPRINT=no. EXECUTE. |
Related pages
...