How to convert numeric to string in SPSS

Deepanshu Bhalla Add Comment
Suppose you have a numeric variable type data and you wish to convert it to string.

Example
Go to File > New > Syntax . In SPSS Syntax Editor, paste the following syntax :

Result
Code            Code1
23464.25     23464.25
2134.27       2134.27
.00               0
Number of cases read:  3    Number of cases listed:  3

Explanation
1. DATA LIST defines a raw data file . The keyword FREE indicates multiple cases can be entered on a single row (freefield format).
2. Variable definition begins after the slash. Code is the variable name and A12 indicates string variable with width 12
3. The BEGIN DATA and END DATA commands enclose the raw data.
4. STRING(numeric type variable, format) returns the value of the numeric expression as a string. For example, STRING(23464.25, F10.2) returns the string value '23464.25'. F10.2 indicates numeric variable with width 10 and 2 decimals. 
5. SUBSTR(string variable, position, [number of characters]) returns the substring beginning at character position of string variable.The third parameter is optional. For example SUBSTR('23464.25', 1,4) returns '2346'
6. RINDEX(within_text, find_text, [number of characters used to divide find_text into separate strings]) returns a number that indicates the starting character position of the last occurence of the find text. For example, RINDEX('23464.25', "123456789",1) returns 10
7. LTRIM removes leading spaces.

String conversion into the same variable
Add the following lines of code after EXECUTE statement.
DELETE VARIABLES Code.
RENAME VARIABLES (Code1 = Code).
LIST .

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "How to convert numeric to string in SPSS"
Next → ← Prev