IBM Support

How to automate the generation of CTABLES command syntax for series of tables with varying sets of variables

Question & Answer


Question

I'm using SPSS for Windows Custom Tables to generate a series of tables. Each table has a single categorical variable in the banner and a set of scale variables stacked in the stub. I need a couple of summary statistics generated for the scale variables. The question is that while the basic structure of the table will always remain the same, I have several different sets of scale variables which must be "tabled" against a varying list of categoricals -- ultimately I have hundreds of tables to generate. Is there a way I can automate this?

Answer

This can be done with a list-processing macro. This will let you maintain the same structure of your tables command each time and simply vary the raw lists of variables fed into it. A good example of this basic technique can be seen in simplified form on page 185 of the SPSS Programming and Data Management book, available in pdf on the MANUALS CD included with SPSS version 13 and later.

The variation in the example that follows is an extra nested macro DO IN loop to cycle through the categoricals and create one CTABLE command per categorical variable in the list.


*First, generate sample data for demonstration.

INPUT PROGRAM.
LOOP id=1 to 625.
DO REPEAT a=var1 to var20.
COMPUTE a=trunc(uniform(10))+1.
END REPEAT.
COMPUTE aa = trunc(uniform(12))+1.
COMPUTE bb = trunc(uniform(2))+1.
COMPUTE cc = trunc(uniform(3))+1.
COMPUTE dd = trunc(uniform(5))+1.
COMPUTE ee = trunc(uniform(4))+1.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXECUTE.
VARIABLE LEVEL aa TO ee (ORDINAL).


*define macro.
*macro will be fed two lists:
*'varlist' is the list of scale, "numbered" variables
*'byvarlist' is the list of categorical, individual "by" variables.

DEFINE !tableloop (varlist = !CHAREND('/')
/byvarlist = !CMDEND)

!LET !h = !HEAD(!varlist)
!LET !t = !TAIL(!varlist)

!DO !byvar !IN (!byvarlist)

CTABLES
/VLABELS VARIABLES = !byvar !varlist
DISPLAY = DEFAULT
/TABLE !h [MEAN F40.2, UCOUNT F40.0]
!DO !var !IN (!t) !CONCAT(" + ",!var," [MEAN F40.2, UCOUNT F40.0]") !DOEND
BY !byvar
/SLABELS POSITION=ROW VISIBLE=NO
/CATEGORIES VARIABLES=!byvar EMPTY=INCLUDE TOTAL=YES POSITION=AFTER.
!DOEND

!ENDDEFINE.

SET MPRINT=ON.

!tableloop varlist = var1 var2 var3 var4 var5 var6 var7 var8 var9 var10
var11 var12 var13 var14 var15 var16 var17 var18 var19 var20 /
byvarlist = aa bb cc dd ee.

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

63407

Document Information

More support for:
IBM SPSS Statistics

Software version:
Not Applicable

Document number:
419057

Modified date:
16 April 2020

UID

swg21477731

Manage My Notification Subscriptions