Macro with !if command

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

alterego

Macro with !if command

Postby alterego » Wed Jun 27, 2012 12:41 pm

Hello guys,

I have got a seemingly very simple, but in fact for me very hard to solve problem with creating my macro. I want to create a macro with an !if command, and I think I did everything like the description said, but it won't work.

This is my code:

Code: Select all

define !macro(x = !tokens(1))
   !if (var1='4') !then
      compute var2 = 2.
   !ifend
!enddefine.
execute.
!macro x=anyvar.
execute.
It runs through without causing an error message, but doesn't change anything whatsoever. (I know I don't need a macro for this special code, it's just the minimal example of my problem.)

The corresponding code outside the macro works fine:
do if var1='4'.
compute var2=2.
end if.
I just don't get what I'm doing wrong...

Any help appreciated! Thanks a lot.

alterego

PS: Coding macros gets very nasty because the spss syntax editor doesn't show colors anymore. Is there a way to fix this?
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: Macro with !if command

Postby GerineL » Wed Jun 27, 2012 2:05 pm

remove execute after enddefine.

I am new to macros, but I think thats it.
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Macro with !if command

Postby Penguin_Knight » Wed Jun 27, 2012 4:23 pm

A few things are not correct:

1) Unlike SAS, the "if" in SPSS does not require "then". You just need to state the consequence:

Code: Select all

define !macro(x = !tokens(1))
   if (var1='4') var2 = 2.
!enddefine.
execute.

!macro x=anyvar.
execute.
2) Second, the if statement can be streamlined to get rid of the compute command. Just like what was shown above. Because the if statement is really part of the executed syntax rather than macro syntax, I don't think it needs an "!" in front of it.

3) You declared a macro variable "x", but you didn't refer to "x" at all in the if statement. Where should the "x" go? Replacing var1 or var2?
alterego

Re: Macro with !if command

Postby alterego » Fri Jun 29, 2012 12:52 pm

Sorry for the late answer! For some reason I wasn't notified by mail though I subscribed the theme…

@Gutnre
Nope, unfortunately thas doesn't change anything. I am still very unsure about how many "execute."s are necessary in my code. I place them quite randomly after every major operation. Seems the first one in my example isn't necessary, though.

@Penguin_Knight
Penguin_Knight wrote:1) Unlike SAS, the "if" in SPSS does not require "then".
This is the original code I took from the command syntax reference:

Code: Select all

DEFINE mymacro(type = !DEFAULT(1) !TOKENS(1))
!IF (!type = 1)!then
frequencies variables=varone.
!ELSE
descriptives variables=vartwo.
!IFEND
!ENDDEFINE.
It definitely says "!if" (not the normal if command, the one with an exclamation mark, note) requires "!then" (p. 620).
I just don't see a difference to my code:

Code: Select all

define !macro(x = !tokens(1))
   !if (var1='4') !then
      compute var2 = 2.
   !ifend
!enddefine.
Penguin_Knight wrote:Second, the if statement can be streamlined to get rid of the compute command.
Alright. Just was an example.
Penguin_Knight wrote:You declared a macro variable "x", but you didn't refer to "x" at all in the if statement. Where should the "x" go? Replacing var1 or var2?
I know. My original code is much longer and more complex. I just eliminated every line that wasn't necessary to produce the error. I tried it with macro variables at first, and that didn't work, so I tried it without, and not even that worked! So I decided to post the easiest code I could find. And that's the one. The "!if" command definitely is the thing that's wrong wether a macro variable is used or not. And I still don't know why.

Funny though: I tried a normal "if" the next day (when I wasn't weary from long coding) myself, and it worked, just like you predicted. Thank you.

Then I tried to replace my "!do" with a "do repeat" – and that didn't work… I am still at a loss why.

So my question is: Why und where do you need these "!do" and "!if" commands? From the command syntax reference I first got the impression normal loops aren't allowed in macros. But they obviously are. So why these new commands?

And I would still be curious to know what's wrong with my original code because I don't seem to get the "!if" command working…

Thank you for your help!

Regards,
alterego

Who is online

Users browsing this forum: No registered users and 2 guests

cron