I would like to write a SPSS macro to perform three operations:
- generate a custom table,
- clean the output window,
- export table.
As you know SPSS macro facility allows to use two types of loops: 'numeric' like (!do !i = !x !to !y) and 'list'/'for each' like (!do !i !in (!1)). My goal is to create a macro with a call as below:
col v1 v2 / "Sheet A" "Sheet B".
working this way (with a 'list' like loop):
- Get first variable name (v1)
- Put it in the
ctablesmacro section - Get first sheet name (string "Sheet A")
- Put it in the
output exportmacro section - Get second variable name
- ...
- Get second sheet name
- ...
and so on.
At first I wrote this:
define col (!positional !charend('/')
/!positional !cmdend)
!do !i !in (!1)
output close all.
ctables
/table x1 + x2
by !i [mean f1.2, totals[mean f1.2]].
output modify
/select logs headings texts warnings pagetitles outlineheaders notes
/deleteobject delete = yes.
!doend
!do !i !in (!2)
output export
/contents export = visible
/xlsx documentfile = "E:\path\file.xlsx"
operation = createsheet
sheet = !i.
!doend
!enddefine.
*** MACRO CALL.
col v1 v2 / "Sheet A" "Sheet B".
The output was overall incorrect because I got tables only with the second variable. However, in the file I found two sheets with correct names. So, I tried nesting:
define col (!positional !charend('/')
/!positional !cmdend)
!do !i !in (!1)
!do !j !in (!2)
output close all.
ctables
/table x1 + x2
by !i [mean f1.2, totals[mean f1.2]].
output modify
/select logs headings texts warnings pagetitles outlineheaders notes
/deleteobject delete = yes.
output export
/contents export = visible
/xlsx documentfile = "E:\path\file.xlsx"
operation = createsheet
sheet = !j.
!doend
!doend
!enddefine.
*** MACRO CALL.
col v1 v2 / "Sheet A" "Sheet B".
The output was exactly the same meaning SPSS crosses each element from the list on the left side of '/' with each element from the list on the right side of '/' and overwrites previous results in Excel file. My goal is to receive macro expansions like this:
* FIRST EXPANSION:
...
ctables
/table x1 + x2
by v1 [mean f1.2, totals[mean f1.2]].
...
output export
/contents export = visible
/xlsx documentfile = "E:\path\file.xlsx"
operation = createsheet
sheet = "Sheet A".
* SECOND (LAST) EXPANSION:
...
ctables
/table x1 + x2
by v2 [mean f1.2, totals[mean f1.2]].
...
output export
/contents export = visible
/xlsx documentfile = "E:\path\file.xlsx"
operation = createsheet
sheet = "Sheet B".
In other words - 2 lists x 2 elements but only two loops - not four. Does anyone have any idea how to get such result?