Date: Tue, 13 Jun 2006 13:22:00 -0600
Reply-To: David Wasserman <david.wasserman@shaw.ca>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: David Wasserman <david.wasserman@shaw.ca>
Subject: Re: Can't !LET & VAR LABELS be used in DO IF...ELSE IF... END IF
structures in MACROs? revisited
Content-type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original
As I understand it, macros have three stages: definition, obviously, between
DEFINE and !ENDDEFINE statements, expansion, and execution. Both expansion
and execution take place when the macro is called, but are clearly a
two-step process. When the macro you have presented expands, everything
that is not a macro command, keyword, or argument is still just a string.
During the expansion, the macro commands, keywords and arguments are
resolved, and essentially disappear, replaced by the strings to which they
resolve. The expansion happens only once, and while it is happening there
are no variable values. Therefore, a DO IF structure, which depends on the
existence of variable values, which change as often as there are cases,
cannot control the execution of a !LET command. All the !LET commands
expand before the resulting strings are treated as SPSS syntax. The end
result is that the last !LET command used to assign a value to a macro
keyword is the !LET command that gives that keyword the string content it
has when the macro expanded output executes. Your example shows that: only
the last !LET command appears to work.
I hope that helps.
David Wasserman
Custom Data Analysis and SPSS Programming
----- Original Message -----
From: "Marta García-Granero" <biostatistics@terra.es>
Newsgroups: bit.listserv.spssx-l
To: <SPSSX-L@LISTSERV.UGA.EDU>
Sent: Tuesday, June 13, 2006 12:45 PM
Subject: Can't !LET & VAR LABELS be used in DO IF...ELSE IF... END IF
structures in MACROs? revisited
> Hi everybody:
>
> Since my first post presented only part of a very long MACRO, perhaps
> the problem can't be easily reproduced by other people. That's why I
> have written a tiny demo that shows the problem:
>
> DATA LIST FREE/testvar.
> BEGIN DATA
> 1 1 1 2 2 2 3 3 3
> END DATA.
> FORMAT testvar (F8).
>
> DEFINE DEMOTEST(method=!TOKENS(1)).
> DO IF !method EQ 1.
> - VAR LABEL testvar 'Method #1 was selected'.
> - !LET !label_1=!UNQUOTE('1-A').
> - !LET !label_2=!UNQUOTE('1-B').
> - !LET !label_3=!UNQUOTE('1-C').
> ELSE IF !method EQ 2.
> - VAR LABEL testvar 'Method #2 was selected'.
> - !LET !label_1=!UNQUOTE('2-A').
> - !LET !label_2=!UNQUOTE('2-B').
> - !LET !label_3=!UNQUOTE('2-C').
> ELSE.
> - VAR LABEL testvar 'Other method was selected'.
> - !LET !label_1=!UNQUOTE('Other-A').
> - !LET !label_2=!UNQUOTE('Other-B').
> - !LET !label_3=!UNQUOTE('Other-C').
> END IF.
> DO IF $casenum EQ 1.
> - ECHO "The method used was:".
> - ECHO !QUOTE(!method).
> END IF.
> VALUE LABEL testvar 1 !QUOTE(!QUOTE(!label_1))
> 2 !QUOTE(!QUOTE(!label_2))
> 3 !QUOTE(!QUOTE(!label_3)).
> FREQUENCIES
> VARIABLES=testvar.
> !ENDDEFINE.
>
> DEMOTEST method=1.
> DEMOTEST method=2.
>
> As you can see if you run this example, the last instance of the list
> of DO IF... ELSE IF... ELSE is always selected.
>
> Can anybody enlighten me?
>
> Thanks,
> Marta mailto:biostatistics@terra.es
>
> (Anyway: my Meta-analysis MACRO is written and working properly -
> using a long workaround to the above mentioned problem - and available
> on request).
>
|