redracer
redracer

Reputation: 53

How do you create several graphs with new variables within a loop in SPSS syntax?

I am using SPSS (v.20) and want to use syntax to create a graph for each of several variables of interest. I have 2 types of graph examples:

EXAMPLE #1:

GGRAPH 
  /GRAPHDATASET NAME="graphdataset" 
    VARIABLES=change_WIraw[LEVEL=ratio] 
    MISSING=LISTWISE REPORTMISSING=NO 
  /GRAPHSPEC SOURCE=VIZTEMPLATE(NAME="Histogram with Normal Distribution"[LOCATION=LOCAL] 
    MAPPING( "x"="change_WIraw"[DATASET="graphdataset"])) 
    VIZSTYLESHEET="Traditional"[LOCATION=LOCAL] 
    LABEL='HISTOGRAM WITH NORMAL DISTRIBUTION: change_WIraw' 
    DEFAULTTEMPLATE=NO.

EXAMPLE #2:

GGRAPH 
  /GRAPHDATASET NAME="graphdataset" VARIABLES=GROUP change_WIraw MISSING=LISTWISE 
REPORTMISSING=NO 
      /GRAPHSPEC SOURCE=INLINE. 
BEGIN GPL 
  SOURCE: s=userSource(id("graphdataset")) 
  DATA: GROUP=col(source(s), name("GROUP"), unit.category()) 
  DATA: change_CTBWss=col(source(s), name("change_WIraw")) 
  DATA: id=col(source(s), name("$CASENUM"), unit.category()) 
  GUIDE: axis(dim(1), label("GROUP")) 
  GUIDE: axis(dim(2), label("change_WIraw")) 
  SCALE: cat(dim(1), include("1", "2", "3", "4")) 
  SCALE: linear(dim(2), include(0)) 
  ELEMENT: schema(position(bin.quantile.letter(GROUP*change_WIraw)), label(id)) 
END GPL.

I want to run a loop so that I can program this to happen for many other variables (interchanging "change_WIraw"). I would love some guidance, thank you! I hope this is the right forum to ask. (I found similar questions, but they were for much simpler functions, like frequency.)

Upvotes: 0

Views: 2155

Answers (2)

RubenGeert
RubenGeert

Reputation: 2952

Your best option -by far- is to use Python for this. Please see www.pythonforspss.org for some very basic basics if you're not using Python for SPSS yet.

Then try this: replace change_WIraw to v10 by the actual variable names (in the second line only). It is presumed they're adjacent in your active DataSet. And then just run:

begin program.
variables='change_WIraw to v10'
import spss,spssaux
vList=spssaux.VariableDict().expand(variables)
for vrbl in vList:
    spss.Submit("""
GGRAPH 
  /GRAPHDATASET NAME="graphdataset" VARIABLES=GROUP %(vrbl)s MISSING=LISTWISE 
REPORTMISSING=NO 
      /GRAPHSPEC SOURCE=INLINE. 
BEGIN GPL 
  SOURCE: s=userSource(id("graphdataset")) 
  DATA: GROUP=col(source(s), name("GROUP"), unit.category()) 
  DATA: change_CTBWss=col(source(s), name("%(vrbl)s")) 
  DATA: id=col(source(s), name("$CASENUM"), unit.category()) 
  GUIDE: axis(dim(1), label("GROUP")) 
  GUIDE: axis(dim(2), label("%(vrbl)s")) 
  SCALE: cat(dim(1), include("1", "2", "3", "4")) 
  SCALE: linear(dim(2), include(0)) 
  ELEMENT: schema(position(bin.quantile.letter(GROUP*%(vrbl)s)), label(id)) 
END GPL."""%locals())
end program.

The syntax will run once for each variable you specified and replace %(vrbl)s with the variable name.

Upvotes: 2

Andy W
Andy W

Reputation: 5089

A simple way to produce seperate graphs for multiple variables is to reshape your dataset from wide to long (i.e. have all of the variables within a single column) and then use split file before your graph command. Example below.

set seed = 10.
input program.
loop #i = 1 to 1000.
compute V1 = RV.NORM(0,1).
compute V2 = RV.NORM(20,5).
compute V3 = RV.NORM(100,15).
end case.
end loop.
end file.
end input program.
dataset name sim.
execute.

varstocases
/make V from V1 to V3
/index groups.

sort cases by groups.
split file by groups.

* Chart Builder.
GGRAPH
  /GRAPHDATASET NAME="graphdataset" VARIABLES=V MISSING=LISTWISE
  REPORTMISSING=NO
  /GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
 SOURCE: s=userSource(id("graphdataset"))
 DATA: V=col(source(s), name("V"))
 GUIDE: axis(dim(1), label("V"))
 GUIDE: axis(dim(2), label("Frequency"))
 ELEMENT: interval(position(summary.count(bin.rect(V))), shape.interior(
  shape.square))
END GPL.

split file off.

I'm using an older version right now that doesn't have viztemplates (so let me know if this doesn't work for them). Although you can produce any graph in inline GPL statements you can with Viztemplates (except for maps).

You will need to use the macro facility or python if you want more control over the particular labels or aesthetics of charts with looping, but this is a quick way to accomplish what you want.

Upvotes: 0

Related Questions