Re: !do-!doend question

Posted by Kirill Orlov on
URL: http://spssx-discussion.165.s1.nabble.com/do-doend-question-tp5740533p5740551.html

Hi, Guido.
This is the first time I see this sort of error.
And I think it is an SPSS bug worth to submit it to the developers.

[I hope that Jon, David and other gurus might want to consider the case.
Because it is interesting.
The topic is: INSERTing macro calls WRITE/tten within the same macro.]

Below is a an example and a workaround.
I was using your data and the syntax is just a little bit rewritten yours.

DATA LIST LIST/ X Y Z (3 A1).
BEGIN DATA
     F F A
     R B B
END DATA.
save outfile = 'c:\temp\d.sav'.
dataset close all.

define !macro1(pop=!tokens(1))
echo !quote(!pop).
!enddefine.

define !macro_loop(v=!tokens(1))
!do !i = 1 !to 2
-get file = 'c:\temp\d.sav'.
-do if $casenum = 1.
- write outfile = !quote(!conc('c:\temp\foo',!i,'.sps'))
     /'!macro1 pop = ' !quote(!conc(!v,!i)) '.'.
-end if.
-exe.
-insert file = !quote(!conc('c:\temp\foo',!i,'.sps')) .
!doend
!enddefine.

!macro_loop v = Z. /*The macro call
*This macro is like yours main one, only it has 2 loops instead of 1.
*[Also, it WRITEs to different files (foo1.sps and foo2.sps) on
different macro loops,
*but that makes no difference: one could write into one same file
foo.sps, like your macro did it.]
*And on the second loop it returns the same ERROR like yours did.
*The macro generates (set printback=on) the syntax below which, when pasted
*and run manually from the syntax window, runs smoothly: .
get file = 'c:\temp\d.sav'.
do if $casenum = 1.
-write outfile = 'c:\temp\foo1.sps' /'!macro1 pop = ' 'Z1' '.'.
end if.
exe.
insert file = 'c:\temp\foo1.sps'.
get file = 'c:\temp\d.sav'.
do if $casenum = 1.
-write outfile = 'c:\temp\foo2.sps' /'!macro1 pop = ' 'Z2' '.'.
end if.
exe.
insert file = 'c:\temp\foo2.sps'.

*So, The syntax generated (expanded) is all right, and the error occurs
when "the macro runs" the syntax it has expanded.
*The error is associated with the second INSERT command (insert file =
'c:\temp\foo2.sps',
*containing syntax: !macro1 pop = Z2.). It is not parsed properly.
*So the LAST macro loop with its corresponding INSERT is problematic in
this respect.

*If you insist on WRITEing out macro calls and INSERTing them on the
same macro loops,
*I might recommed you the following simple trick. Just request one more
macro loop than needed, and do not
*use INSERT on that last macro loop: .
define !macro_loop(v=!tokens(1))
!do !i = 1 !to 3
-get file = 'c:\temp\d.sav'.
-do if $casenum = 1.
- write outfile = !quote(!conc('c:\temp\foo',!i,'.sps'))
     /'!macro1 pop = ' !quote(!conc(!v,!i)) '.'.
-end if.
-exe.
!IF (!i<>3) !THEN
-insert file = !quote(!conc('c:\temp\foo',!i,'.sps')) .
!IFEND
!doend
!enddefine.

!macro_loop v = Z. /*The macro call: No error now.



20.05.2021 9:16, Guido Schiffhorst пишет:
>
> define macro1 (pop !tokens(1))
>
> echo !quote(!pop).
>
> !enddefine.
>
> define macro_loop (v !tokens(1))
>
>     !do !i = 1 !to 1
>
>         get file = 'c:\temp\d.sav'.
>
>         do if $casenum = 1.
>
>             write outfile = 'c:\temp\foo.sps'
>
>             /'macro1 pop =' !quote(!v) '.'.
>
>         end if.
>
>         exe.
>
>         insert file = 'c:\temp\foo.sps'.
>
>     !doend
>
> !enddefine.
>
> define macro_wo_loop (v !tokens(1))
>
>     get file = 'c:\temp\d.sav'.
>
>     do if $casenum = 1.
>
>         write outfile = 'c:\temp\foo.sps'
>
>         /'macro1 pop =' !quote(!v) '.'.
>
>     end if.
>
>     exe.
>
>     insert file = 'c:\temp\foo.sps'.
>
> !enddefine.
>
> macro_wo_loop v = Z.
>
> macro_loop v = Z.
>


=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD