1
votes

I use a custom table function in SPSS in order to build tables. Now, I got ideal configuration for my tables and syntax looks like this:

DATASET ACTIVATE DatenSet2.
* Benutzerdefinierte Tabellen.
CTABLES
  /FORMAT EMPTY=ZERO MISSING='.' MINCOLWIDTH=1.27 MAXCOLWIDTH=2.54 UNITS=CM
  /SMISSING VARIABLE
  /VLABELS VARIABLES=Q3 DISPLAY=NONE  /VLABELS VARIABLES=gruppebreak regbreak sexbreak alterbreak 
    bildbreak erwerbbreak DISPLAY=LABEL
  /TABLE Q3 [C][COUNT F40.0, COLPCT.COUNT PCT40.1] BY Q3 [C] + gruppebreak [C] + regbreak [C] + 
    sexbreak [C] + alterbreak [C] + bildbreak [C] + erwerbbreak [C]
  /SLABELS POSITION=ROW VISIBLE=NO
  /CATEGORIES VARIABLES=Q3 [1, 2] EMPTY=EXCLUDE TOTAL=YES LABEL='Total' POSITION=AFTER
  /CATEGORIES VARIABLES=gruppebreak regbreak sexbreak alterbreak bildbreak erwerbbreak ORDER=A 
    KEY=VALUE EMPTY=INCLUDE
  /TITLES
    TITLE=')TABLE'.

In this syntax Q3 is the variable I am analyzing according to 6 different criteria. These six remain unchanged all the time. My problem is that I have 300 variables like Q3 and I would like to generate tables for all of them. I wanted to ask whether there is some possibility to build a "loop" which will simply replace Q3 with other variables in the Dataset until all variables are analyzed?

Thank you in advance...

2

2 Answers

1
votes

I'm not sure what you are going to do with all that output, but you can automate its production.

Simplest, you can define a macro for your CTABLES syntax that takes just one parameter, the variable name, and supplies all the other parameters.

That still leaves you with the task of calling the macro 300 times.

If you have installed the Python Essentials for Statistics via the SPSS Community website (www.ibm.com/developerworks/spssdevcentral), you can embed your syntax in a small Python program that iterates over all the variables of interest.

1
votes

Here I define what will be the loop as 'LOOP1'. You can use this exact syntax for the first two lines, it is just boilerplate. Next I put the table I want to generate. Anywhere you see a '!i' the current variable in the loop will be put in there. The '!DOEND.' and '!ENDDEFINE.' are also boilerplate you can leave them. So far just replace your table where mine is and replace Q3 with !i anywhere it appears in your table. The last part where I write LOOP1 Q1 Q2 ... is where I am now calling the loop. This is where you would put your 300 variables which you would like to loop over. You must follow your variable list with the forward slash (/) and the period.

DEFINE LOOP1(!POS !CHAREND('/')).

!DO !i !IN (!1).

CTABLES

  /VLABELS VARIABLES = !i age_groups_1 DISPLAY=BOTH

/TABLE !i [C] BY age_groups_1 [C][COLPCT.COUNT PCT40.1, COLPCT.COUNT.LCL PCT40.1, 
    COLPCT.COUNT.UCL PCT40.1, UCOUNT F40.0]

/SLABELS POSITION=ROW

/CATEGORIES VARIABLES=!i [1, 2] EMPTY=EXCLUDE TOTAL=YES POSITION=AFTER

/CATEGORIES VARIABLES=age_groups_1 ORDER=A KEY=VALUE EMPTY=INCLUDE

/CRITERIA CILEVEL=95.

!DOEND.

!ENDDEFINE.

LOOP1 Q1 Q2 Q3 Q4 Q5A Q5B Q5C Q5D Q5E Q6 Q6_dummy Q6_recode Q7_01 Q7_02 Q7_03 Q7_04 Q7_05 Q7_06 Q7_07 
Q7_08 Q7_09 Q7_10 Q7_11 Q7_12 Q7_13 Q8A Q8B Q8C Q9 Q10 Q11_1 Q11_2 Q11_3 Q11_4 Q11_5 Q12 Q13_1 
Q13_2 Q13_3 Q13_4 Q13_5 Q13_6 Q13_7 Q14 Q15A Q15B Q15C Q15D Q15E Q15F Q15G Q15H Q16A Q16B Q16C Q16D 
Q16E Q16F Q16G Q17 Q18 Q19 Q20_1 Q20_2 Q20_3 Q20_4 Q20_5 Q20_6 Q21A Q21B Q21C Q21D Q21E Q21F 
Q22_C01 Q22_C02 Q22_C03 /.