Macro Tutorial
Home Up Macro Tutorial Debugging Macros

SPSS Programming and Data Management

Enter keywords


Exact match


Advanced Search
Feedback
Site Map

 

Other languages
Deutsch
Español
Français
Italiano
Português

 

Do you love this site?

 

SPSS Macro tutorials

 

  1. The Basics
  2. Macros and the command terminator
  3. Just (!) a string parser
  4. Fully commented example of a macro (obtain n independent samples of size m)
  5. Replacing a character in many string variables
  6. Modify a macro to handle many variables
    Version 1: to change the format (the name remains the same) of a variable from string to string, numeric to string or string to numeric
    Version 2: same as above but doing 5-10 variables in the same macro call.
    Version 3: same as above but doing it for ALL numeric or string variables between 2 given variables in the data file.
  7. Doing arithmetic with macro variables (for advanced users)
  8. Another fully commented macro (print bar charts excluding categories with less than n cases)

Let me know if you have suggestions for subjects to be covered here or if a given area is not clear.

 

The Basics

Note that you have to be familiar with syntax in order to be in position to understand macros.

A macro is similar to a function. First, you define it, then  you have to 'call it'. The macro needs to be defined only once per SPSS "session".
A session begins when you start SPSS and ends when you shut it down. Thus if you start SPSS, define a macro then work with many different data files, the macro still exists and can be called at any time without having to be redefined. However, if you shut down SPSS and start it again, you have to redefine the macro (that is, execute the code between the DEFINE and !ENDDEFINE commands) before being able to call it.

Macro definitions may be placed anywhere in the syntax file because they get "preferential treatment" in the sense that they are processed (that is they are  "defined") before the rest of the syntax file is processed.

Here is a very simple example of a macro:
DEFINE !stats (myvars=!CMDEND)
FREQUENCY VARIABLES= !myvars /ORDER= ANALYSIS.
!ENDDEFINE.

You would call the macro using a line such as

!stats myvars=age sex status.

assuming age sex and status are the variables for which you want the Frequency information.

The DEFINE statement (the first line of the macro definition) tells you that the name of the macro is !stats, the name cannot exceed 8 characters.
That statement also tells you that when you call the macro, you have to assign the variables of interest to myvars. This has been done in the macro call where we have "myvars=age sex status".

It is important to note that although the macro variable is named "myvars" in the DEFINE line, it must be referred to as "!myvars" within the macro.

Once the macro is defined, the line
!stats myvars=age sex status.
has the same effect as the line
FREQUENCY VARIABLES= age sex status /ORDER= ANALYSIS.

You might be thinking "What's the big deal?". The big deal is that macros can do much more complex tasks that a simple Frequency; they could be doing things which would otherwise require hundreds of lines of syntax and hours of work. With a macro you need only one line to call and execute all those lines. There are over 140 macros in this site. The best way to find all of them is to search for "!ENDDEFINE" using the Search tool which is on the left side of the screen of each page of this site.

 

Macros and the Command Terminator

The "Command Terminator" is the period which must end each syntax command in interactive mode. (The periods are not needed when the syntax is run using the INCLUDE statement or the Production Facility. My recommendation is to always end syntax commands with the period).

However the sequence beginning with "DEFINE" and ending with "!ENDDEFINE." is just one syntax command. It properly ends with a period. For this reason it is not necessary to insert a period at the end of each macro command. In some cases, inserting a period after a macro command will create a bug! (for an example see this macro) Doing so could confuse the processor. Therefore the general rule is: do not terminate macro commands with a period. For instance,  !DO !var=1 !TO !nbfiles does not need a command terminator.

Of course macros often contain syntax command, for instance the following line needs to end with a period

SAVE OUTFILE=!QUOTE(!CONCAT("c:\Temp\new file ",!var,".sav")).

because it is a syntax command (which happens to be inside a macro) and syntax commands needs to end with a period.

Just (!) A String Parser

The fundamental thing to understand with respect to Macros is that macro language is just a "string parser". By this I mean that macro's magic is entirely performed before the first case has been processed by a given macro.

Once you understand this, it is easy to see that a command such as

!IF (!age) !GT age !THEN ... !ENDIF

does not make sense. It does not make sense because age is a variable whose value (normally) varies by case, since the macro is "expanded" before the first case is read, the macro parser does not have a clue whether the !IF --- !END IF condition is satisfied or not.

