[SPSS] Using a loop/vector to run a syntax file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[SPSS] Using a loop/vector to run a syntax file

John Endahl
I am a self-taught SPSS user, and have struggled with creating a loop structure to run a syntax file that generates several custom tables in the vanilla version of SPSS Statistics 19. After doing additional research, I *think* the proper method to handle this issue is to place the reporting syntax into a macro structure, and call that macro within a loop or vector structure.

I need to run this syntax dozens of times, and would like to automate that process. Ideally, I would like to send the school number and school name into the macro, and have the macro search using the school number and add the school name in the TITLE command. (The school number would be substituted for "07621" in the filter condition of line 3 of the syntax, and the school name would probably need to be CONCATed in the TITLE call on line 13 in place of Holt Central HS).

As an example, if I had three buildings:

ID   Name
123 First School
253 Second School
521 Third School

Then the macro call might look like !myreport schoolnum schoolname.

The reporting syntax follows:

>
> USE ALL.
> COMPUTE filter_$=(Building = "07621" AND (Grade<=12 OR Grade = 14)).
>  * COMPUTE filter_$=(OperatingDistrict = "33170" AND (Grade<=12 OR Grade = 14)).
> VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.
> FORMATS filter_$ (f1.0).
> FILTER BY filter_$.
> EXECUTE.
>
> /**** Q1 - Total School Enrollment  ****/.
> /***** TABLE 1 ********/.
>
> TITLE Holt Central HS 2007-08 Pg. 4 - Student Enrollment by Subgroup.
> FREQUENCIES VARIABLES=EDFlag SWDFlag LEPFlag HomeFlag MigFlag
>   /ORDER=ANALYSIS.
>
> /***** TABLE 2 & 3 *******/.
>
> TITLE "Pg. 5/6 - Grade Level Attendance/Grade Level Enrollment".
> CTABLES
>   /VLABELS VARIABLES=Grade ATTFlag EXPFlag DISPLAY=LABEL
>   /TABLE Grade [C] BY ATTFlag [C][COUNT 'N' F40.0, ROWPCT.COUNT '%' PCT40.1] + EXPFlag [C][COUNT F40.0] + DRPFlag [C][COUNT F40.0]
>   /CATEGORIES VARIABLES=Grade ORDER=A KEY=VALUE EMPTY=EXCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE TOTAL=YES POSITION=BEFORE
>   /CATEGORIES VARIABLES=EXPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=DRPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE.
>
>
> /***** TABLE 4 & 5 ********/.
> TITLE "Pg. 6/7 - Sub-Group Enrollment/Sub-Group Attendance".
> CTABLES
>   /VLABELS VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag EthAmIndFlag EthHawFlag SWDFlag
>     LEPFlag HomeFlag MigFlag GenMFlag GenFFlag ATTFlag EXPFlag DRPFlag
>     DISPLAY=LABEL
>   /TABLE EDFlag [C][COUNT F40.0] + EthWFlag [C][COUNT F40.0] + EthBFlag [C][COUNT F40.0] +
>     EthHisFlag [C][COUNT F40.0] + EthAsFlag [C][COUNT F40.0] + EthAmIndFlag [C][COUNT F40.0] +
>     EthHawFlag [C][COUNT F40.0] + SWDFlag [C][COUNT F40.0] + LEPFlag [C][COUNT F40.0] + HomeFlag
>     [C][COUNT F40.0] + MigFlag [C][COUNT F40.0] + GenMFlag [C][COUNT F40.0] + GenFFlag [C][COUNT F40.0]
>     BY ATTFlag [C] + EXPFlag [C] + DRPFlag [C]
>   /CATEGORIES VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag EthAmIndFlag EthHawFlag
>     SWDFlag LEPFlag HomeFlag MigFlag GenMFlag GenFFlag EXPFlag DRPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE TOTAL=YES POSITION=BEFORE.
>


Am I headed in the right direction? How would I "step through" my school IDs and school names for the repeated calls to the macro? In my old BASIC days, it would be a simple DATA-READ pair within a FOR-NEXT loop, and I'd assign the values to an array. I'd then use a second FOR-NEXT loop to step through the elements of the ARRAY  as I called the subroutine. Unfortunately, my BASIC experiences don't seem to have much application in the SPSS syntax arena.

