Date: Thu, 2 Sep 1999 13:27:47 -0500
Reply-To: "Tverdek, Edward" <etverdek@SPSS.COM>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Tverdek, Edward" <etverdek@SPSS.COM>
Subject: Re: Changing Variable type
Dale,
The FORMATS command can be used to change print and write formats of
a variable and, in some cases, the variable type, but it cannot be used
to affect one of the more commonly needed conversions in variable type:
changing a string variable to its numeric equivalent. In the event that
this is what Dwight had in mind, I've appended below a note covering some
of the available command options.
Ed Tverdek
Training Coordinator
SPSS Technical Support
____________________________________________________________________________
Q.
I'm using SPSS for Windows. In the past, I've been able to
convert variables I have stored in a string format to numeric
variables simply by opening a Define Variable menu and
checking the appropriate option under the Type button. SPSS
echoes in my Output Viewer (or Navigator, or Window) a FORMATS
command specifying my string variable name and a format of F8.
Now I'd like to do the same thing in an SPSS command file. Yet
when I include the very command SPSS claims to have used in
converting my previous variable, I get a message saying
FORMATS strngvar (f8).
>Error # 4836 in column 9. Text: STRNGVAR
>Only the formats A and AHEX can be applied to string
variables.
>This command not executed.
If this command works when SPSS changes the format in the Define
Variable menu, why doesn't it work when I invoke it through
command syntax?
A.
When SPSS converts a variable from a string to numeric format
(or from any format to another) through the Define Variable menu,
it executes a process in the Data Editor that is not driven
exclusively by SPSS commands. This process allows SPSS to convert
invisibly a variable format while leaving it in the same place
in the data file. The FORMATS command is eventually applied to the
new variable, but this is the only portion of the process executed
via SPSS commands, and hence is the only command that shows up in
the log and in the journal file. It is a "new" variable in so far
as all other dictionary information attached to the previous variable
is lost, but it otherwise assumes the position in the file of the
previous variable.
You can compute a new numeric variable from the original string
variable through SPSS command syntax using either of two routes:
RECODE strngvar (CONVERT) INTO numvar.
or
COMPUTE numvar=NUMBER(strngvar,Fw.d).
Substitute for "Fw.d" in the second example the numeric format
you want to use to read the string variable. Once numvar is
computed through either means, you can use a FORMATS command
to format it for display, as does the routine executed via the
Define Variable menu.
This method will not, as does the Define Variable menu option,
replace the original string variable with a new numeric
variable of the same name. It merely adds the new variable to
the end of the file and leaves the original string intact. If
you have no further use for the original string variable, you
can specify it on a DROP subcommand the next time you execute
the SAVE command.
If, for whatever reasons, you would like to emulate in SPSS
command syntax what the menu option does, you can MATCH the
file to itself, specifying the same variable order as found in
the current file but substituting the numeric variable name for
the original string one, and then execute a RENAME VARIABLES
command which converts the numeric variable name to the original
string name. None of this is necessary to make use of the new
numeric variable, and need only be done if there is an overriding
need to do exactly what the Define Variable menu does when it
converts a variable format. Below is a brief sample job which,
when run, illustrates this procedure, converting v3 to a numeric
variable and situating it in the active file where its string
counterpart formerly existed.
DATA LIST/v1 to v4 1-4 (a).
BEGIN DATA
1234
3456
2345
END DATA.
COMPUTE numvar=NUMBER(v3,F1).
MATCH FILES FILE=*
/KEEP v1 v2 numvar v4.
RENAME VARIABLES (numvar=v3).
EXE.
-----Original Message-----
From: Daley, Henry [mailto:HDaley@DMHMRSAS.STATE.VA.US]
Sent: Thursday, September 02, 1999 1:12 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: Re: Changing Variable type
With the FORMAT command you can change variable type.
For instance,
FORMAT client_id (F8).
formats a variable as a numeric var with 8 digits.
FORMAT client_id (A8).
formats a variable as a string var with 8 characters.
When you do foramt changes with the drop down menus, the concomitant syntax
should be appearing in your output file.
> ----------
> From: Harley, Dwight[SMTP:HarleyD@ECS.EDMONTON.AB.CA]
> Reply To: Harley, Dwight
> Sent: Thursday, September 02, 1999 9:32 AM
> To: SPSSX-L@LISTSERV.UGA.EDU
> Subject: Changing Variable type
>
> Given an SPSS data file, is there a way using syntax perhaps, to change
> the
> variable type of a large number of variables? It is tedious process to
> double click on each variable name and the type button to make this change
> for 20 or 30 variables.
>