chipsin
chipsin

Reputation: 675

Dynamically name new aggregated variables in SPSS

I have a large dataset I have to aggregate using numerous functions (e.g. max, min, mean, etc.).

All variables have similar prefixes. I can call these dynamically using spssinc select variables and then aggregate using the code below.

spssinc select variables macroname="!vars_prefix" /properties pattern="prefix*.".

DATASET DECLARE Test.
SORT CASES BY ID_No.
AGGREGATE
  /OUTFILE='Test'
  /PRESORTED
  /BREAK=ID_No
  /!vars_prefix=MEAN(!vars_prefix).

I am unsure how to do this for multiple functions and apply the function name as a suffix in the aggregated output. I imagined something like below would have worked (but alas it didn't). Is this possible or a limitation of SPSS Statistics?

DATASET DECLARE Test.
SORT CASES BY ID_No.
AGGREGATE
  /OUTFILE='Test'
  /PRESORTED
  /BREAK=ID_No
  /CONCAT(!vars_prefix, '_Mean')=MEAN(!vars_prefix)
  /CONCAT(!vars_prefix, '_Max')=MAX(!vars_prefix).

Upvotes: 1

Views: 168

Answers (1)

eli-k
eli-k

Reputation: 11310

The following macro loops through the list of variables you created and adds a suffix to each. To use the macro you need to add the suffix in parentheses after the macro call.

define !add_suffix (!pos=!enclose("(",")"))
    !do !vr !in (!eval(!vars_prefix)) !concat(!vr, !1) !doend
!enddefine.

After defining the macro you can call it with a new suffix wherever you need. For example:

DATASET DECLARE Test.
SORT CASES BY ID_No.
AGGREGATE
  /OUTFILE='Test'
  /PRESORTED
  /BREAK=ID_No
  /!add_suffix (_mean)=MEAN(!vars_prefix)
  /!add_suffix (_max)=max(!vars_prefix)
  /!add_suffix (_min)=min(!vars_prefix).

Another example:

dataset activate Test.
do repeat  rng=!add_suffix (_range) / mx=!add_suffix (_max) / mn = !add_suffix (_min)  .
compute rng = mx - mn .
end repeat.
exe.

Upvotes: 1

Related Questions