Next Upcoming Google+ Hangout: Tuesday, August 27 @ 7PM (CST) - To Participate CLICK HERE

Search For Topics/Content

SPSS Topics Discussion > User defined function in SPSS

Hi,

I wanted to create some user defined function in which I want to define my syntax and need to pass only variable name every time to run the syntax. For example: I have to run number of custom tables and wanted to pass common keyword/syntax globally. Can anyone help me on this.

CTABLES
/FORMAT EMPTY='-' MISSING='.'
/SMISSING VARIABLE
/VLABELS VARIABLES=Q1 Age_s DISPLAY=NONE /VLABELS VARIABLES=Age_gr Maritalstatus_gr Income_gr
DISPLAY=LABEL
/TABLE Q1 [C][COUNT F40.0, COLPCT.COUNT PCT40.1] + Age_s [S][COUNT 'Sigma' F40.0, MEAN, STDDEV, SEMEAN] BY Age_gr [C] +
Maritalstatus_gr [C] + Income_gr [C]
/SLABELS POSITION=ROW
/CATEGORIES VARIABLES=Q1 [SUBTOTAL='20-34 (Net)', 2, 3, 4, SUBTOTAL='35-49 (Net)', 5, 6, 7,
SUBTOTAL='50-60 (Net)', 8, 9, OTHERNM] EMPTY=INCLUDE POSITION=BEFORE
/CATEGORIES VARIABLES=Age_gr ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=BEFORE
/CATEGORIES VARIABLES=Maritalstatus_gr Income_gr ORDER=A KEY=VALUE EMPTY=INCLUDE
/COMPARETEST TYPE=MEAN ALPHA=0.2 ORIGIN=COLUMN INCLUDEMRSETS=YES
CATEGORIES=ALLVISIBLE MEANSVARIANCE=ALLCATS
/COMPARETEST TYPE=PROP ALPHA=0.20 ORIGIN=COLUMN INCLUDEMRSETS=YES
CATEGORIES=ALLVISIBLE
/TITLES
TITLE='Q1. Please select your age.'.

CTABLES
/FORMAT EMPTY='-' MISSING='.'
/SMISSING VARIABLE
/VLABELS VARIABLES=Q2 Income_s DISPLAY=NONE /VLABELS VARIABLES=Age_gr Maritalstatus_gr Income_gr
DISPLAY=LABEL
/TABLE Q2 [COUNT F40.0, COLPCT.COUNT PCT40.1] + Income_s [COUNT 'Sigma' F40.0, MEAN, STDDEV, SEMEAN] BY Age_gr [C] +
Maritalstatus_gr [C] + Income_gr [C]
/SLABELS POSITION=ROW VISIBLE=NO
/CATEGORIES VARIABLES=Q2 [SUBTOTAL='< $50 K/Yr (net)', 1, 2, 3, 4, SUBTOTAL='$50 - <$100 K/Yr '+
'(net)', 5, 6, 7, 8, SUBTOTAL='$100 K+/Yr (net)', 9, 10, 11, 12, OTHERNM] EMPTY=INCLUDE
POSITION=BEFORE
/CATEGORIES VARIABLES=Age_gr ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=BEFORE
/CATEGORIES VARIABLES=Maritalstatus_gr Income_gr ORDER=A KEY=VALUE EMPTY=INCLUDE
/COMPARETEST TYPE=MEAN ALPHA=0.20 ORIGIN=COLUMN INCLUDEMRSETS=YES
CATEGORIES=ALLVISIBLE MEANSVARIANCE=ALLCATS
/COMPARETEST TYPE=PROP ALPHA=0.20 ORIGIN=COLUMN INCLUDEMRSETS=YES
CATEGORIES=ALLVISIBLE
/TITLES
TITLE='Q2. Please select your household income before tax.'.

Do let me know if you have any question.

Regards,
Gaurav

May 17, 2012 | Unregistered CommenterGaurav

I'm not sure if I understand your question 100%, but I think you need to use the SPSS Production utility to accomplish what you want.

1. Under "Utilities", click on "Production" and start a new production job.
2. In the dialogue, click "new" to start a new job.
3. Browse to the syntax that you want to run on an automated basis and select it (it needs to have the dynamic variable setup, using a constant with an @ sign. For example: @dynamicVar
4. Click on the Runtime Value tab
5. On this tab, put @dynamicVar (or whatever you use) in the symbol column, put in desired text in the prompt column (e.g. "Please enter the variable to be used in table").
- the "Default Value" column is optional.
- to my knowledge, the "quote" box need only be checked if you need the dynamic value to be in quotes, such as when it is part of a path or file name in syntax
6. Back in the "General" tab, assign an output file name (and path, using browse), save the job, and run it!

If it worked, it should present you with a dialogue box (with whatever text you specified earlier) and present you with a text input box, which it will then use to insert the variable into your table syntax. These production jobs (.spj files) can be executed from the command line or from scripts also (e.g. vbscripts).

I hope this helps!

If things aren't totally clear, you can download the production jobs manual here:

Click HERE to download SPSS Production Jobs Manual

Also, here is another resource about how to use production jobs (straight from IBM):

IBM SPSS Resource

May 17, 2012 | Registered CommenterJeremy Taylor

The usual ways to parameterize a batch of syntax would be as a macro or using Python programmability. The PF parameter substitution mechanism is essentially a friendlier and simpler version of macro but less powerful. Programmability is the most powerful but requires some learning beyond standard syntax.

September 23, 2012 | Unregistered CommenterJK Peck

JK Peck is 100% correct. Using macros and/or ESPECIALLY learning Python does offer a lot more flexibility and power, but is much more complex and requires some familiarity with programming to start.

September 24, 2012 | Registered CommenterJeremy Taylor