macro with references

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

macro with references

Marks, Jim
I am trying to run a macro with the following structure:
 
define !prep2 (vnam = !CHAREND ( '/' ) ).
!DO !var2 !IN (!vnam).
 
add file /file =!QUOTE(!CONCAT(
   "C:\grp_",!var2,"_200706-01.sav"))
   /file =!QUOTE(!CONCAT(
   "C:\grp_",!var2,"_200706-02.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-03.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-04.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-05.sav"))
.
exe.
!DOEND.
!ENDDEFINE.
 
!prep2 vnam = 235_1 /.
 
When I call the macro I want !var2 to be replaced by 235_ . Instead the
macro makes 3 calls--
    first inserting         235         in place of !var2
    second inserting     _           in place of !var2
    third inserting         1           in place of !var2
 
The files all exist-- grp_235_1_200706-01.sav (and so on).
 
Any ideas on the macro expansion does this? (I plan to edit the macro to
call the right files, but I'd like to understand the macro expansion
logic)
 

Jim Marks

Senior Market Analyst

x 1616

 
Reply | Threaded
Open this post in threaded view
|

AW: macro with references

Georg Maubach
Hi Jim,

It is very simple and basically nothing to do with the macro itself. The reason for this behaviour is that the parser of the syntax program (this what happens among other things if you click "run") divides the text into tokens. Tokens are text blocks such as "DISPLAY DICTIONARY" but also _ and - are tokens by themselves. The macro text processing concentrates on tokens. Such the "string" 235_1 is actually three tokens "235", "_" and "1".

You can force SPSS to read the string "235_1" as a string if you quote it like

!prep2 vnam = "235_1" .
* BTW: you can omit the trailing slash here because the commands ends anyway .

I am not really sure if you could use the quoted string right way. If your string expands to

C:\grp_"235_1"_200706-01.sav

then put in an unquote command like

!QUOTE(!CONCAT("c:\grp_",!UNQUOTE(!var2),"_200706-01.sav"))

You can keep track of that if you switch the SPSS setting so that SPSS shows you the execution of the macro in the output by using

PRESERVE .
SET MPRINT=ON .
!prep2 vnam = "235_1" .
RESTORE .

HTH

Best regards

Georg Maubach
Research Manager



-----Ursprüngliche Nachricht-----
Von: SPSSX(r) Discussion [mailto:[hidden email]] Im Auftrag von Marks, Jim
Gesendet: Mittwoch, 15. August 2007 19:38
An: [hidden email]
Betreff: macro with references

I am trying to run a macro with the following structure:

define !prep2 (vnam = !CHAREND ( '/' ) ).
!DO !var2 !IN (!vnam).

add file /file =!QUOTE(!CONCAT(
   "C:\grp_",!var2,"_200706-01.sav"))
   /file =!QUOTE(!CONCAT(
   "C:\grp_",!var2,"_200706-02.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-03.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-04.sav"))
   /file =!QUOTE(!CONCAT(
    "C:\grp_",!var2,"_200706-05.sav"))
.
exe.
!DOEND.
!ENDDEFINE.

!prep2 vnam = 235_1 /.

When I call the macro I want !var2 to be replaced by 235_ . Instead the macro makes 3 calls--
    first inserting         235         in place of !var2
    second inserting     _           in place of !var2
    third inserting         1           in place of !var2

The files all exist-- grp_235_1_200706-01.sav (and so on).

Any ideas on the macro expansion does this? (I plan to edit the macro to call the right files, but I'd like to understand the macro expansion
logic)


Jim Marks

Senior Market Analyst

x 1616
Reply | Threaded
Open this post in threaded view
|

Re: macro with references

Richard Ristow
In reply to this post by Marks, Jim
At 01:38 PM 8/15/2007, Marks, Jim wrote:

>I am trying to run a macro with the following structure:
>
>define !prep2 (vnam = !CHAREND ( '/' ) ).
>!DO !var2 !IN (!vnam).
>
>add file /file =!QUOTE(!CONCAT(
>    "C:\grp_",!var2,"_200706-01.sav"))
>    /file =!QUOTE(!CONCAT(
>    "C:\grp_",!var2,"_200706-02.sav"))
...


>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-03.sav"))
>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-04.sav"))
>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-05.sav"))
>.
>exe.
>!DOEND.
>!ENDDEFINE.
>
>!prep2 vnam = 235_1 /.
>
>When I call the macro I want !var2 to be replaced by 235_ . Instead
>the
>macro makes 3 calls--
>     first inserting         235         in place of !var2
>     second inserting     _           in place of !var2
>     third inserting         1           in place of !var2

See George Maubach's reply.  But your real problem is the
     !DO !var2 !IN (!vnam).

!DO ... !IN is for looping through a set of code, inserting a different
value (from a list of values) each pass through.

You don't have a loop. You have a single pass through your code, in
which !var2 is referred to a number of times.

Try
a. Remove the !DO macro construct, and the !DOEND
b. In the code, replace all occurrences of !var2 by !vnam

-Best of luck,
  Richard
Reply | Threaded
Open this post in threaded view
|

Re: macro with references

Marks, Jim
Thanks, Georg and Richard.

I understand that the strings are interpreted as tokens (not sure why
"_" is always interpreted as a separate token in macro expansion, even
when it is "embedded" with other characters...)

This was a simplified version of the macro-- I will be looping through
two sets of lists-- that's why I have !IN and !CHAREND in the macro.

--jim


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Richard Ristow
Sent: Wednesday, August 15, 2007 5:21 PM
To: [hidden email]
Subject: Re: macro with references

At 01:38 PM 8/15/2007, Marks, Jim wrote:

>I am trying to run a macro with the following structure:
>
>define !prep2 (vnam = !CHAREND ( '/' ) ).
>!DO !var2 !IN (!vnam).
>
>add file /file =!QUOTE(!CONCAT(
>    "C:\grp_",!var2,"_200706-01.sav"))
>    /file =!QUOTE(!CONCAT(
>    "C:\grp_",!var2,"_200706-02.sav"))
...


>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-03.sav"))
>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-04.sav"))
>    /file =!QUOTE(!CONCAT(
>     "C:\grp_",!var2,"_200706-05.sav"))
>.
>exe.
>!DOEND.
>!ENDDEFINE.
>
>!prep2 vnam = 235_1 /.
>
>When I call the macro I want !var2 to be replaced by 235_ . Instead the

>macro makes 3 calls--
>     first inserting         235         in place of !var2
>     second inserting     _           in place of !var2
>     third inserting         1           in place of !var2

See George Maubach's reply.  But your real problem is the
     !DO !var2 !IN (!vnam).

!DO ... !IN is for looping through a set of code, inserting a different
value (from a list of values) each pass through.

You don't have a loop. You have a single pass through your code, in
which !var2 is referred to a number of times.

Try
a. Remove the !DO macro construct, and the !DOEND b. In the code,
replace all occurrences of !var2 by !vnam

-Best of luck,
  Richard