Combine all .sav files in the given folder.sbs
You have to supply only file path and mask, script does the rest. The resulting sav file contains a variable identifying the source file of each case.
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 | ' Demo: this script combines all sav files (meeting a given mask eg *.sav) located in a given folder ' The sav files will normally have the same variables (but it works even if they don't) ' The label of the variable source contains the path of the data files ' The value labels of the variable source contain the name of the source data files ' rlevesque@videotron.ca 2001/10/2 ' Visit my SPSS site http://pages.infinit.net/rlevesqu/index.htm Option Explicit Sub Main Dim strPath As String Dim strFilemask As String ' Modify the next 2 lines to fit your requirements strPath ="c:\\temp\\" strFileMask ="f*.sav" Call CombineDataFiles(strPath, strFileMask) End Sub Sub CombineDataFiles (strPath As String, strFileMask As String) Dim strFname As String Dim strCmd As String Dim intFileNb If InStr(strPath, "\\") = 0 Then 'no path given, use current folder strPath = objSpssApp.GetSPSSPath End If If Len(strFileMask) = 0 Then 'no file Mask given, combine all files strFileMask = "*.sav" End If 'Get the first file name and load file strFname = Dir$(strPath & strFileMask) strCmd = "GET FILE='" & strPath & strFname & "'." & vbCr strCmd = strCmd & "COMPUTE source=1." & vbCr strCmd = strCmd & "VALUE LABEL source 1 " & "'" & strFname & "'." & vbCr strCmd = strCmd & "VARIABLE LABEL source 'path=" & strPath & "'." & vbCr strCmd = strCmd & "EXECUTE." objSpssApp.ExecuteCommands strCmd , True 'Combine the other files intFileNb = 2 While strFname <> "" strFname = Dir$() If strFname <> "" Then strCmd = "ADD FILES /FILE=* /FILE='" & strPath & strFname & "'." & vbCr strCmd = strCmd & "IF MISSING(source) source=" & intFileNb & "." & vbCr strCmd = strCmd & "ADD VALUE LABEL source " & intFileNb & " '" & strFname & "'." & vbCr strCmd = strCmd & "EXECUTE." Debug.Print strCmd objSpssApp.ExecuteCommands strCmd , True intFileNb = intFileNb + 1 End If Wend ' Save the combined file strCmd = "SAVE OUTFILE='" & strPath & "combined file.sav'." & vbCr strCmd = strCmd & "EXECUTE." objSpssApp.ExecuteCommands strCmd , True End Sub |
Related pages
...