SPSS Statistics

 View Only

 how to pass parameters to a macro in inserted syntax

Jump to  Best Answer
Christoph Kulicke's profile image
Christoph Kulicke posted Fri January 23, 2026 07:10 AM

Hello everyone!

I have the following scenario:

I use a master syntax to call different sub-syntaxes producing outputs for different files.
Currently, I need to go into each subsyntax and comment out/in different sections depending on the context.
Ideally, for those sections I would define a macro with a conditional statement. However, I don't know how to pass the parameter on which the decision is made to the macro call, which is in the subsyntax, not the master syntax.

The goal is, that only the master syntax must be opened.  

Is it at all possible to pass a parameter to macro defined in another syntax?
Is there an option to define a macro variable thats global or can be accessed in the same session after the macro ran?
Is there maybe a different possibility to induce a state from the mastersyntax that an !IF statement in the inserted syntax can evaluate?

Thanks a lot in advance!
Christoph  

Christoph Kulicke's profile image
Christoph Kulicke  Best Answer

SOLUTION:

I found a solution that works by defining my decision value as a macro in the Mastersyntax and calling that in the IF clause in the subsyntax.

The trick is to use the macro expansion directly as a string to compare to instead of a return value.
This is the scheme I used:

Master Syntax:

DEFINE TestValue ()
OptionA
!ENDDEFINE.
insert file = "Test_Subsyntax.sps".


Inserted Syntax:

DEFINE Decision ()
!IF (!EVAL(TestValue) = OptionA) !THEN
    freq var1.
!ELSE
    freq var2.
!IFEND
!ENDDEFINE.

Decision.

 Importantly, it did not work without !EVAL()

Jon Peck's profile image
Jon Peck IBM Champion

Macros can have  parameters, so  you could use DEFINE to create the macro and then invoke it with parameters regardless of whether it is in the same file or a different one that  you have INSRTed.  And macros can refer to values of other macros, so you can set the value(s) in the main syntax and refer to them in other macros.  You may need to use the !EVAL function to get those values.  See the DEFINE command in the Command Syntax Reference for details.  An alternative would be to use Python programmability for all this as it is more flexible and more powerful, but tha requires some Python knowledge.  The Programming and Data Management book available via Help > Doc in PDF Format coiuld help with that.

Christoph Kulicke's profile image
Christoph Kulicke

I am not sure, if I explained this correctly. Maybe I just misunderstood your answer.

Anyways, I tried to implement your suggestion but failed.

I used this as my mastersyntax:

DEFINE defineTest(t = !TOKENS(1))
    !LET !test = !t.
    !IF (!t = OptionA) !THEN
        desc var1.
    !ELSE
        desc var2.
    !IFEND
!ENDDEFINE.

defineTest t=OptionA.
insert file = "Test_Subsyntax.sps".

and this as my subsyntax:

DEFINE Decision_1 ()
    !IF (!test = OptionA) !THEN
        freq var1.
    !ELSE
        freq var2.
    !IFEND
!ENDDEFINE.

DEFINE Decision_1a ()
    !IF (!EVAL(!test) = OptionA) !THEN
        freq var1.
    !ELSE
        freq var2.
    !IFEND
!ENDDEFINE.

DEFINE Decision_2 ()
    !IF (!t = OptionA) !THEN
        freq var1.
    !ELSE
        freq var2.
    !IFEND
!ENDDEFINE.

DEFINE Decision_2a ()
    !IF (!EVAL(!t) = OptionA) !THEN
        freq var1.
    !ELSE
        freq var2.
    !IFEND
!ENDDEFINE.

Decision_1.
Decision_1a.
Decision_2.
Decision_2a.

All of these four macros used the else path.

My goal is to set a variable in the mastersyntax that can be used for !IF statements in the subsyntax.

Jon Peck's profile image
Jon Peck IBM Champion

You did not say what went wrong or what is in the inserted file, but I unscrambled the macro definitions in the email version I received and got these to work.

Suggestions:

Run 

set mprint on.

at the top.  It will then show you the generated code for both the definitions and the invocations so can see exactly what it is doing..

display macro.

will show you what macros are defined at any point.

Macros defined in the main syntax should be usable in INSERTed files, but remove the insert and trying running the code (after defining a few variables) all in the main syntax file to simplify the debugging..

Christoph Kulicke's profile image
Christoph Kulicke

I'll try to reframe this, sorry.

I want to achieve a situation, where I can define a value in the Mastersyntax and depending on this value, the inserted syntax runs different lines of code.
The goal is to set this variable once in the Mastersyntax, so I don't have to open each inserted syntax and manually adjust the code there.

I will sketch this out in pseudo code, maybe this will help clear up my issue:

Mastersyntax:

define Test as a macro with return value or variable // change the value manually, if needed

insert otherSyntaxA.sps
insert otherSyntaxB.sps
insert otherSyntaxC.sps

-------

Inserted Syntax (A, B and C):

// some code that stays the same

if (Test = OptionA) then // run different code depending on the value of Test defined in the mastersyntax
    codeA
else
    codeB

// some code that stays the same

if (Test = OptionA) then // run different code depending on the value of Test defined in the mastersyntax
    codeA
else
    codeB

// some code that stays the same

The issue I was having with the example code I gave in my last post, was that die Decision macros in the inserted syntax did not use the value of the variables !test or !t, defined when executing the Test macro in the mastersyntax. Even though I assigned the value OptionA to !t in the Mastersyntax, !IF (!t = OptionA) evaluated to false and the else clause was executed. The same for the three other configurations.

Therefore, I am wondering how I can access the value of a macro variable from the Mastersyntax in a macro situated inside the inserted syntax.

Addressing your solutions:

- Call the macro from the Mastersyntax:
The problem with calling the Decision macro in the master syntax is, that in my real case, there are several macro calls in the inserted syntax in between other lines of code. So, I would need to break the inserted syntax into multiple parts at each macro call and insert those all separately, which is not feasible.

- Call a macro in the Mastersyntax from the inserted syntax:
To use the result of this macro in the evaluation of the IF-clause, I would need a return value from the macro, right? I am not aware, how this would be possible. From my understanding Macros just run lines of code based on parameters and do not return anything like function.