Macros

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

Macros

Hashmi, Syed S
Hi all,

I was wondering if anybody ideas about a good learning resource for Macros
in SPSS. I tried to get an idea from some online resources but am more
confused now than I was before.

Also, when would it better to use a macro as opposed to a DO REPEAT
command or even a LOOP command. I've used the DO REPEAT quite a bit but
haven't had much experience with the LOOP.

Would appreciate any help anybody can provide. Thanks.

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

Re: Macros

peter link
Shahrukh -

A great resource online (and not just for SPSS Macros) that many of us have
used

http://www.spsstools.net/

A fine book, SPSS Programming and Data Management, can be found here,
also....

http://www.spsstools.net/spss_programming.htm

this can be downloaded in pdf format for free or purchased in hard copy
form.

Peter Link
VA San Diego Healthcare System

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]]On Behalf Of
Shahrukh Hashmi
Sent: Friday, June 22, 2007 2:58 PM
To: [hidden email]
Subject: Macros


Hi all,

I was wondering if anybody ideas about a good learning resource for Macros
in SPSS. I tried to get an idea from some online resources but am more
confused now than I was before.

Also, when would it better to use a macro as opposed to a DO REPEAT
command or even a LOOP command. I've used the DO REPEAT quite a bit but
haven't had much experience with the LOOP.

Would appreciate any help anybody can provide. Thanks.

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

Re: Macros

Richard Ristow
In reply to this post by Hashmi, Syed S
At 05:57 PM 6/22/2007, Shahrukh Hashmi wrote:

>I was wondering if anybody ideas about a good
>learning resource for Macros in SPSS. I tried to
>get an idea from some online resources but am
>more confused now than I was before.

One always recommends:

>Levesque, Raynald, "SPSS® Programming and Data
>Management, 2nd Edition/A Guide for SPSS® and
>SAS® Users". SPSS, Inc., Chicago, IL, 2005.
>You can also download it free as a PDF file,
>from http://www.spss.com/spss/SPSS_programming_data_mgmt.pdf.

The second edition is better on macros than are
the the third or fourth:

>The third edition has a very extensive section
>on programming SPSS with the Python [language]
>add-in. That's valuable if you have SPSS 14 or
>higher (15 will be out soon), but no use if you
>don't. The third edition omits sections on
>scripting (which you probably won't miss) and a
>section on macros (which you might).(*)

But but I don't know if the second edition is still available on-line.

Or, on the other hand, learn Python and skip
macros altogether; Python can do pretty well
everything macros can, often better.

>Also, when would it better to use a macro as
>opposed to a DO REPEAT command or even a LOOP
>command. I've used the DO REPEAT quite a bit but
>haven't had much experience with the LOOP.

Generally, they're for quite different purposes.
DO REPEAT and LOOP run over lists of variables
within a transformation program; macros loop over
commands. See recent postings (and the threads they fall in)

Fri, 22 Jun 2007 <09:26:04 -0700< and
Fri, 22 Jun 2007 <09:43:11 -0700>, "Re: Multiple
Graphs from Matrix Output";
Fri, 22 Jun 2007 <15:53:39 -0400>, "Re: Macro to run multiple models".

.........................
(*)Date: Mon, 4 Sep 2006 15:44:32 -0400
From:    Richard Ristow <[hidden email]>
Subject: Re: syntax
To:      [hidden email]





>--
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.5.472 / Virus Database: 269.9.4/860 -
>Release Date: 6/21/2007 5:53 PM
Reply | Threaded
Open this post in threaded view
|

Re: Macros

Hashmi, Syed S
Thanks Richard... and also to Peter for the other reply.  I'll try and
get the resources that you suggested.

>
> Generally, they're for quite different purposes.
> DO REPEAT and LOOP run over lists of variables
> within a transformation program; macros loop over
> commands. See recent postings (and the threads they fall in)
>

Does this mean that if I want to run transformations over a set of
variables, say var_1 to var_100, it's always better to run a "DO
REPEAT"? Can't the transformation commands be embedded in a macro as
well? Maybe by specifying all commands to be run for var_i where i = 1
to 100?? Or would that mean complicating things more than necessary?

Thanks again.
Reply | Threaded
Open this post in threaded view
|

Re: Macros

Richard Ristow
At 07:20 PM 6/22/2007, Hashmi, Syed S wrote:

>>Generally, [macros, and DO REPEAT and LOOP are] for quite different
>>purposes. DO REPEAT and LOOP run over lists of variables within a
>>transformation program; macros loop over commands.
>
>Does this mean that if I want to run transformations over a set of
>variables, say var_1 to var_100, it's always better to run a "DO
>REPEAT"?

"Always" is a big word, and it's partly a matter of opinion anyway, but
I'd recommend DO REPEAT in almost all cases where it can do the job.
It'll almost always be easier both to write and to read. Efficiency
will be almost exactly the same,

However, I was deliberately simplifying the issues, to lay out the
alternatives in the sharpest terms.

DO REPEAT and LOOP do operate only within a transformation program or
INPUT PROGRAM; that much is absolutely true.

But, you ask

>Can't the transformation commands be embedded in a macro as well?
>Maybe by specifying all commands to be run for var_i where i = 1 to
>100?? Or would that mean complicating things more than necessary?

*If* DO IF will do the job for you, using a macro instead does, yes,
usually complicate things more than necessary.

But you're right: you can write macros that generate transformation
code to be run within a transformation program; and it's sometimes
helpful. (In fact, you can write macros to generate code within a
transformation *statement*, like generating many clauses on a RECODE.)

For an instance, see my posting
Date:     Tue, 12 Jun 2007 15:10:12 -0400
From:     Richard Ristow <[hidden email]>
Subject:  Re: Pass variable as argument to a macro
Comments: To: Julien Mostard <[hidden email]>
To:       [hidden email]

for a macro that generates a list of IF statements. However, that macro
could probably be recast as a DO IF, though losing flexibility in
specifying variable names:

Here's the macro code from that posting:

>DEFINE !compute_2   (max_term=!TOKENS(1)
>                     /var_name=!TOKENS(1)
>                     /IndxVar =!TOKENS(1)
>                     /ValuVar =!TOKENS(1))
>!LET !list=!NULL
>   /*calculate var_nameXXXX variables. */
>    !DO !cnt=1 !TO !max_term
>       IF (!IndxVar = !cnt) !CONCAT(!var_name,!cnt)=!ValuVar.
>    !LET !list=!CONCAT(!list,' ',!CONCAT(!var_name,!cnt))
>!DOEND
>!ENDDEFINE.
>
>* Call the macro compute_2.
>PRESERVE.
>SET MPRINT=yes.
>!compute_2 max_term=4      var_name = IntrstB
>               IndxVar = exist3 ValuVar = exist4.
>   69 M>
>   70 M>  .
>   71 M>  IF ( exist3 = 1 ) IntrstB1 = exist4.
>   72 M>  IF ( exist3 = 2 ) IntrstB2 = exist4.
>   73 M>  IF ( exist3 = 3 ) IntrstB3 = exist4.
>   74 M>  IF ( exist3 = 4 ) IntrstB4 = exist4.
>   75 M>  .
>RESTORE.
>   76 M>  RESTORE.

An implementation using DO REPEAT (not tested) would be

DO REPEAT cnt      = 1        to 4
          /var_name = IntrstB1 TO IntrstB4.
.  IF (exist3 = cnt) var_name = exist4.\
END REPEAT.
END REPEAT.