SPSS Statistics

 View Only

SPSS Macro for splitting a variable into multiple separate variables

  • 1.  SPSS Macro for splitting a variable into multiple separate variables

    Posted Fri March 22, 2024 06:25 AM


    I need assistance with an SPSS macro and hope someone here can help me.

    In my survey, a set of questions was asked about a variable text display for different brands (e.g. Audi, BMW,...). For example, the question was: How satisfied are you with the quality of [main brand]?

    The results were each written into a variable (here F309_1 to F309_14). However, in the data set I would like to have the values ​​of these basic variables transferred to individual, brand-specific variables.

    I wrote the following macro for this.

    DEFINE markenmacro (AnzahlMarken !TOKENS(1) / Markenvariable !TOKENS(1) / Basisvariable !TOKENS(1) / MaxItems !TOKENS(1))

    !DO !i = 1 !TO !AnzahlMarken.

    DO IF !Markenvariable = !i.

    RECODE !concat(!Basisvariable,_,1) to !concat(!Basisvariable,_,!MaxItems) (else = copy) into !concat(!Basisvariable,!i,_,1) to !concat(!Basisvariable,!i,_,!MaxItems).

    ELSE IF !Markenvariable ne !i.

    RECODE !concat(!Basisvariable,_,1) to !concat(!Basisvariable,_,!MaxItems) (else = 99) into !concat(!Basisvariable,!i,_,1) to !concat(!Basisvariable,!i,_,!MaxItems).

    end if.



    markenmacro AnzahlMarken = 30 Markenvariable = F308 Basisvariable = F309  MaxItems = 14.



    *AnzahlMarken= Number of brands

    *Markenvariable= Variable containing the queried brand

    *Basisvariable= Variable to be split into separate variables

    *MaxItems = Number of items of the base variable.

    And the macro actually works. As a result, I get the variables F3091_1 to F3091_14 for brand 1, F3092_1 to F3092_14 for brand 2, etc.

    Now, however, the result variables should not be named with numbers but with letters. So it should be F309a_1 to F309a_14, F309b_1 to F309b_14 etc.

    And at this step I am stuck. I would need to include a loop that goes through the letters from 'a' to 'xyz' in addition to the brand loop and selects the next letter each time. But I have no idea how to implement this in a macro.

    Does anyone here have a solution for me?

    Thank you very much in advance and best regards,