I need to run a block of syntax (shown at bottom of this post) for 30 or so variables and I'd prefer to create a macro that passes in each variable rather than create syntax for all 30 variables. Actually, I already created the syntax for all 30 vars, but I'd still prefer to create a macro.
I created part of the macro and was able to get the RANK and sort cases to work, but I got hung up on the "if" statement. It gives me this error which I haven't been able to resolve...
Error # 6834 in column 70. Text: 25
In a macro expression, an operand was not preceded by an operator.
Here is the macro test that I'm getting the error on...
DEFINE !outliers (!POS = !CHAREND('/'))
!DO !i !IN (!1)
RANK VARIABLES=!i (A)
/NTILES(4)
/PERCENT
/PRINT=No
/TIES=MEAN.
sort cases !CONCAT(P,!i) (A).
if (TRUNC(!CONCAT(P,!i)) ge 25 and LAG(!CONCAT(N,!i))=1) !CONCAT(!i,_25th)=!i.
!DOEND
!ENDDEFINE.
!outliers Q16 Q17 Q18 Q19 /.
And here is the syntax that I want turned into a macro, where I would like a macro var to replace all instance of Q16 (which didn't work)...
RANK VARIABLES=Q16 (A)
/NTILES(4)
/PERCENT
/PRINT=No
/TIES=MEAN.
sort cases PQ16 (A).
if (TRUNC(PQ16) ge 25 and LAG(NQ16)=1) Q16_25th=Q16.
if (TRUNC(PQ16) ge 75 and LAG(NQ16)=3) Q16_75th=Q16.
AGGREGATE
/OUTFILE= MODE=ADDVARIABLES
/BREAK=
/Q16_25th_first=FIRST(Q16_25th)
/Q16_75th_first=FIRST(Q16_75th).
compute LOFQ16= Q16_25th_first - 3 (Q16_75th_first - Q16_25th_first).
compute UOFQ16= Q16_75th_first + 3 * (Q16_75th_first - Q16_25th_first).
OMSEND.
temp.
select if Q16 le LOFQ16 or Q16 ge UOFQ16.
freq vars Q16.
Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=Q16 MISSING=LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: Q16=col(source(s), name("Q16"))
DATA: id=col(source(s), name("$CASENUM"), unit.category())
GUIDE: text.title(label("Q16"))
ELEMENT: schema(position(bin.quantile.letter(1*Q16)), label(id))
END GPL.
FYI, this block of syntax identifies outliers (LOF and UOF=Lower and Upper Outer Fences), runs a frequency on those outliers, and then creates a boxplot of all values.
Answer by Chris Keran (26) | Mar 31, 2015 at 06:10 PM
Hah! That was it. Thank you kindly, Jon.
FYI, I knew it was related to "25" but I was incorrectly focusing on the first instance, but after you said it, I saw the error pointed to column 70 in the text--that should've been my clue.
Thanks again.
Answer by Chris Keran (26) | Mar 31, 2015 at 06:44 PM
I fixed and ran the entire block and it worked except not the chart. I now get a warning with the GGRAPH command. Thoughts?
Warnings
An inline graph specification was expected but not found.
Execution of this command stops.
DEFINE !outliers (!POS = !CHAREND('/'))
!DO !i !IN (!1)
RANK VARIABLES=!i (A)
/NTILES(4)
/PERCENT
/PRINT=No
/TIES=MEAN.
sort cases !CONCAT(P,!i) (A).
if (TRUNC(!CONCAT(P,!i)) ge 25 and LAG(!CONCAT(N,!i))=1) !CONCAT(!i,"_25th")=!i.
if (TRUNC(!CONCAT(P,!i)) ge 75 and LAG(!CONCAT(N,!i))=3) !CONCAT(!i,"_75th")=!i.
AGGREGATE
/OUTFILE= MODE=ADDVARIABLES
/BREAK=
/!CONCAT(!i,"_25th_first")=FIRST(!CONCAT(!i,"_25th"))
/!CONCAT(!i,"_75th_first")=FIRST(!CONCAT(!i,"_75th")).
compute !CONCAT("LOF",!i) = !CONCAT(!i,"_25th_first") - 3 (!CONCAT(!i,"_75th_first") - !CONCAT(!i,"_25th_first")).
compute !CONCAT("UOF",!i) = !CONCAT(!i,"_75th_first") + 3 * (!CONCAT(!i,"_75th_first") - !CONCAT(!i,"_25th_first")).
*OMSEND.
temp.
select if !i le !CONCAT("LOF",!i) or !i ge !CONCAT("UOF",!i).
freq vars !i.
Chart Builder.
GGRAPH
/GRAPHDATASET NAME="graphdataset" VARIABLES=!i MISSING=LISTWISE REPORTMISSING=NO
/GRAPHSPEC SOURCE=INLINE.
BEGIN GPL
SOURCE: s=userSource(id("graphdataset"))
DATA: !i=col(source(s), name("!i"))
DATA: id=col(source(s), name("$CASENUM"), unit.category())
GUIDE: text.title(label("!i"))
ELEMENT: schema(position(bin.quantile.letter(1!i)), label(id))
END GPL.
!DOEND
!ENDDEFINE.
!outliers Q16 Q17 Q18 Q19 /.
Answer by JonPeck (4671) | Mar 31, 2015 at 07:02 PM
I'm sorry to have to quote this to you from the CSR:
"Inline GPL blocks cannot be contained within DEFINE-!ENDDEFINE macro definitions. If you want to use
GPL within macro definitions, do not use inline GPL blocks. Put the GPL in a file and reference it with
SOURCE=GPLFILE."
Of course, that solution won't work in your case. While sometimes macro expansion can work in GPL, it is not supported due to the nature of the GPL syntax.
The way to do this is with Python programmability rather than macro. If you want to go that route and can send me some sample data to test with (peck AT us.ibm.com), I can convert this for you.
Answer by Chris Keran (26) | Mar 31, 2015 at 08:43 PM
Awesome, thank you. I just now sent you an email.
Installing python plugin 6 Answers
Fuzzy match tolerance 4 Answers
spss.Submit and unicode strings 1 Answer
spssaux.GetValuesFromXMLWorkspace 2 Answers
COM objects only working if SPSS already running? 10 Answers