Macro: nested loops

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

cpthindsight
Posts: 1
Joined: Sat Apr 13, 2013 11:53 am

Macro: nested loops

Postby cpthindsight » Sat Apr 13, 2013 12:25 pm

Hi all,

I have to generate al lot of graphs of one variable and thought it would be easier using a macro.

Basically there are 3 parameters to base the graphs on:
1. The variable is observed for 6 years, so the first parameter is the year: <year>
2. The grouping variable on the x-axis: <groupvar>
3. The type of statistic, e.g. MEAN or SUM: <stat>

I want to able to generate all possible combinations of 1-3. I.e., for each grouping variable I want combinations of years and stats. Therefore, I thought nested loops would do the trick:

Code: Select all

SET MPRINT=no. 
define graphs (year=!cmdend /groupvar=!cmdend /stat=!cmdend)

!DO !gr !IN (!groupvar)
!DO !yr !IN (!year)
!DO !st !IN (!stat)

GRAPH
  /BAR(SIMPLE)=!st(!concat('varname_', !yr)) BY !gr.

!DOEND 
!DOEND 
!DOEND .
!enddefine .

* Call the macro .
SET MPRINT=yes.
graphs year=2006 2007 2008 2009 groupvar=groupvar1 groupvar2 stat=SUM MEAN.
However, this code does not generate graphs. It doesn't generate errors either. It returns this:

Code: Select all

SET MPRINT=no.
  60  0 M>  SET MPRINT=no.
define graphs (year=!cmdend /groupvar=!cmdend /stat=!cmdend)
!DO !gr !IN (!groupvar)
!DO !yr !IN (!year)
!DO !st !IN (!stat)
GRAPH
  /BAR(SIMPLE)=!st(!concat('varname_', !yr)) BY !gr.
!DOEND
!DOEND
!DOEND .
!enddefine .
* Call the macro .
SET MPRINT=yes.
graphs year=2006 2007 2008 2009 groupvar=groupvar1 groupvar2 stat=SUM MEAN.
  78  0 M>  
  79  0 M>  .
  80  0 M>  
  81  0 M>  .
Could someone please tell me what's wrong with the syntax?

Thanks a bunch!

EDIT: moved post to correct forum
JonPedersen
Posts: 117
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Macro: nested loops

Postby JonPedersen » Wed May 01, 2013 9:13 am

Hi,
The problem is in your macro definition line, I believe. You cannot use cmdend, because that tells spss to collect tokens to the end of the command. That will make the two next variables be empty, therefore no output and no error messages.
Here is what the help file says about cmdend
!CMDEND Assign to the argument all of the remaining text on the macro call, up to the start of the next command. !CMDEND is useful for changing the defaults on an existing command. Since !CMDEND reads up to the next command, only the last argument on the argument list can be specified with !CMDEND. If !CMDEND is not the final argument, the arguments following !CMDEND are read as text.
For cases such as this use !enclose, for example

Code: Select all

define graphtodeath (year=!enclose("(",")")  /groupvar=!enclose("(",")") /stat=!enclose("(",")")
hth

Who is online

Users browsing this forum: No registered users and 1 guest

cron