Suppose you have a numeric variable type data and you wish to convert it to string.
Example
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).
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 .
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 .
Share Share Tweet