On the other hand the following macro line is acceptable

COMPUTE years2=65 - !age.

since the macro expansion can replace !age by the value given in the macro call or defined within the macro by a !LET statement. The replacement is not conditional on information coming from individual data cases.

Fully Commented Example of a Macro

I list a macro then describe the purpose of each line of code. (Note: the line numbers are only to help me comment on the macro, macros never have line numbers)

* How can I obtain n independent random samples of size m from the same source file?

* Answer posted to SPSSX-L by rlevesque@videotron.ca on 2001/04/14.
1   PRESERVE.
2   SET MPRINT=no.
3   *///////////////////////////////.
4   DEFINE !getsamp (nbcases=!TOKENS(1) /nbfiles=!TOKENS(1) )

5   !DO !var=1 !TO !nbfiles
6   GET FILE='C:\Program Files\SPSS\GSS93 subset.sav'.
7   COMPUTE draw=UNIFORM(1).
8   COMPUTE dummy=1.
9   RANK VARIABLES=draw BY dummy /RANK INTO rank1.
10 SELECT IF rank1 LE !nbcases.
11 SAVE OUTFILE=!QUOTE(!CONCAT("c:\Temp\new file ",!var,".sav")).
12 !DOEND
13 !ENDDEFINE.
14 *///////////////////////////////.

15 *Next line starts the whole process.
16 SET MPRINT=yes.
17 !getsamp nbcases=300 nbfiles=4.
18 RESTORE.

The macro definition starts at line 4 and ends at line 13. The macro is called at line 16. Note that the line numbers are included only to facilitate the comments. If you want to run this syntax, the line numbers must be removed and it is recommended that all commands start in the first column, sub-commands on the other hand should start in the second or higher column. Doing so ensures that the macro can be INCLUDED in an other syntax file using the INCLUDE command.

Comments on each line
  1. This command works in conjunction with the command RESTORE in line 17.
    From the Syntax Guide we have:
    PRESERVE stores current SET specifications that can later be restored by the RESTORE command. PRESERVE and RESTORE are especially useful with the macro facility.
    PRESERVE—RESTORE sequences can be nested up to five levels.
  2. I use the SET MPRINT command all the time when I write or debug a macro. For greater readability of the Output Window, it is preferable to set it to no before the macro definition starts and to set it to yes (see line 16) before calling the macro.
  3. I like to enclose my macros between special characters which I use just for this purpose. See line 3 and 14. This allows me to locate macros faster in long syntax files.
  4. The beginning of the macro. The name of the macro is !getsamp; I like to start macro names with ! this helps to quickly identify macro names in ordinary syntax. The macro requires 2 arguments when called: nbcases and nbfiles. Thus if nbcases=500 and nbfiles=40, the macro will generate 40 independent random files of 500 cases each. Each sample will be saved in its own file
  5. This line works in conjunction with line 12. The macro variable !var will take all values from 1 to the value of !nbfiles which was given in the macro call. Warning: Note that the name of the arguments in line 4 do not start with an "!" however when reference is made within the macro to these parameters, it is necessary to add a "!" at the beginning of the parameter name.
  6. This loads the file in memory. As written, the macro needs to be changed when the initial population file changes. It would be easy to modify the macro so that the name and path of the file would be given as an argument in the macro call. There would then be 3 arguments instead of 2. One advantage of giving the file name in the macro call is that the macro can then be loaded in memory using an INCLUDE command.
  7. This line defines a new variable named draw and assigns a random value between 0 and 1 to each case in in the file.
  8. This defines a new variable named dummy which equals 1 for all cases in the file. This variable will be needed in line 9.
  9. This ranks each value of draw in increasing order. For each case, the rank of the value of draw is contained in the variable rank1. Thus the smallest value of draw in the file gets the rank1=1. The second smallest value gets rank1=2.
  10. It is clear that each case of the original file has an equal chance of getting the smallest, second smallest, etc. value of draw. Thus when, in line 10,  we select the cases for which the rank1 has a value between 1 and !nbcases (in other words, the !nbcases cases which have the smallest value of draw), we are selecting a random sample of the initial population.
  11. This saves the random sample in a file. When !var1 equals 1, the name of the file is "new file 1.sav", when !var2=2, the name is "new file 2.sav" and so on up to 300. See the Syntax Reference Guide for explanations of !QUOTE and !CONCAT.
  12. The end of the loop.
  13. The end of the macro definition.
  14. See comments on line 3.
  15. This line is just a comment.
  16. Once a macro is working, you may replace the "yes" by a "no" in order to reduce the number of lines in the LOG section of the OUTPUT Window.
  17. The macro call  !getsamp nbcases=300 nbfiles=4. causes the macro to get 4 random files of 300 cases each. Similarly !getsamp nbcases=300 nbfiles=1000. would generate 1,000 random files.
    The macro definition (10 lines) plus that one line of macro call are replacing about 10,000 lines of syntax which would be required without a macro!! It should be clear to anybody that macros are very useful.

