Indexing macro variables

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

Indexing macro variables

M-24
Hi - I've got two questions:

1) Is there a way to use the macrovars !cat1 to !cat3 in the loop below?

I've tried several combinations of !CONCAT & !EVAL (to get something similar to SAS's &&x&i).

2) I understand there are no 'global' macro variable in SPSS. Any trick around this?

Thanks for any pointers.

-------------

SET MPRINT ON.

DEFINE !test()
!LET !cat1 = 15
!LET !cat2 = 25
!LET !cat3 = 25
!LET !nCat = 3

!DO !i = 1 !TO !nCat
COMPUTE !CONCAT(N,!cat,!i) = 0.
COMPUTE !CONCAT(N,!CONCAT(!cat,!i)) = 0.
!DOEND

!ENDDEFINE.



Windows Live™: Keep your life in sync. Check it out.
Reply | Threaded
Open this post in threaded view
|

Re: Indexing macro variables

Spousta Jan
Hi M,
 
1) The old-fashioned SPSS way may be:
 

SET MPRINT ON.

 

DEFINE test()

 

!LET !indices = "15 25 35"

!DO !i !in (!indices)

   COMPUTE !CONCAT("N",!i) = 0.

!DOEND

 

execute.

!ENDDEFINE.

test.

 
The new way uses Python:
 

BEGIN PROGRAM PYTHON.

 

import spss

indices = ["15", "25", "35"]

for i in indices:

      spss.Submit("COMPUTE N" + i + " = 0.")

spss.Submit("EXECUTE.")

 

END PROGRAM.

 

2) The traditional trick how to create "global" macro variables in SPSS is this:

 

DEFINE pi() 3.14159 !ENDDEFINE.

 

COMPUTE S = pi * R**2 .

COMPUTE L = A * cos(pi * B) .

exe.

 
As you see, it is clearly less elegant than in SAS - again my advice is to use Python if you have a new version of SPSS.
 
Hope this helps,
 
Jan


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of M
Sent: Friday, February 06, 2009 2:19 AM
To: [hidden email]
Subject: Indexing macro variables

Hi - I've got two questions:

1) Is there a way to use the macrovars !cat1 to !cat3 in the loop below?

I've tried several combinations of !CONCAT & !EVAL (to get something similar to SAS's &&x&i).

2) I understand there are no 'global' macro variable in SPSS. Any trick around this?

Thanks for any pointers.

-------------

SET MPRINT ON.

DEFINE !test()
!LET !cat1 = 15
!LET !cat2 = 25
!LET !cat3 = 25
!LET !nCat = 3

!DO !i = 1 !TO !nCat
COMPUTE !CONCAT(N,!cat,!i) = 0.
COMPUTE !CONCAT(N,!CONCAT(!cat,!i)) = 0.
!DOEND

!ENDDEFINE.



Windows Live™: Keep your life in sync. Check it out.  

_____________

Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

P Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.

 


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

 

P Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --
Reply | Threaded
Open this post in threaded view
|

Re: Indexing macro variables

M-24
This is helpful. Thanks Jan.

Marc.


Subject: RE: Indexing macro variables
Date: Fri, 6 Feb 2009 10:18:46 +0100
From: [hidden email]
To: [hidden email]; [hidden email]

Hi M,
 
1) The old-fashioned SPSS way may be:
 
SET MPRINT ON.
 
DEFINE test()
 
!LET !indices = "15 25 35"
!DO !i !in (!indices)
   COMPUTE !CONCAT("N",!i) = 0.
!DOEND
 
execute.
!ENDDEFINE.
test.
 
The new way uses Python:
 
BEGIN PROGRAM PYTHON.
 
import spss
indices = ["15", "25", "35"]
for i in indices:
      spss.Submit("COMPUTE N" + i + " = 0.")
spss.Submit("EXECUTE.")
 
END PROGRAM.
 
2) The traditional trick how to create "global" macro variables in SPSS is this:
 
DEFINE pi() 3.14159 !ENDDEFINE.
 
COMPUTE S = pi * R**2 .
COMPUTE L = A * cos(pi * B) .
exe.
 
