SPSS Statistics

SPSS Statistics

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

 View Only
Expand all | Collapse all

How to convert from string to numeric preserving fractions?

  • 1.  How to convert from string to numeric preserving fractions?

    Posted Tue October 13, 2020 03:58 PM

    I have a column of string vars including decimals (eg 0.5) as well as values like <0.1. I want to make this var into a numeric var.

    I tried this syntax:

    compute VAR2 = number(VAR1, F2).

    execute.

    But the values less than one are returned as 0. The values that include a symbol (eg '<0.1') are also not computed. Is there a way to convert this var into numeric that includes values less than 0, and also converts values like '<0.1' to 0.1?






    #SPSSStatistics
    #Support
    #SupportMigration


  • 2.  RE: How to convert from string to numeric preserving fractions?

    Posted Tue October 13, 2020 10:19 PM

    Hi Support Member​ 

    You are on the right track. This may be as simple as changing your desired format:

    NUMERIC var2 (F4.2).

    COMPUTE var2 = NUMBER(var1, F4.2).

    EXECUTE.

     

    Your '<0.1' values present a different problem. The presence of the '<' makes these values literally a string. There is no straight conversion for that other than to change the value. Do you also have '>0.1' values? How do you want to treat these?

     

    You might use the RECODE command to change these strings into some other string that would be easy to then convert to a number. Look also at the RECODE command with CONVERT keyword.

    https://www.ibm.com/support/knowledgecenter/SSLVMB_27.0.0/statistics_reference_project_ddita/spss/base/syn_recode.html

     

    You might want to use the RTRIM() and LTRIM() functions on a COMPUTE command to strip the spaces and/or other text characters prior to converting to a number.

    https://www.ibm.com/support/knowledgecenter/SSLVMB_27.0.0/statistics_reference_project_ddita/spss/base/syn_transformation_expressions_string_functions.html#syn_transformation_expressions_string_functions

     

    I hope this helps!

    Support Member​ 

     

     






    #SPSSStatistics
    #Support
    #SupportMigration