Unfortunately, I find the SPSS documentation to be far too comprehensive (too many options and flags) to act as a good learning tool, and haven't been able to find a tutorial online that addresses this type of process, or that doesn't require far greater knowledge of the control structures than I currently possess. Can anyone on the list offer some assistance, or point me to a good beginning-intermediate resource for learning these types of control structures? Thanks, in advance.

...and if this isn't the type of question that is appropriate for this list, my apologies!

--> John
--
John Endahl, Data Services Specialist
Ingham Intermediate School District
(517) 244-1233

"A mind is a fire to be kindled, not a vessel to be filled."

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: [SPSS] Using a loop/vector to run a syntax file

Maguin, Eugene
John,

Although there are some things I'm not clear about, I'm not sure that you
need a macro. From your example code, it looks like your data are all in one
file with a strucure like this:

SchID SchName Grade v1 ... V345

You could sort by schid and grade and then
Split file by schid grade.
Frequencies ...
Ctables ...

Split file off.

What you don't get with split files is the changing title.

You could do this with a looping macro and there is an example of one, I
seem to recall, in the macro section of the syntax reference, which you
should have on the help menu. I don't know much about macros but others on
the list are macro stars.

I'd suggest that you check the split file command first.


Gene Maguin


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
John Endahl
Sent: Friday, June 17, 2011 10:48 AM
To: [hidden email]
Subject: [SPSS] Using a loop/vector to run a syntax file

I am a self-taught SPSS user, and have struggled with creating a loop
structure to run a syntax file that generates several custom tables in the
vanilla version of SPSS Statistics 19. After doing additional research, I
*think* the proper method to handle this issue is to place the reporting
syntax into a macro structure, and call that macro within a loop or vector
structure.

I need to run this syntax dozens of times, and would like to automate that
process. Ideally, I would like to send the school number and school name
into the macro, and have the macro search using the school number and add
the school name in the TITLE command. (The school number would be
substituted for "07621" in the filter condition of line 3 of the syntax, and
the school name would probably need to be CONCATed in the TITLE call on line
13 in place of Holt Central HS).

As an example, if I had three buildings:

ID   Name
123 First School
253 Second School
521 Third School

Then the macro call might look like !myreport schoolnum schoolname.

The reporting syntax follows:

>
> USE ALL.
> COMPUTE filter_$=(Building = "07621" AND (Grade<=12 OR Grade = 14)).
>  * COMPUTE filter_$=(OperatingDistrict = "33170" AND (Grade<=12 OR Grade =
14)).

> VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.
> FORMATS filter_$ (f1.0).
> FILTER BY filter_$.
> EXECUTE.
>
> /**** Q1 - Total School Enrollment  ****/.
> /***** TABLE 1 ********/.
>
> TITLE Holt Central HS 2007-08 Pg. 4 - Student Enrollment by Subgroup.
> FREQUENCIES VARIABLES=EDFlag SWDFlag LEPFlag HomeFlag MigFlag
>   /ORDER=ANALYSIS.
>
> /***** TABLE 2 & 3 *******/.
>
> TITLE "Pg. 5/6 - Grade Level Attendance/Grade Level Enrollment".
> CTABLES
>   /VLABELS VARIABLES=Grade ATTFlag EXPFlag DISPLAY=LABEL
>   /TABLE Grade [C] BY ATTFlag [C][COUNT 'N' F40.0, ROWPCT.COUNT '%'
PCT40.1] + EXPFlag [C][COUNT F40.0] + DRPFlag [C][COUNT F40.0]
>   /CATEGORIES VARIABLES=Grade ORDER=A KEY=VALUE EMPTY=EXCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE
TOTAL=YES POSITION=BEFORE
>   /CATEGORIES VARIABLES=EXPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=DRPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE.
>
>
> /***** TABLE 4 & 5 ********/.
> TITLE "Pg. 6/7 - Sub-Group Enrollment/Sub-Group Attendance".
> CTABLES
>   /VLABELS VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag
EthAmIndFlag EthHawFlag SWDFlag
>     LEPFlag HomeFlag MigFlag GenMFlag GenFFlag ATTFlag EXPFlag DRPFlag
>     DISPLAY=LABEL
>   /TABLE EDFlag [C][COUNT F40.0] + EthWFlag [C][COUNT F40.0] + EthBFlag
[C][COUNT F40.0] +
>     EthHisFlag [C][COUNT F40.0] + EthAsFlag [C][COUNT F40.0] +
EthAmIndFlag [C][COUNT F40.0] +
>     EthHawFlag [C][COUNT F40.0] + SWDFlag [C][COUNT F40.0] + LEPFlag
[C][COUNT F40.0] + HomeFlag
>     [C][COUNT F40.0] + MigFlag [C][COUNT F40.0] + GenMFlag [C][COUNT
F40.0] + GenFFlag [C][COUNT F40.0]
>     BY ATTFlag [C] + EXPFlag [C] + DRPFlag [C]
>   /CATEGORIES VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag
EthAmIndFlag EthHawFlag
>     SWDFlag LEPFlag HomeFlag MigFlag GenMFlag GenFFlag EXPFlag DRPFlag
ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE
TOTAL=YES POSITION=BEFORE.
>


