Variable Formats in SPSS Syntax

by Karen Grace-Martin

Share

One of the places that SPSS syntax excels at efficiency is when you’re creating new variables.  This is especially true when you’re creating a LOT of new variables, but even one or two can be quicker if you write the syntax code instead of menus.

And just as importantly, you’ll have documentation for exactly how you created them. (You think you’ll remember now, but 75 new variables later, you’ll thank me).

So once you create a new variable, you should of course immediately assign a Variable Label, and if appropriate, Value Labels and Missing Data Codes using Syntax.

Another thing that helps keep your new variable clean and interpretable is to assign the format.  The default format is F8.2, which indicates a numerical value

You could go into the Variable View screen and manually change the Width and Decimals columns, which indicate how many characters go before and after (for numeric variables) the decimal point.

But why do all that when you can just use a single command to define multiple variables?

The syntax command is FORMATS.  Here is the command for some common formats:

FORMATS NumVar1 NumVar2 (F5.0)
/NumVar3 (F6.1)
/StringVar1 (A15).

You can see the FORMATS command is followed by the variable names, then the format in parentheses.

Numeric variables NumVar1 and Numvar2 will both get the same format: with 5 digits, and nothing after the decimal.

Numeric variable NumVar3 will have 6 digits total, with one after the decimal.

And string variable (i.e. its value contain letters) StringVar1 is 15 characters wide.

This will get you started, but you can get all the specifics in the FORMATS section of the  Command Syntax Reference, which is included in the SPSS help.

[Note: Edited explanation of F6.1 to be 6 digits total, not 6 digits before the decimal).

Want to learn more? If you’re just getting started with data analysis in SPSS, or would like a thorough refresher, please join us in our online workshop Introduction to Data Analysis in SPSS.

{ 9 comments… read them below or add one }

Ryan

Hi,

What should I do if I run a syntax and get error #100 and #105?

Thanks

Reply

Rachel

Great post! Now… I need to change the format (variable length) for an existing varible (LastName) and it’s giving me an error on the FORMATS command:

>Warning # 4837 in column 24. Text: LastName
>The width of a string format must match the width of the string variable. > The correct width has been substituted.

I’m trying to merge two files but the lengths have to be the same, and I would love not to change it by hand all the time.

Thanks!!

Reply

Lisa

Hi Rachel,
I had the same problem, this is what the SPSS tutorial says:
“For string variables, you can only use FORMATS to switch between A and AHEX formats. FORMATS cannot be used to change the length of string variables. To change the length of a string variable, declare a new variable of the desired length with the STRING command and then use COMPUTE to copy values from the existing string into the new variable.”
I suppose you are done formatting, but I post this for the next person looking for an answer to this question…
Greetings, Lisa

Reply

rd

Karen,

Love your writing style. Just one small comment on the sentence below:

“Numeric variable NumVar3 will have 6 digits before and one after the decimal.”

The format f6.1 means that the var is a total of 6 characters wide including the sign, the decimal point and the decimal place.

Keep up the great work!

Reply

Karen

Thanks, RD. I fixed it.

Reply

Fatih

Hi,
In crosstab analysis I can format a 0/1 row variable in a desending order. Can I do the same for a column variable?

Reply

Karen

Good question. I have no idea. Anyone else know?

Reply

Jagadish Soni

Hi,

I want to change variable width for 16 files contain 25 variables. Please suggest syntax for this. I used following syntax which are not working.

1): Variable width /var1 (6)

2): Data list /var1 (6).

Jagdish

Reply

Karen

Hi Jagdish,

I don’t know off the top of my head, and it would take me a while to figure it out. I would suggest spsstools.net. There are many good spss program examples there.

Karen

Reply

Leave a Comment

Please note that, due to the large number of comments submitted, any comments on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to answers and more resources 24/7.

Previous post:

Next post: