What is wrong with this macro?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

What is wrong with this macro?

Sundance Kid
The recode syntax looks as follows:

RECODE unit4 (CONVERT) ('C4'=5) INTO UNIT50.
EXECUTE.

So variable unit4 is being recoded to a different variable called UNIT50.

I want to change the text after convert dynamically. So I tried to use the following macro:

DEFINE TEST (Arg1 = !Tokens(1))
 RECODE unit4 (CONVERT) !Arg1 INTO UNIT50.
EXECUTE.
!ENDDEFINE.


TEST Arg1 = ('C4'=5).


The idea is when I call TEST it should update the code after convert. But whenever I try to run this I get the error message:

>Error # 4652 in column 26.  Text: INTO
>Unrecognized text appears on the RECODE command within the value
>specifications.
>Execution of this command stops.

>Error # 3220 in column 1.  Text: C4
>Unrecognized text appears on the EXECUTE command.  This command allows no
>subcommands.
>Execution of this command stops.



It seems to be reading the C4 value but for some reason it won't work. Please help.
Reply | Threaded
Open this post in threaded view
|

Re: What is wrong with this macro?

Andy W
Macros have special rules for how the parameters are interpreted/tokenized. When you say you only want one "token" passed to Arg1, it splits |('C4'=5)| into several tokens, with only the left parenthesis being the first token and "C4'=5)" being the second token. Same thing happens if you try to pass in negative values.

A workaround for this is to change how the parameters are passed. Two solutions are to use !CMDEND, (since there are no other tokens) or !ENCLOSE. (Another option would be to pass the parameters as quoted strings and use !UNQUOTE within the macro.)

DEFINE TEST2 (Arg1 = !CMDEND)
 RECODE unit4 (CONVERT) !Arg1 INTO UNIT50.
EXECUTE.
!ENDDEFINE.

Or use Enclose (note how now !Arg1 is inside parenthesis),

DEFINE TEST3 (Arg1 = !ENCLOSE("(",")"))
 RECODE unit4 (CONVERT) (!Arg1) INTO UNIT50.
EXECUTE.
!ENDDEFINE.

Always remember when you are having problems with macros to use "SET MPRINT ON." and try to debug the macro yourself.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/