As you see, it is clearly less elegant than in SAS - again my advice is to use Python if you have a new version of SPSS.
 
Hope this helps,
 
Jan


From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of M
Sent: Friday, February 06, 2009 2:19 AM
To: [hidden email]
Subject: Indexing macro variables

Hi - I've got two questions:

1) Is there a way to use the macrovars !cat1 to !cat3 in the loop below?

I've tried several combinations of !CONCAT & !EVAL (to get something similar to SAS's &&x&i).

2) I understand there are no 'global' macro variable in SPSS. Any trick around this?

Thanks for any pointers.

-------------

SET MPRINT ON.

DEFINE !test()
!LET !cat1 = 15
!LET !cat2 = 25
!LET !cat3 = 25
!LET !nCat = 3

!DO !i = 1 !TO !nCat
COMPUTE !CONCAT(N,!cat,!i) = 0.
COMPUTE !CONCAT(N,!CONCAT(!cat,!i)) = 0.
!DOEND

!ENDDEFINE.



Windows LiveT: Keep your life in sync. Check it out.  

_____________

Tato zpráva a všechny připojené soubory jsou důvěrné a určené výlučně adresátovi(-ům). Jestliže nejste oprávněným adresátem, je zakázáno jakékoliv zveřejňování, zprostředkování nebo jiné použití těchto informací. Jestliže jste tento mail dostali neoprávněně, prosím, uvědomte odesilatele a smažte zprávu i přiložené soubory. Odesilatel nezodpovídá za jakékoliv chyby nebo opomenutí způsobené tímto přenosem.

P Jste si jisti, že opravdu potřebujete vytisknout tuto zprávu a/nebo její přílohy? Myslete na přírodu.

 


This message and any attached files are confidential and intended solely for the addressee(s). Any publication, transmission or other use of the information by a person or entity other than the intended addressee is prohibited. If you receive this in error please contact the sender and delete the message as well as all attached documents. The sender does not accept liability for any errors or omissions as a result of the transmission.

 

P Are you sure that you really need a print version of this message and/or its attachments? Think about nature.

-.- --


Windows LiveT: E-mail. Chat. Share. Get more ways to connect. Check it out.
Reply | Threaded
Open this post in threaded view
|

Re: Indexing macro variables

Albert-Jan Roskam
In reply to this post by M-24
Hi!

--Confusingly, !eval (spss) and %eval (sas) have different meanings.
!eval is used to run a macro within a macro.
--How should the COMPUTE statement evaluate? Can you give an example?
Currently it reads as a sophisticated way of defining a number of constants. ;-)
--The Spss macro language is not very powerful (the lack of a 'real' eval function
ia just one of the reasons). It's easier to use Python for this.

begin program.
import spss
def test (cats):
    for k, cat in enumerate(cats):
        spss.Submit("COMPUTE n%s%s = 0." % (str(cat), str(k)))
        # this evaluates as
        # compute n151 = 0
        # compute n252 = 0
        # compute n253 = 0
        # ... which is nonsensical ;-)
test (cats = [15, 25, 25])
end program.

Cheers!!
Albert-Jan


From: M <[hidden email]>
To: [hidden email]
Sent: Friday, February 6, 2009 2:19:01 AM
Subject: Indexing macro variables

Hi - I've got two questions:

1) Is there a way to use the macrovars !cat1 to !cat3 in the loop below?

I've tried several combinations of !CONCAT & !EVAL (to get something similar to SAS's &&x&i).

2) I understand there are no 'global' macro variable in SPSS. Any trick around this?

Thanks for any pointers.

-------------

SET MPRINT ON.

DEFINE !test()
!LET !cat1 = 15
!LET !cat2 = 25
!LET !cat3 = 25
!LET !nCat = 3

!DO !i = 1 !TO !nCat
COMPUTE !CONCAT(N,!cat,!i) = 0.
COMPUTE !CONCAT(N,!CONCAT(!cat,!i)) = 0.
!DOEND

!ENDDEFINE.



Windows Live™: Keep your life in sync. Check it out.