Note that if the only thing required were to calculate the Mean, Standard Deviation or some other statistics for each of the sample files, then these values could be saved to a single result file; there would be only one file created by the macro.

 

Replacing a character in many string variables

In String Manipulation Tutorial the following syntax is used to replace "." by ",":

LOOP IF INDEX(name2,".")>0.
+   COMPUTE SUBSTR(name2,INDEX(name2,"."),1)=",".
END LOOP.

Using the same technique for 100 variables would require 300 lines of code! The following circumstances are presented and the  solutions are given.

  1. Example 1
    Assume variables have related names but are not necessarily consecutive in the data file (for instance var1, var2, var3, ... or any other characters instead of "var")
  2. Example 2
    Assume variables have unrelated names but are consecutive in the data file (for instance vara, name, address, ..., product are consecutive in the data file)
  3. Example 3
    Assume variables have unrelated names but are non consecutive in the data file, however all string variables between the first variable (for instance "vara" in 2 above) and the last variable ("product" in 2 above) need to be processed.
  4. Example 4
    Assume variable have unrelated names and are not consecutive in the file. In addition, some of the string variables between the first variable (for instance "vara" in 2 above) and the last variable ("product" in 2 above) must not be processed.

The file Change some characters inside many string variables.SPS contains the 4 examples given below.

1. Replace Characters Example 1

Variables have related names but are not necessarily consecutive in the data file (for instance the names are var1, var2, var3, ... or any other characters instead of "var")

* Create a data file to work with.

INPUT PROGRAM.
LOOP id=1 TO 3.
VECTOR var(100A8).
LOOP cnt=1 TO 100.
COMPUTE var(cnt)="txt.abc".
END LOOP.
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.
EXECUTE.

* The following approach using a loop does NOT work because SUBSTR needs an actual variable name.
* It does not recognize the vector v(cnt) as a variable name.

VECTOR v=var1 TO var100.
LOOP cnt=1 TO 100.
+ LOOP IF INDEX(v(cnt),".")>0.
+ COMPUTE SUBSTR(v(cnt),INDEX(v(cnt),"."),1)=",".
+ END LOOP.
END LOOP.
EXECUTE.

* Using DO REPEAT does work.
DO REPEAT v=var1 TO var100.
LOOP IF INDEX(v,".")>0.
+ COMPUTE SUBSTR(v,INDEX(v,"."),1)=",".
END LOOP.
END REPEAT.
EXECUTE.

* The following macro solution also works.
SET MPRINT=no.
*/////////////////////.
DEFINE !replace(vname=!TOKENS(1)
    /nbvars=!TOKENS(1)
    /oldchar=!TOKENS(1)
    /newchar=!TOKENS(1))
* To replace a character in many string variables.

!DO !cnt=1 !TO !nbvars
LOOP IF INDEX(!CONCAT(!vname,!cnt),!oldchar)>0.
+ COMPUTE SUBSTR(!CONCAT(!vname,!cnt),INDEX(!CONCAT(!vname,!cnt),!oldchar),1)=!newchar.
END LOOP.
!DOEND
EXECUTE.
!ENDDEFINE.
*/////////////////////.

SET MPRINT=yes.
* The macro is called as follows.
!replace vname=var nbvars=100 oldchar='.' newchar=','.

* The DO REPEAT solution is easier than the above macro solution, however, one has to remember that
* procedures are not allowed inside a DO REPEAT structure, the Macro solution is thus more general.

 

2. Replace Characters Example 2

Assume variables have unrelated names but are consecutive in the data file (for instance vara, name, address, ..., product are consecutive in the data file)

