Macro to specify value of a variable throughout syntax

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

0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

Macro to specify value of a variable throughout syntax

Postby 0101 » Wed Dec 07, 2011 4:17 pm

I often apply the same syntax to a dataset, but occasionally need to change the value (referenced in the syntax) of one or two variables depending on what cases I want to include in my analysis.

For example, today I may want to include cases where FiscalYear > 2005 AND Status = 1.

But tomorrow I may want to include cases where FiscalYear > 2000 AND Status = 0.

I've been doing a "find and replace" every time, but I'd like to use a dynamic variable (a macro?) instead. I would define the value or values of the variables up front, and the values I define would be automatically used throughout the syntax where specified. I think a macro is what I need, but I'm a bit new to those so I'm may be off.

A typical line in the syntax might look like this:

Code: Select all

COMPUTE FilterVariable = FiscalYear > 2005 AND Status = 1.
... which I assume I'd change to something like:

Code: Select all

COMPUTE FilterVariable = FiscalYear > !FiscalYearValue AND Status = !StatusValue.
And use some sort of define !... statement up front to specify the values?

Any suggestions on how to do this?

Thank you.
JonPedersen
Posts: 118
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: Macro to specify value of a variable throughout syntax

Postby JonPedersen » Wed Dec 07, 2011 9:18 pm

Hi,
with macros, two possibilities come to mind. The first is to use two macros to define the values:

Code: Select all

define StatusValue()
  2
!enddefine.

define FiscalYearValue()
  2005
!enddefine.

*commands as needed.....
compute filtervar= Status=StatusValue and FiscalYear=FiscalYearValue.
filter by filtervar.
* commands as needed...
The other is to define your whole program as a macro

Code: Select all

define RunMyStuff (SV=!TOKENS(1) /FYV=!TOKENS(1))
* fill inn beginning of commands.
compute filtervar=Status=!SV and FiscalYear=!FYV.
filter by filtervar.
* rest of your commands.
!enddefine.
*.
* The run the macro.
RunMystuff SV=1 FYV=2007.
What is best depends on your purpose. The main benefit of the first version is that you get the definition at the top of your syntax file.
The main benefit of the second is that it is very convenient to run several variants after each other.

This can of course also be done with the Python extension, but that is probably overkill.
hth
Jon
0101
Posts: 52
Joined: Fri Mar 04, 2011 9:42 pm

Re: Macro to specify value of a variable throughout syntax

Postby 0101 » Thu Dec 15, 2011 7:49 pm

Thank you! Both work very well.

Who is online

Users browsing this forum: No registered users and 2 guests

cron