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.