* The most convenient way to handle this situation is when one has simply to give to a macro the name of the first and last variables which have to be processed.

* One of the macro Gems (see "Define list of variables between two variables.SPS" in http://pages.infinit.net/rlevesqu/Macros.htm#MacroGems) can then be used.

* Create a data file.

INPUT PROGRAM.
LOOP id=1 TO 3.
STRING strone text34 str2 beta gamma alpha (A8).
DO REPEAT var=strone text34 str2 beta gamma alpha.
COMPUTE var="txt.ab.c".
END REPEAT PRINT.
END LOOP.
END CASE.
END FILE.
END INPUT PROGRAM.
EXECUTE.
SAVE OUTFILE='c:\temp\testdata.sav'.

* Suppose we want to process variables between text34 and gamma inclusively. This solution assumes we have hundreds of variables between these 2 variables. It is not convenient to list them manually.

* Note however that the following solution can also be used when you know that all variables after a given variables need to be processed but you do not know ahead of time the name of the last variable, in that case you simply create a new string variable and use that name as the "ending variable".

* Note: when you save the macro Gem in your macro folder, keep ONLY the macro definition, in other words, delete the data definition and the example of macro call,

INCLUDE FILE='c:\Program Files\SPSS\macros\DefineListOfVariablesBetweenTwoVariables.SPS'.

* Call the macro Gem to define a macro which gives the list of all variables between the 2 given variables.
!DefList var1=text34 var2=gamma fname='c:\temp\testdata.sav'.

* The macro is called as follows.
SET MPRINT=yes.
!rep_chr oldchar='.' newchar=',' vnames=!list1.

* See Example 4 below for the definition of the macro !rep_chr.

 

3. Replace Characters Example 3

Assume variables have unrelated names but are non consecutive in the data file, however all string variables between the first variable (for instance "vara" in 2 above) and the last variable ("product" in 2 above) need to be processed.

* The same solution as in EXAMPLE 2 can be used.

* Create a data file.
INPUT PROGRAM.
LOOP id=1 TO 3.
STRING strone text34 str2 beta gamma (A8).
DO REPEAT var=strone text34 str2 beta gamma.
COMPUTE var="txt.ab.c".
END REPEAT PRINT.
END LOOP.
END CASE.
END FILE.
END INPUT PROGRAM.
EXECUTE.

* create a numeric variable between the first and last string variables.
COMPUTE numvar=2.
STRING alpha(A8).
COMPUTE alpha="txt.a.b.c".
SAVE OUTFILE='c:\temp\testdata.sav'.

INCLUDE FILE='c:\Program Files\SPSS\macros\DefineListOfVariablesBetweenTwoVariables.SPS'.

* Call the macro Gem to define a macro which gives the list of all variables between the 2 given variables.
!DefList var1=text34 var2=alpha fname='c:\temp\testdata.sav'.

* The macro is called as follows.
SET MPRINT=yes.
!rep_chr oldchar='.' newchar=',' vnames=!list1.

* Note that *errors* will occur when the macro attempts to modify the numeric variables (variable numvar), as a result of that error, the command will not be executed. However, we do not care since we did not intend to modify numeric variables in any event.

* See Example 4 below for the definition of the macro !rep_chr.

4. Replace Characters Example 4

Assume variable names have unrelated names and are not located consecutively in the data file. Assume further that some of the string variables between the first variable (for instance "vara" in 2 above) and the last variable ("product" in 2 above) must be processed but some other string variables between the first and last variable must not be processed

* In this case, the names of each string variable which needs to be processed must be explicitly given to the macro.

INPUT PROGRAM.
LOOP id=1 TO 3.
STRING strone text34 str2 name alpha (A8).
DO REPEAT var=strone text34 str2 name alpha .
COMPUTE var="txt.ab.c".
END REPEAT PRINT.
END LOOP.
END CASE.
END FILE.
END INPUT PROGRAM.
EXECUTE.

* A macro can be used.

SET MPRINT=no.
*/////////////////////.
DEFINE !rep_chr(oldchar=!TOKENS(1)
    /newchar=!TOKENS(1)
    /vnames=!CMDEND)
/* To replace a character in many string variables */

!DO !vname !IN (!vnames)
LOOP IF INDEX(!vname,!oldchar)>0.
+ COMPUTE SUBSTR(!vname,INDEX(!vname,!oldchar),1)=!newchar.
END LOOP.
!DOEND
EXECUTE.
!ENDDEFINE.
*/////////////////////.

* Assume that for variables text34 str2 alpha we want to replace all dots by commas. (The variables are not consecutive in the file.)

* The macro is called as follows.
SET MPRINT=yes.
!rep_chr oldchar='.' newchar=',' vnames=text34 str2 alpha.

*Of course if you wanted to replace occurences of 'a' by 'b' you would call the macro as follows.

!rep_chr oldchar='a' newchar='b' vnames=text34 str2 alpha.

 

Modify a macro to handle many variables

Version 1

* This macro is useful when you have to change the format from
*      string to string (ss)
*      numeric to string (ns) or
*      string to numeric (sn)
* while retaining the original variable name.
* rlevesque@videotron.ca 2001/05/23.

*/////////////////////.
DEFINE !convert (vname=!TOKENS(1)
    /nformat=!TOKENS(1)
    /type=!TOKENS(1))
/* where vname= the name of the variable to change format*/.
* nformat = the new format.
* type of change = ss (string to string), ns or sn where n=numeric and s=string.

!IF (!type='ss') !THEN STRING temp1234(!nformat).
COMPUTE temp1234=!vname.
!IFEND
!IF (!type='ns') !THEN STRING temp1234(!nformat).
COMPUTE temp1234=LTRIM(STRING(!vname,F18.0)).
!IFEND !IF (!type='sn') !THEN COMPUTE temp1234=NUMBER(!vname,F18.0).
FORMAT temp1234(!nformat).
!IFEND

MATCH FILES FILE=* /DROP=!vname.
RENAME VARIABLE (temp1234=!vname).
!ENDDEFINE.
*/////////////////////.

* Create a dummy data file to illustrate the use of the macro.
DATA LIST LIST /var1(A8), var2(A8), var3(F8.0).
BEGIN DATA
abcdefg 254 235
adadad 128 265
END DATA.
LIST.

* Example 1.
!convert vname=var1 nformat=A5 type=ss.
*The above command converts variable var1 from format (A8) to format (A5).

* Example 2.
!convert vname=var2 nformat=F8.2 type=sn.
*The above command converts variable var2 from format (A8) to format (F8.2).
* (Of course if the string contains an invalid number, the resulting numeric variable will be missing.

* Example 3.
!convert vname=var3 nformat=A8 type=ns.
*The above command converts numeric var3 from format (F8.2) to format (A8).

**************************************.
* Version 2 of the macro (a generalization).
* Suppose we often have to change the format of 5 to 10 variables and that the same type (ss sn or ns) is involved.
* We now change the macro so that only one macro call is required to do all 5-10 variables.
**************************************.

SET MPRINT=yes.
*/////////////////////.
DEFINE !conver2 (type=!TOKENS(1)
    /cformat=!TOKENS(1)
    /nformat=!TOKENS(1)
    /vnames=!CMDEND)

/* where.
* type     = type of change ss (string to string) ns or sn where n=numeric and s=string.
* cformat = the conversion format (only needed when type=ns).
* nformat = the new format.
* vnames= the names of the variables to change format.

!DO !vname !IN (!vnames)
!IF (!type='ss') !THEN STRING temp1234(!nformat).
COMPUTE temp1234=!vname.
!IFEND

!IF (!type='ns') !THEN
STRING temp1234(!nformat).
COMPUTE temp1234=LTRIM(STRING(!vname,!cformat)).
!IFEND                        
!IF (!type='sn') !THEN
COMPUTE temp1234=NUMBER(!vname,!nformat).
FORMAT temp1234(!nformat).
!IFEND                        
MATCH FILES FILE=* /DROP=!vname.
RENAME VARIABLE (temp1234=!vname).
!DOEND
!ENDDEFINE.
*/////////////////////.                        

* Testing the macro (only 4 variables are used instead of 5-10).
DATA LIST LIST /var1(A8) vnum2(A8) varx(A8) vary(A8).

BEGIN DATA
1235.23		254.13	235.00	6532.20 
53261.32	128.09	265.85	2591.99
END DATA.
LIST.                        

* Change the format from 2 to 3 decimal places.
!conver2 type=sn nformat=F8.3 vnames=var1 vnum2 varx vary.

* Here is the macro expansion of the above macro call.
199 M>
200 M> COMPUTE TEMP1234=NUMBER( var1 , F8.3 ).
201 M> FORMAT TEMP1234( F8.3 ).
202 M>
203 M> MATCH FILES FILE=* /DROP= var1.
204 M> RENAME VARIABLE (TEMP1234= var1 ).
205 M>
206 M>
207 M> COMPUTE TEMP1234=NUMBER( vnum2 , F8.3 ).
208 M> FORMAT TEMP1234( F8.3 ).
209 M>
210 M> MATCH FILES FILE=* /DROP= vnum2.
211 M> RENAME VARIABLE (TEMP1234= vnum2 ).
212 M>
213 M>
214 M> COMPUTE TEMP1234=NUMBER( varx , F8.3 ).
215 M> FORMAT TEMP1234( F8.3 ).
216 M>
217 M> MATCH FILES FILE=* /DROP= varx.
218 M> RENAME VARIABLE (TEMP1234= varx ).
219 M>
220 M>
221 M> COMPUTE TEMP1234=NUMBER( vary , F8.3 ).
222 M> FORMAT TEMP1234( F8.3 ).
223 M>
224 M> MATCH FILES FILE=* /DROP= vary.
225 M> RENAME VARIABLE (TEMP1234= vary ).
226 M> .
227 M> 
             

* Convert the result back to string (but with only one decimal place).
!conver2 type=ns cformat=F8.1 nformat=A8 vnames=var1 vnum2 varx vary.

* Here is the macro expansion of the above macro call.
233 M>
234 M> STRING TEMP1234( A8 ).
235 M> COMPUTE TEMP1234=LTRIM(STRING( var1 , F8.1 )).
236 M>
237 M>
238 M> MATCH FILES FILE=* /DROP= var1.
239 M> RENAME VARIABLE (TEMP1234= var1 ).
240 M>
241 M> STRING TEMP1234( A8 ).
242 M> COMPUTE TEMP1234=LTRIM(STRING( vnum2 , F8.1 )).
243 M>
244 M>
245 M> MATCH FILES FILE=* /DROP= vnum2.
246 M> RENAME VARIABLE (TEMP1234= vnum2 ).
247 M>
248 M> STRING TEMP1234( A8 ).
249 M> COMPUTE TEMP1234=LTRIM(STRING( varx , F8.1 )).
250 M>
251 M>
252 M> MATCH FILES FILE=* /DROP= varx.
253 M> RENAME VARIABLE (TEMP1234= varx ).
254 M>
255 M> STRING TEMP1234( A8 ).
256 M> COMPUTE TEMP1234=LTRIM(STRING( vary , F8.1 )).
257 M>
258 M>
259 M> MATCH FILES FILE=* /DROP= vary.
260 M> RENAME VARIABLE (TEMP1234= vary ).
261 M> .

 

**************************************.
* Version 3 of the macro.
* This is actually the same macro as version 2 but here we use it jointly with one of the Macro Gems
* Suppose we often have to change the format of 600 variables and that the
* same type (ss sn or ns) is involved.
**************************************.

!DEfList var1=g var2=k fname="c:/temp/mydata.sav".

* The macro !DefList (for Define List) can be found here.
* The above macro call defines a macro named !list1 whose purpose is to list the names
* of all the variables between var1 and var2 in the file c:/temp/mydata.sav
* This list is then used as one of the parameter in the macro call of conver2 as follows.

!conver2 type=ns cformat=F8.1 nformat=A8 vnames=!list1.
* The above 2 macros will convert all variables between g and k (they are all assumed to be numeric) to a string.
The names of the variables will remain the same (only the format will change).

Note: If only some variables between variables g and k were numeric we could use the macro !vartype (also located in Macro Gems) to obtain the list of only numeric (or only string) variables. We would then use the correct variable list as the argument of the !conver2 macro call. 

 

 

Doing arithmetic with macro variables (for advanced users)

It is not possible to (directly) do arithmetic with macro variables. For instance, given the following 2 macro statements: !LET !a=1.
!LET !b=2.

the following is not a valid command:
!LET !c=!a + !b.

The following is an indirect method of obtaining the sum of !a and !b.
!LET !c=!LENGTH(!CONCAT(!BLANK(!a),!BLANK(!b)).
This is not a very friendly method... Try to do a division (or calculate the log of !b) with a similar method!

* Suppose we need the macro variable !myvar to take on "complex" values such as the ln(2), ln(3) and ln(4). Any number of values and any other formulas could be handled, but I will use these 3 to illustrate the method. The trick is to have SPSS a) calculate the numeric values and then b) write the syntax which will subsequently be run.

SET MPRINT=yes.                                  
*///////////////////////////////////.
DEFINE !doit(nbval=!TOKENS(1))
INPUT PROGRAM.
LOOP cnt=1 TO !nbval.
COMPUTE myval=LN(cnt).
END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.                         

* Next line assumes you need 5 digits.
FORMAT myval(F8.5).
* Define the macro variable.
DO IF cnt=1.
WRITE OUTFILE='c:\temp\macro var.sps' /"DEFINE !values()"/myval.
ELSE IF cnt<!nbval.
WRITE OUTFILE='c:\temp\macro var.sps' /myval.
ELSE.
WRITE OUTFILE='c:\temp\macro var.sps' /myval /"
!ENDDEFINE.".
END IF.
EXECUTE.

* Run the syntax to define the variable.
INCLUDE FILE='c:\temp\macro var.sps'.
EXECUTE.
!ENDDEFINE.
*///////////////////////////////////.

!doit nbval=3.
*In the following macro, the macro variable !myvar takes on the values ln(1), ln(2) and ln(3).

DEFINE !test()
!LET !cnt=!NULL
!LET !val=!EVAL(!values)
!DO !myvar !IN (!val)
!LET !cnt=!CONCAT(!cnt,!BLANK(1))
COMPUTE !CONCAT(var,!LENGTH(!cnt))=!myvar.
!DOEND
EXECUTE.
!ENDDEFINE.                               

!test.
* Next line is to see all five digits.
FORMAT var1 TO var2 (F8.5).

This is the Output of the above Syntax.

 

Another fully commented macro

This macro prints bar charts of categorical variables excluding category values were there are less than n cases.

 

1 SET MPRINT=no.
2 *////////////////////.
3 DEFINE !chart2 (minnb=!TOKENS(1) /cats=!CMDEND)
4 /* minnb = minimum number of cases required in order to chart category value*/.
5 /* cats    = names of the categorical variables to be charted*/.

6 COMPUTE dummy=1.
7 !DO !cat !IN (!cats)
8 FILTER OFF.
9 * Sorting is necessary for the MATCH FILES command.
10 SORT CASES BY !cat.
11 !LET !fname=!QUOTE(!CONCAT('C:\temp\',!cat,'.SAV'))
12AGGREGATE
13    /OUTFILE=!fname
14    /PRESORTED
15    /BREAK=!cat
16   /nb = N(dummy).
17MATCH FILES /FILE=*
18    /TABLE=!fname
19    /BY !cat.
20FREQUENCY VARIABLES=!cat.
21LIST.
22COMPUTE flag=(nb>=!minnb).
23FILTER BY flag.
24GRAPH
25    /BAR(SIMPLE)=COUNT BY !cat
26    /MISSING=REPORT.
27* Delete the variable nb.
28MATCH FILES FILE=* /DROP=nb.
29!DOEND

30* Delete the variable dummy.
31MATCH FILES FILE=* /DROP=dummy.
32!ENDDEFINE.
33*///////////////////

34SET MPRINT=yes.
35!chart2 minnb=5 cats= age prog year.
36EXECUTE.

 

The macro definition starts at line 3 and ends at line 32. The macro is called at line 35. Note that the line numbers are included only to facilitate the comments. If you want to run this syntax, the line numbers must be removed. It is recommended that all commands start in the first column, sub-commands on the other hand should start in the second or higher column. Doing so ensures that the macro can be INCLUDED in an other syntax file using the INCLUDE command.

Comments on each line
  1. I use the SET MPRINT command all the time when I write or debug a macro. For greater readability of the Output Window, it is preferable to set it to no before the macro definition starts and to set it to yes (see line 34) before calling the macro.
  2. I like to enclose my macros between special characters which I use just for this purpose. See line 2 and 33. This allows me to locate macros faster in long syntax files.
  3. The beginning of the macro. The name of the macro is !chart2. I like to start macro names with ! this helps to quickly identify macro names in ordinary syntax. The macro requires 2 arguments when called: minnb and cats. Thus if minnb=5 and cats=var1 var2 var3, the macro will generate bar charts for the categorical variables var1, var2 and var3 where any given category of these variables will only be charted if they have more than 5 cases with that category value. Note that a command terminator is not required at the end of a macro command. (!ENDDEFINE is more a syntax than a macro command). Since there are 3 tokens (or variables) in !cats, the syntax within the loop will be executed 3 times.
  4. This is a comment. Within macros, it is safer to always begin comments with /* and end them with */
  5. A second line of comment.
  6. The variable dummy is used in line 16.
  7. The !DO command works in conjunction with the !DOEND in line 29 to form a DO LOOP.
    Line 7 means that the macro variable !cat will successively take on each of the values contained in the macro variable !cats. The Macro variable !cats is defined in the macro call in line 35. In this example, since cats=age prog year, the macro variable !cat will successively equal each of these 3 categorical names.
  8. This turns the filter off. All cases become visible to SPSS procedures after this line is executed. Note that in line 23 some variables are filtered out. Line 8 thus undo what line 23 has done.
  9. A comment.
  10. This sorts the data file by increasing value of the variable contained in the macro variable !cat. (Thus the first time the loop goes through, this will sort the file by order of age; the second time the loop goes through, sorting will be done by order of prog, etc)
  11. This defines the macro variable !fname. When !cat=age, we have
    !fname=!QUOTE(!CONCAT('C:\temp\',age,'.SAV'))
    !fname=!QUOTE(C:\temp\age.SAV)
    !fname="C:\temp\age.SAV"
  12. This AGGR command (line 12 to 16) counts the number of cases that fall within each category value of the categorical variable !cat. For instance there might be 3 persons aged 25, 7 aged 26, etc. The results are saved in the file whose name is given by the macro variable !fname.
  13. This sub-command of the AGGREGATE command gives the name of the file which will contain the results
  14. Since the file was sorted in line 10, this line informs SPSS that it is not necessary to sort the file. When the file is presorted, less memory is required to run the AGGR command. The command also executes faster.
  15. This instruct the AGGR command to use the variable represented by the macro variable !cat as the Break variable.
  16. In addition to the break variable, the file of results will contain a variable named nb which will contain the number of cases having each of the different values of the break variable. For instance when !cat=age, the result file might contain cases such as
    case 1: age=25, nb=5
    case 2: age=26, nb=7
    etc
  17. Match the current file with
  18. the file whose name is in the macro variable !fname
  19. do the matching  on the basis of the variable whose name is in the macro variable !cat. The variable nb is added to the original data file.
    Thus if case 1 of the original data file has age=25, the value of nb will be 5
    Similarly if case 2 of the original data file has age=26, the value of nb will be 7
    Similarly if case 3 of the original data file has age=25, the value of nb will be 5, and so on.
  20. This prints the Frequency Table of var !cat. This is just to allow verification that only the categorical values with at least !minnb cases are entered in the chart graph.
  21. The list of cases is printed for information purposes. This line should be commented out when you do real jobs.
  22. The flag variable will equal 1 for cases with a value of nb > !minnb. In other words, only cases for which we want to do a bar chart will have a value of 1. Other cases will have a value of 0.
  23. This line renders "invisible" cases where flag=0. These cases are ignored by SPSS procedures. It is as if they not in  the data file. They remain invisible until a FILTER OFF command is encountered.
  24. These
  25. lines
  26. print the Bar Charts. Of course you could replace this by any other graph.
  27. A comment
  28. It is necessary to delete the variable nb otherwise lines 17-19 will not be able to add the variable nb (since it will already exist) the next time the loop goes through.
  29. The end of the syntax to be repeated within the loop. Control goes back to line 7 to process the next variable name contained in the macro variable !cats.
  30. A comment
  31. This deletes the variable dummy which is no longer necessary.
  32. The end of the macro definition.
  33. See comment on line 2.
  34. See comments on line 1.
  35. This is the macro call. The line should now be clear.

 

(This area will be expanded)

 

AddFreeStats.com Free Web Stats in real-time !

 

 

This page was last updated on July 18, 2003.  Raynald's SPSS Tools

© Raynald Levesque 2001-04