Changing a variable's width is rarely necessary. Nevertheless, it's good to know how when it's needed and how it's done.
Changing Variable Width in SPSS
- For string variables, width refers to how many characters a value can hold. An exception are multibyte characters as explained in SPSS Unicode Mode.
- Somewhat confusingly, "width" is not the width of a variable's column as displayed on screen, which is referred to as columns.
- For string variables, width should be increased when new values are desired that are longer than the current width. This is demonstrated by the syntax example below.
- For numeric variables, "width" refers to how many digits should be displayed. However, SPSS will often override the specified width is it's insufficient. If not,
FORMATS
can be used for increasing it.
SPSS Formats and Alter Type Syntax Examples
(The test data used by the syntax below are found here.)
*1. Width for "income" increases from 5 to 6 after running the line below.
formats income(f6.0).
*2. "Stefano" (7 letters) is too long for "name" (6 letters). We'll therefore increase its width to 7 characters.
alter type name(a7).
*3. Now we can change "Stefan" to "Stefano".
if name eq 'Stefan' name = 'Stefano'.
execute.
formats income(f6.0).
*2. "Stefano" (7 letters) is too long for "name" (6 letters). We'll therefore increase its width to 7 characters.
alter type name(a7).
*3. Now we can change "Stefan" to "Stefano".
if name eq 'Stefan' name = 'Stefano'.
execute.
Changing Decimal Places in SPSS
- Decimal places can be changed by the
FORMATS
command. Just note that the first number refers to the width of the entire variable (including decimals) so the second number (decimals) should always be smaller. - Also, keep in mind that the actual data values will not change because of using
FORMATS
. They are merely displayed differently.
SPSS Formats Syntax Example
(The test data used by the syntax below are found here.)
*Note how running the line below displays two decimals for "income" under "Data View".
formats income(f5.2).
formats income(f5.2).
THIS TUTORIAL HAS 2 COMMENTS:
By Van on April 23rd, 2015
Hi Ruben,
This is also very good tutorials. I wonder though, how I can change Width and Columns in, say, 50 files, in one go? I have 50 files of data that have the same variables; however, their Width and Columns all differ. This lead to crazy data merging. How do I make sure they all have the same Width and Columns? Thanks a lot!
By Ruben Geert van den Berg on April 27th, 2015
The issue you describe only applies to the length (not columns) of string variables. Syntax for fixing this for many data files is given in adjust string lengths over files.
Before using it, do make sure that you have the SPSS Python Essentials properly running since the syntax requires these.