Am I headed in the right direction? How would I "step through" my school IDs
and school names for the repeated calls to the macro? In my old BASIC days,
it would be a simple DATA-READ pair within a FOR-NEXT loop, and I'd assign
the values to an array. I'd then use a second FOR-NEXT loop to step through
the elements of the ARRAY  as I called the subroutine. Unfortunately, my
BASIC experiences don't seem to have much application in the SPSS syntax
arena.

Unfortunately, I find the SPSS documentation to be far too comprehensive
(too many options and flags) to act as a good learning tool, and haven't
been able to find a tutorial online that addresses this type of process, or
that doesn't require far greater knowledge of the control structures than I
currently possess. Can anyone on the list offer some assistance, or point me
to a good beginning-intermediate resource for learning these types of
control structures? Thanks, in advance.

...and if this isn't the type of question that is appropriate for this list,
my apologies!

--> John
--
John Endahl, Data Services Specialist
Ingham Intermediate School District
(517) 244-1233

"A mind is a fire to be kindled, not a vessel to be filled."

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: [SPSS] Using a loop/vector to run a syntax file

ViAnn Beadle
In reply to this post by John Endahl
Rather than having a macro, you might consider using split files to do this
by splitting on school name. It wouldn't do the insert of the full name into
the title. It would however produce a header for each group.

The problem with a macro is that it is oblivious to the actual contents of
data and you have to manually insert the school number and name into it.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
John Endahl
Sent: Friday, June 17, 2011 8:48 AM
To: [hidden email]
Subject: [SPSS] Using a loop/vector to run a syntax file

I am a self-taught SPSS user, and have struggled with creating a loop
structure to run a syntax file that generates several custom tables in the
vanilla version of SPSS Statistics 19. After doing additional research, I
*think* the proper method to handle this issue is to place the reporting
syntax into a macro structure, and call that macro within a loop or vector
structure.

I need to run this syntax dozens of times, and would like to automate that
process. Ideally, I would like to send the school number and school name
into the macro, and have the macro search using the school number and add
the school name in the TITLE command. (The school number would be
substituted for "07621" in the filter condition of line 3 of the syntax, and
the school name would probably need to be CONCATed in the TITLE call on line
13 in place of Holt Central HS).

As an example, if I had three buildings:

ID   Name
123 First School
253 Second School
521 Third School

Then the macro call might look like !myreport schoolnum schoolname.

The reporting syntax follows:

>
> USE ALL.
> COMPUTE filter_$=(Building = "07621" AND (Grade<=12 OR Grade = 14)).
>  * COMPUTE filter_$=(OperatingDistrict = "33170" AND (Grade<=12 OR Grade =
14)).

