* Content of Jon Peck's email to the SPSSX-List on 2004/02/13.
Subject: Scripts for Switching between short and long variable names
Here are two lightly tested scripts with accompanying syntax to move between
short and long variable names of SPSS 12.
The first one uses a SAVE command to generate the short names.
The second one uses the output of EXPORT to reconstruct the long names on IMPORT.
In both scripts, change the name and location of the OMS output as
needed by editing the definition of the script variable FILENAME
First, here is a way to go from long names to short ones.
Note that these are not necessarily exactly what SPSS 11.5 and earlier
would have produced when data were read in from a non-SPSS source
because of issues with different truncation rules that were used in
different places in SPSS and because things that happen during a
session could change the result of the shortening algorithm.
The first step is to do a save that displays the short and long forms
of names when they are different. OMS is used to capture the mapping table.
oms select log
/destination format=oxml outfile='e:\temp\namelist.xml'.
save outfile='e:\temp\fred.sav' /names.
omsend.
The second step is to run this script, which I call RenameLongToShort.sbs.
Be aware that email may introduce line wrapping that needs to be undone to
make this code valid. For repeated use of the same external source,
you could extract the SPSS syntax from the log after running this script
and incorporate it in your job.
------ start of script
Sub Main
'BEGIN DESCRIPTION
'This script presumes that a SAVE command with /NAMES has written a correspondence table to
'a file in OMS XML format. It renames all variables with long names to short ones.
'END DESCRIPTION
Dim theline As String
Dim shortname As String, longname As String, startloc As Long, endloc As Long,
On Error GoTo error_rename
Const FILENAME ="e:\temp\namelist.xml" ' change this as appropriate
Const STARTPATTERN="---------- ------------- "
Const ENDPATTERN= " "
Const STARTLINE = ""
Const ENDLINE = ""
Open FILENAME For Input As #1
Line Input #1, theline
startloc = InStr(theline, STARTPATTERN)
If (startloc = 0) Then
GoTo exit_rename
End If
theline = Mid(theline, startloc+ Len(STARTPATTERN))
Do
endloc = InStr(theline, ENDLINE)
shortlong = Mid(theline, Len(STARTLINE)+1, endloc - Len(STARTLINE)-1) 'short and long name'
theline = Mid(theline, endloc + Len(ENDLINE))
shortname = Left(shortlong, InStr(shortlong, " "))
longname = Mid(shortlong, Len(shortname)+1)
objSpssApp.ExecuteCommands("RENAME VARIABLES " & longname & "=" & shortname & ".", False)
Loop Until Left(theline, Len(ENDPATTERN)) = ENDPATTERN
exit_rename:
Close #1
Exit Sub
error_rename:
MsgBox(Err.Description)
GoTo exit_rename
End Sub
--------- end of script
Second, here is a way to go from short names to long ones when using the portable file.
Save the portable file via syntax and capture its renaming table:
oms select log
/destination format=oxml outfile='e:\temp\exportnamelist.xml'.
export outfile='e:\temp\fred.por' .
omsend.
Then after the IMPORT, run the following script, which I call RenamePortShortToLong.sbs. It is almost identical to the previous script but does the renaming in the reverse direction.
-------- start of script
Sub Main
'BEGIN DESCRIPTION
'This script presumes that an Export command has written a correspondence table to
'a file in OMS XML format. It renames all variables with short names to the original long ones.
'END DESCRIPTION
Dim theline As String
Dim shortname As String, longname As String
On Error GoTo error_rename
Const FILENAME ="e:\temp\exportnamelist.xml" ' change this as appropriate
Const STARTPATTERN="Abbreviated ExtendedName Name "
Const ENDPATTERN= "omsend."
Const STARTLINE = ""
Const ENDLINE = ""
Open FILENAME For Input As #1
Line Input #1, theline
startLoc = InStr(theline, STARTPATTERN)
If (startloc = 0) Then
GoTo exit_rename
End If
theline = Mid(theline, startloc+ Len(STARTPATTERN))
Do
endloc = InStr(theline, ENDLINE)
shortlong = Mid(theline, Len(STARTLINE)+1, endloc - Len(STARTLINE)-1) 'short and long name'
theline = Mid(theline, endloc + Len(ENDLINE))
shortname = Left(shortlong, InStr(shortlong, " "))
longname = Mid(shortlong, Len(shortname)+1)
objSpssApp.ExecuteCommands("RENAME VARIABLES " & shortname & "=" & longname & ".", False)
Loop Until Left(theline, Len(ENDPATTERN)) = ENDPATTERN
exit_rename:
Close #1
Exit Sub
error_rename:
MsgBox(Err.Description)
GoTo exit_rename
End Sub
----------end of script