Reputation: 79
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.
Upvotes: 3
Views: 1095
Reputation: 2929
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.
/* 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.
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.
Upvotes: 3
Reputation: 5417
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.
Upvotes: 3
Reputation: 11360
!max
so there's no reason to assume that your macro would do anything.aggregate
function. If you want to do it through a macro anyway, have the macro create the aggregate function.Upvotes: 3