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
' 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

' Modifications by Ray done on 2003/05/19 to read text files (with extension .u77) and combine
' them in a single sav file. The variable fname contains the name of the initial u77 file.

' 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		="F:\\heide\\compact\\rias data\\"
	strFilemask	="*.U77"
	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)
	Call GetFile(strCmd,strPath,strFname)
	strCmd = "COMPUTE source=1." & vbCr
	strCmd = strCmd & "ADD VALUE LABEL source 1 " & " '" & strFname & "'." & vbCr
	strCmd = strCmd & "SAVE OUTFILE='" & strPath & "combined.sav'." & vbCr
	strCmd = strCmd & "FREQ VAR=ALL." & vbCr
	objSpssApp.ExecuteCommands strCmd , True

	'Combine the other files
	intFileNb = 2
    While strFname <> ""
		strFname = Dir$()
		If strFname <> "" Then
			Call GetFile(strCmd,strPath,strFname)
			strCmd ="FREQ VAR=ALL." & vbCr
			objSpssApp.ExecuteCommands strCmd , True

			strCmd = "ADD FILES /FILE='" & strPath & "combined.sav' /FILE=* ." & vbCr
			strCmd = strCmd & "IF MISSING(source) source=" & intFileNb & "." & vbCr
			strCmd = strCmd & "ADD VALUE LABEL source " & intFileNb & " '" & strFname & "'." & vbCr
			strCmd = strCmd & "EXECUTE." & vbCr
			strCmd = strCmd & "SAVE OUTFILE='" & strPath & "combined.sav'." & vbCr
			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

Sub GetFile(strCmd As String, strPath As String, strFname As String )
	Dim strFileExtension As String
	strFileExtension=LCase(Right(strFname,3))

	Select Case strFileExtension
		Case "sav"
			strCmd = "GET FILE='" & strPath & strFname & "'." & vbCr
		Case "u77"
			strCmd="Get DATA  /Type = TXT" & vbCr
	 		strCmd=strCmd & " /FILE ='" & strPath & strFname & "'" & vbCr
	 		strCmd=strCmd & " /FIXCASE = 1 /ARRANGEMENT = FIXED /FIRSTCASE = 32" & vbCr
	 		strCmd=strCmd & " /IMPORTCASE = All /VARIABLES =" & vbCr
	 		strCmd=strCmd & " /1  V1 0-3 F4" & vbCr
	 		strCmd=strCmd & " V2 4-6 A3" & vbCr
	 		strCmd=strCmd & " V3 7-8 F2" & vbCr
	 		strCmd=strCmd & " V4 9-21 A13" & vbCr
	 		strCmd=strCmd & " V5 22-25 F4" & vbCr
	 		strCmd=strCmd & " V6 26-26 F1" & vbCr
			strCmd=strCmd & " V7 27-35 A9" & vbCr
			strCmd=strCmd & " V8 36-36 F1" & vbCr
			strCmd=strCmd & " V9 37-45 A9" & vbCr
			strCmd=strCmd & "  V10 46-48 F3." & vbCr

			strCmd = strCmd & "STRING fname(A15)." & vbCr
			strCmd = strCmd & "COMPUTE fname=" & Chr$(34) & strFname & Chr$(34) & "." & vbCr
			strCmd = strCmd & "VARIABLE LABEL source 'path=" & strPath & "'." & vbCr
			strCmd = strCmd & "Execute."
			objSpssApp.ExecuteCommands strCmd , True

		Case Else
			MsgBox ("File type " & strFileExtension & " not defined" & _
				vbCr & "Script will exit",vbCritical)
	End Select
End Sub