spss

Dynamically remove columns with no variance in syntax


I want to a run a range of algorithms in SPSS that require input variables to have some variance. I want to be able to dynamically select variables in SPSS that have a regular expression AND do not have variance equal to 0. I often define variable lists using spssinc select variables, using regular expressions. Is there a way to extend this to only select variables that have variance not equal to zero?

Below is an example dataset. As you can see the column named Test_price01 has variance of 0. Can this column be dropped from the dataset (or is there another way to not select this column?)?

data list list/ID (A3) Sex (A1) Age (F2.0) Education (A5) Test_price01 Test_new01 Test_income01 Test_exp01 Test_01 Test_house01 Test_car01 Test_boat01 Test_var01 Test_var02 .
begin data
    ID1 M 20 Prim 1 2 3 4 5 6 7 8 9 9
    ID2 F 22 High 1 4 3 6 3 8 1 2 5 8
    ID3 M 30 High 1 8 6 4 2 1 3 5 7 9
end data.
dataset name survey.

Below is how I usually dynamically select variables

spssinc select variables macroname="!Test_Vars" /properties pattern=".*Test_".

Solution

  • I don't know of a direct way to do this in SPSS, but the following syntax gives you a way to automate syntax creation according to analysis results - you capture the results from the output through the OMS functionality, then create a new syntax based on the results, and run it:

    * this is your example with a couple of zero variance variables added.
    data list list/ID (A3) Sex (A1) Age (F2.0) Education (A5) Test_price01 Test_new01 Test_income01 Test_exp01 Test_01 Test_house01 Test_car01 Test_boat01 Test_var01 Test_var02 .
    begin data
        ID1 M 20 Prim 1 2 3 4 5 6 7 8 9 9
        ID2 F 22  High 1 4 3 6 3 8 7 2 9 8
        ID3 M 30 High 1 8 6 4 2 1 7 5 9 9
    end data.
    dataset name survey.
    
    DATASET DECLARE  stds.
    OMS  /SELECT TABLES
      /IF COMMANDS=['Descriptives'] SUBTYPES=['Descriptive Statistics']
      /DESTINATION FORMAT=SAV  OUTFILE='stds' .
    desc Test_price01 to Test_var02.
    omsend.
    
    dataset activate stds.
    select if Std.Deviation=0.
    write out="yourpath\del vars.sps"/"delete variables ", Var1, "." .
    exe.
    
    dataset activate survey.
    insert file="yourpath\del vars.sps".