Standardizing SPSS file import- Greatly simplify opening text files

spss file

SPSS File import

Importing “wide” (lots of variables)  data in SPSS can be tedious and time consuming!  Especially when there are lots of string and numeric variables having different widths which you plan to use / merge with additional files (String variables need to be the same width).

I wrote an AutoHotKey script to automate setting the width and decimal places plus align string and numerics to make it easier to differentiate between them.   It sounds kind of crazy but, if you watch the following video, you’ll see how much easier the import SPSS file import is to work with.

 
#SingleInstance, Force
#NoEnv

;~ RAlt::
;~ Browser_Forward::Reload
;~ RControl::
;~ Browser_Back::
;**********************set width of standard *********************************
InputBox, A_Length,Default String width, How wide do you want all String variables to be set?,,,,,,,,999
InputBox, F_Format,Default Numeric width,How wide & decimals do you want all NUMERIC variables to be set?,,,,,,,,6.1
;~ A_Length:="999"
;~ F_Format:="6.0"

Store:=Clipboard ;preserve clipboard
clipboard:="" ; Empty the clipboard
  Send, ^c
  ClipWait, 1
  var:=clipboard
;****run through items finding lenght of longest named variable**********************
loop, parse, var, `n, `r 
{
	RegExMatch(A_loopfield, "\s+(\w+)\s",len,1)
	if StrLen(len1) > Max_Length
		Max_Length:=StrLen(len1)+3
}  ;MsgBox % max_Length

;**perform Regex on List- replace A length and F length*********************************
var:=RegExReplace(Var,"\s(A\d{1,2})","   A" . A_Length)
var:=RegExReplace(Var,"\s(F\d{1,2}\.\d)","   F" . F_Format)
;~ MsgBox % Clipboard:=var  ;preview variables for SPSS file import
;*****Now go through and pad AXXX and FXXX values*********************************
loop, parse, var, `n, `r
{
	RegExMatch(A_loopfield, "\s+(\w+)\s+(\w.*)",len,1)
	IfInString, len2,A
		NewLength:=Max_Length - StrLen(len1) 
	
	IfInString, len2,F
		NewLength:=Max_Length - StrLen(len1)+5
NewRow.= "  " . len1 Pad_Var(NewLength," ") . len2 . "`r`n" ;roll them back up into syntax
} 
Clipboard:=NewRow . "`r`nVar width All (15)."
Send, ^v ;paste to screen
Clipboard:=Store ;restore clipboard
Return

;**********************pad with characters*********************************
Pad_Var(N,Char){
pad:=""
	loop, %N%
		pad.=Char
;~ MsgBox % pad
Return pad
}
;end SPSS file import

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.