SPSS Statistics

 View Only

 Conducting multiple CART analyses at once using SPSS script

Jihwan Lee's profile image
Jihwan Lee posted Thu June 26, 2025 09:09 AM

I tried conducting CART analysis on 2 dependent variables (), 5 sets of independent variables (G1~G5), and 17 observation groups classified by the variable "city_id", so that it would yield a total of 2*5*17=170 results at once.

The version is SPSS 28 (free trial). The code is as follows:

DEFINE !DV () h_ousingsatisfaction h_ousingenvsatisfaction !ENDDEFINE.
DEFINE !G1() 
    A_beneficiaries
!ENDDEFINE.
DEFINE !G2()   
    A_gender A_familysize B_constructionyear B_indoorenvironmentlevel B_indoorsafetyclean C_safetyaroundhouse C_pedestriansafety C_noisearoundhouse C_cleannessaroundhouse C_airpollutionaroundhouse C_accesstomedical C_accesstopublic C_accesstoparking
!ENDDEFINE.
DEFINE !G3() 
    A_married A_children B_housemanageexpense C_neighborhoodrelation C_accesstotransportation
!ENDDEFINE.
DEFINE !G4() 
    A_age B_houseprice B_interiorquality C_accesstonatural
!ENDDEFINE.
DEFINE !G5() 
    A_education A_monthlyincome A_debt B_dwellingarea C_schooldistrict C_accesstocommercial C_accesstocultural
!ENDDEFINE.
DEFINE !CITY () 11 21 22 23 24 25 26 29 31 32 33 34 35 36 37 38 39       !ENDDEFINE.
 
DEFINE !RunCART (dep !TOKENS(1)
                                   inds  !TOKENS(1)
                                   city    !CMDEND)
     SELECT IF (city_id = !city).
     EXECUTE.
     TREE !dep
        BY !inds
        /METHOD REG
        /CRITERIA MAXDEPTH(5) MINGROUP(20) MINPARENT(40)
        /PRINT SUMMARY SPLIT
        /SAVE PREDVALUE(pred_!dep._city!city)
        /PLOT NODE SUMMARY.
    GRAPH EXPORT "tree_!dep._city!city.png" /REPLACE WIDTH=800.
    SELECT IF (city_id >= 0).
    EXECUTE.
!ENDDEFINE.
 
 
!DO !dep IN !DV
    !DO !gNO IN G1 G2 G3 G4 G5
        !DO !cID IN !CITY
            !RunCART dep !dep city !cID inds !(!gNO)
        !ENDDO
    !ENDDO
!ENDDO.
However, I got the error in the DEFINE !RunCART section:
- The DEFINE statement contains an invalid keyword specification.  Valid specifications are !DEFAULT, !NOEXPAND, !TOKENS, !CMDEND, !CHAREND, and !ENCLOSE.
- The DEFINE statement contains duplicate subcommands in the parameter definition (e.g., more than one !TOKENS).
- The DEFINE statement contains subcommands that conflict with parameter definitions (e.g., !TOKENS and !CHAREND).
and in the !DO section:
- The first word in the !DO line is not recognized as an SPSS Statistics command.
I don't know well about SPSS commands, so I have no idea how to fix the error (GPT doesn't either). Can you please share me your knowledge?
Jon Peck's profile image
Jon Peck IBM Champion

I don't have time to go through all the macro code, but here are two tips that might help.

First, when working with macros, run SET MPRINT ON.  This will show the syntax after expanding the macro.  That  probably won't help with the invalid DEFINE statement, but it will help diagnosing issues with the macro expansion.

Second, you can use SPLIT FILES to handle the iteration over the different city_id groups instead of using a macro for that..  Look at the Data > Split Files or the SPLIT FILES doc in the Command Syntax Reference.