Scripts for Switching between short and long variable names
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | * 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="<line>---------- -------------</line><line> </line>" Const ENDPATTERN= "<line> </line>" Const STARTLINE = "<line>" Const ENDLINE = "</line>" 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="<line>Abbreviated Extended</line><line>Name Name</line><line> </line>" Const ENDPATTERN= "<line>omsend.</line>" Const STARTLINE = "<line>" Const ENDLINE = "</line>" 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 |
Related pages
...
Navigate from here