3

I'm a beginner with SPSS macros, but I can't find any general guide on macro variables (I've obviously tried Raynald Levesque's site).

So I'm trying to write a macro variable that gives me the maximum value of a column. The column is called anmois Here's what I wrote :

    SET MPRINT=yes /PRINTBACK=yes.
    DEFINE !max (anmois)
    !LET !max = !max(anmois)
    !ENDDEFINE.

It doesn't work. Any help would be greatly appreciated.

Thanks.

Negarev
  • 79
  • 2
  • 8

3 Answers3

3
  1. there is no macro function !max so there's no reason to assume that your macro would do anything.
  2. macro functions don't in themselves manipulate or analyze the data - they only create syntax, which then does the manipulation and analysis.
  3. you don't need a macro to calculate the max of your column - use aggregate function. If you want to do it through a macro anyway, have the macro create the aggregate function.
eli-k
  • 10,898
  • 11
  • 40
  • 44
  • 1. So you can't use regular SPSS functions (like max(), concat() etc...) in a macro ? That's odd, because I don't understand the difference between commands like aggregate and functions like max(). To me they're all commands. – Negarev Nov 27 '16 at 22:02
  • SPSS macro commands are used to automate creation of syntax, e.g. you can create a complex process including `aggregate` and `max()` and any other syntax command, and then run this with one macro call at any point you wish. so all of the macro commands are about creating the text of the syntax, and some are similar to regular syntax for text manipulation, but none of them actually touch the data. so the macro creates syntax for `aggregate` and for `max()` but these commands are not part of the macro language, and not part of the macro. – eli-k Nov 28 '16 at 05:18
3

If you are just starting with macro, I suggest that your learn Python for SPSS instead. It is much more powerful. You can download the Programming and Data Management book from the IBM Predictive Analytics Community website. It shows how to use the language for typical SPSS tasks. The details of all the functions can be found in the help, too.

The Macro documentation is not great, but you can read about it under DEFINE and in a later section in the Command Syntax Reference accessible from the Help menu.

JKP
  • 5,419
  • 13
  • 5
3

You can achieve this using the old skool Ugly Hack method or Python. I echo Jons’ (JKP) comments that if you are learning macros for the first time, better to jump straight to Python.

I provide solutions for both methods below.

/* Generate demo data*/.
DATA LIST LIST
/ ID V1.
BEGIN DATA.
1, 4.56
2, 6.85
3, 7.75
END DATA.
DATASET NAME dsDemo.

Native SPSS 'Ugly Hack' Method:

/* Using AGGREGATE obtain max value*/.
DATASET DECLARE dsAggMax. 
AGGREGATE OUTFILE=dsAggMax /V1_Max=MAX(V1).


/* Using WRITE OUTFILE generate the appropriate syntax to define max value as a macro variable*/.
DATASET ACTIVATE dsAggMax.
DO IF $CASENUM=1. 
WRITE OUTFILE='C:\Temp\macro var.sps' /  "DEFINE !MyV1Max()", V1_Max (F8.3), " !ENDDEFINE.".
END IF.
EXECUTE.

/* Run the generated syntax file*/.
INSERT FILE='C:\Temp\macro var.sps'.

/*Use the defined macro in whatever context you wish*/.
DATASET ACTIVATE dsDemo.
COMPUTE IsMax_1WrtSyn=V1=!MyV1Max.

Python Method:

BEGIN PROGRAM PYTHON.
import spssdata
#Step1: Get all values in from desired variable V1
s1=spssdata.Spssdata("V1", names=False, convertUserMissing=True, omitmissing=True).fetchall()
print s1 #[(4.56,), (6.85,), (7.75,)]

#Step 2:  Retrieve just the first item holding the data value from each tuple
s2=[i[0] for i in s1]
print s2 #[4.56, 6.85, 7.75] 

#Step 3: Get the maximum value from these values
s3=max(s2)
print s3 #7.75

#Step 4a: Use this maximum value stored in a python variable to execute desired job
spss.Submit("COMPUTE IsMax_2Py=V1=%(s3)s." % locals())

#Alternatively

#Step4b: Create a DEFINE macro variable storing the max value to use outside of python and in native SPSS syntax

spss.SetMacroValue("!MyV1Max_PyGen", s3)
END PROGRAM PYTHON.

/* Use the python generate macro in Step4b outside of python*/.
COMPUTE IsMax_3PyGen=V1=!MyV1Max_PyGen.
Jignesh Sutar
  • 2,909
  • 10
  • 13
  • Alright I understand, thanks a lot ! I guess I thought macros worked like VBA ... – Negarev Nov 27 '16 at 21:35
  • Not such a simple comparison. But as Eli-k summarises macros in SPSS are mere string generating for syntax processing than reading data. – Jignesh Sutar Nov 27 '16 at 21:44