> VALUE LABELS filter_$ 0 'Not Selected' 1 'Selected'.
> FORMATS filter_$ (f1.0).
> FILTER BY filter_$.
> EXECUTE.
>
> /**** Q1 - Total School Enrollment  ****/.
> /***** TABLE 1 ********/.
>
> TITLE Holt Central HS 2007-08 Pg. 4 - Student Enrollment by Subgroup.
> FREQUENCIES VARIABLES=EDFlag SWDFlag LEPFlag HomeFlag MigFlag
>   /ORDER=ANALYSIS.
>
> /***** TABLE 2 & 3 *******/.
>
> TITLE "Pg. 5/6 - Grade Level Attendance/Grade Level Enrollment".
> CTABLES
>   /VLABELS VARIABLES=Grade ATTFlag EXPFlag DISPLAY=LABEL
>   /TABLE Grade [C] BY ATTFlag [C][COUNT 'N' F40.0, ROWPCT.COUNT '%'
PCT40.1] + EXPFlag [C][COUNT F40.0] + DRPFlag [C][COUNT F40.0]
>   /CATEGORIES VARIABLES=Grade ORDER=A KEY=VALUE EMPTY=EXCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE
TOTAL=YES POSITION=BEFORE
>   /CATEGORIES VARIABLES=EXPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=DRPFlag ORDER=A KEY=VALUE EMPTY=INCLUDE.
>
>
> /***** TABLE 4 & 5 ********/.
> TITLE "Pg. 6/7 - Sub-Group Enrollment/Sub-Group Attendance".
> CTABLES
>   /VLABELS VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag
EthAmIndFlag EthHawFlag SWDFlag
>     LEPFlag HomeFlag MigFlag GenMFlag GenFFlag ATTFlag EXPFlag DRPFlag
>     DISPLAY=LABEL
>   /TABLE EDFlag [C][COUNT F40.0] + EthWFlag [C][COUNT F40.0] + EthBFlag
[C][COUNT F40.0] +
>     EthHisFlag [C][COUNT F40.0] + EthAsFlag [C][COUNT F40.0] +
EthAmIndFlag [C][COUNT F40.0] +
>     EthHawFlag [C][COUNT F40.0] + SWDFlag [C][COUNT F40.0] + LEPFlag
[C][COUNT F40.0] + HomeFlag
>     [C][COUNT F40.0] + MigFlag [C][COUNT F40.0] + GenMFlag [C][COUNT
F40.0] + GenFFlag [C][COUNT F40.0]
>     BY ATTFlag [C] + EXPFlag [C] + DRPFlag [C]
>   /CATEGORIES VARIABLES=EDFlag EthWFlag EthBFlag EthHisFlag EthAsFlag
EthAmIndFlag EthHawFlag
>     SWDFlag LEPFlag HomeFlag MigFlag GenMFlag GenFFlag EXPFlag DRPFlag
ORDER=A KEY=VALUE EMPTY=INCLUDE
>   /CATEGORIES VARIABLES=ATTFlag [1.00, .00, OTHERNM] EMPTY=INCLUDE
TOTAL=YES POSITION=BEFORE.
>


Am I headed in the right direction? How would I "step through" my school IDs
and school names for the repeated calls to the macro? In my old BASIC days,
it would be a simple DATA-READ pair within a FOR-NEXT loop, and I'd assign
the values to an array. I'd then use a second FOR-NEXT loop to step through
the elements of the ARRAY  as I called the subroutine. Unfortunately, my
BASIC experiences don't seem to have much application in the SPSS syntax
arena.

Unfortunately, I find the SPSS documentation to be far too comprehensive
(too many options and flags) to act as a good learning tool, and haven't
been able to find a tutorial online that addresses this type of process, or
that doesn't require far greater knowledge of the control structures than I
currently possess. Can anyone on the list offer some assistance, or point me
to a good beginning-intermediate resource for learning these types of
control structures? Thanks, in advance.

...and if this isn't the type of question that is appropriate for this list,
my apologies!

--> John
--
John Endahl, Data Services Specialist
Ingham Intermediate School District
(517) 244-1233

"A mind is a fire to be kindled, not a vessel to be filled."

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command SIGNOFF SPSSX-L For a list of
commands to manage subscriptions, send the command INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: [SPSS] Using a loop/vector to run a syntax file

John Endahl
This is a really late reply to this question, but the SPLIT FILE SEPARATE command was *exactly* the solution I need. Many thanks to Eugene and ViAnn for the assistance.

--> John