How to return a value from a macro

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

mwulfe
Posts: 7
Joined: Mon Feb 13, 2012 3:24 pm

How to return a value from a macro

Postby mwulfe » Mon Feb 13, 2012 3:41 pm

I have written SPSS 19 code with several macros in one syntax file. Each of these macros has at least 2 input tokens. So far this is an example of how I do it:

Code: Select all

DEFINE CMCode(  cMonth = !TOKEN(1)  
                          /cYear= !TOKEN(1) )
COMPUTE #cmc = ((!cYear - 1900) * 12) + !cmonth.
IF (!cYear < 1900 OR !cYear > 2100 OR !cMonth < 1 OR !cMonth > 12) #cmc = $SYSMIS.
IF missing( !cMonth ) #cmc= $SYSMIS.
IF missing( !cYear ) #cmc= $SYSMIS.
!ENDDEFINE.

CMCode cMonth = V123 cYear = V456.
COMPUTE V789 = #cmc.
Is there a more direct way of doing this? I'm thinking of some way to return a value from the macro.

Thanks,
Martin
JonPedersen
Posts: 117
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: How to return a value from a macro

Postby JonPedersen » Tue Feb 14, 2012 3:48 am

Hi,
I think the answer is "not really". You can include the return variable as a parameter in the Macro definition, and have the compute statement inside the macro. I.e:

Code: Select all

DEFINE CMCode(  cMonth = !TOKEN(1)  
                          /cYear= !TOKEN(1) 
                         /result=!TOKENS(1))
COMPUTE #cmc = ((!cYear - 1900) * 12) + !cmonth.
IF (!cYear < 1900 OR !cYear > 2100 OR !cMonth < 1 OR !cMonth > 12) #cmc = $SYSMIS.
IF missing( !cMonth ) #cmc= $SYSMIS.
IF missing( !cYear ) #cmc= $SYSMIS.
compute Result=#cmc.
!ENDDEFINE.

CMCode cMonth = V123 cYear = V456 Result=V789.
But there is not a way, I think, to really define user defined functions in SPSS that would look like an ordinary function. I think that a Python solution would be similar in concept.
jon
mwulfe
Posts: 7
Joined: Mon Feb 13, 2012 3:24 pm

Re: How to return a value from a macro

Postby mwulfe » Tue Feb 14, 2012 1:50 pm

Jon -

Thanks for your response! If I understand your code correctly, it essentially passes the parameters by reference so any change to RESULT would be saved, in this call, in V789. Is that right?

Your code may in fact work well, and it seems to me there is no longer a need for the temporary variable. Then I would just have to either delete the RESULT variable or simply not save it with the output data file.

What I don't fully understand is the need for the "!". Wouldn't it also be needed for !RESULT?

Code: Select all

DEFINE CMCode(  cMonth = !TOKEN(1)  
                          /cYear= !TOKEN(1) 
                         /result=!TOKENS(1))
COMPUTE !result = ((!cYear - 1900) * 12) + !cmonth.
IF (!cYear < 1900 OR !cYear > 2100 OR !cMonth < 1 OR !cMonth > 12) !result = $SYSMIS.
IF missing( !cMonth ) !result = $SYSMIS.
IF missing( !cYear ) !result= $SYSMIS.
!ENDDEFINE.

CMCode cMonth = V123 cYear = V456 Result=V789.
JonPedersen
Posts: 117
Joined: Wed May 25, 2011 7:07 am
Contact:

Re: How to return a value from a macro

Postby JonPedersen » Wed Feb 15, 2012 11:34 am

Hi,
Yes, it is essentially a parameter pass by reference. And yes, you are right, I forgot the ! in front of Result. (this is actually one of the more common macro programming errors). All parameters passed to the macro need the ! as do macro keywords.)
best,
jon

Who is online

Users browsing this forum: No registered users and 1 guest

cron