SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  Recoding string variable in to a new numeric variable

    Posted Mon December 14, 2020 11:47 AM
    I have a large dataset where I need to:

    1. Recode 40 string variables to individual new numeric variables concatenating at least 20 different codes with varying character lengths by their first 3 characters such as 'E10', E11' for 2 of these codes and convert them as 1, 2 and rest as 0 - plus account for missing values.
    2. Identify all 1's and 2's for converted variables and transform them to a single new numeric variable for individual subjects. 
    Any help will be much appreciated.

    ------------------------------
    ZKhan
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Recoding string variable in to a new numeric variable

    Posted Mon December 14, 2020 12:35 PM
    Edited by System Admin Fri January 20, 2023 04:15 PM
    1. Ordinarily, AUTORECODE is used to transform strings to numerics. I'd have to experiment with the data and see but I don't believe that DO REPEAT will directly do what you want.
    2. For the second, it sounds to me like a case for the COUNT command.

    The documentation has help for AUTORECODE, DO REPEAT and COUNT.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM SPSS Statistics
    Chicago
    ------------------------------



  • 3.  RE: Recoding string variable in to a new numeric variable

    Posted Mon December 14, 2020 02:10 PM
    I have a simple 2-variable example here. You can expand it to 40 (or however many) variables in your own case.

    *My own sample data. You have your own "a" variables.

    data list free /a1 a2 (2a32).
    begin data.
    E10VALUES E11ANDMORECHARACTERS
    E12STUFF E10VALUE
    end data.

    *You would change "x1 to x2" in the NUMERIC and VECTOR commands below to "x1 to x40" for your own data. The VECTOR command for "#a" would be your own existing string variables.

    numeric x1 to x2 (F2).
    vector x=x1 to x2.
    vector #a=a1 to a2.

    *The LOOP command would change to 1 to 40.

    loop #i=1 to 2.
    compute x(#i)=0.
    if substr(#a(#i),1,3)="E10" x(#i)=1.
    if substr(#a(#i),1,3)="E11" x(#i)=2.
    end loop.

    execute.

    Then you could use the COUNT command to create new variables that count any target value(s) of your "x" variables. See help for COUNT for details.​

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------