• Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey

Syntax for changing width of SPSS string variable

SPSS string variableUse Syntax to change the with of SPSS string variable

String variables in SPSS are still a pain to deal with and merging data files together with a key variable that is a string is NOT fun in SPSS.  The string variable needs to be the same width in each data file and needs to be sorted the same (ascending-descending).  I can’t tell you the number of times I’d curse the old format!

My below macro makes it a breeze to change the width of your SPSS string variables.  The process is pretty straight forward.

Years ago I established some defaults for variables that were frequently in my data sets like: email, First name, Last name, company name, Job title, Job Role, etc.  Surprisingly setting the width to wider than they are doesn’t actually take up any extra file size so, as a rule of thumb, I recommend doubling what you typically see (otherwise you’ll end up with truncated data)

Here is the macro I use which automates the process of changing the SPSS string text width.  🙂

 
*///resize variable////////////.
DEFINE !CNGWDTH (NAME !TOKENS (1) /WIDTH !TOKENS (1))
STRING NEWVAR (!WIDTH).
compute newvar=!name.
EXE.
DELETE VARIABLES !NAME.
RENAME VARIABLES NEWVAR=!NAME.
!ENDDEFINE.
*///////////////.

!CNGWDTH Name=email   Width=A65.

BTW- if you were reading this with the goal of changing the column width of variables in the data editor (think of Excel and how you resize the columns) the above macro has nothing to do with it.  Below is the code you’d use to change email to 30 wide; Country to 20 wide; and the rest to 6 wide.

 VARIABLE WIDTH ALL (6)  email (30) country (20).

Automating prep work for SQL in list query of hundreds-thousands of items

 

SQL in List

Often I have a list of hundreds (sometimes thousands) of items that I need to insert into a  query. Unfortunately I can only add 99 items at a time.

This video demonstrates how my script replaces the first 98 line breaks with commas and wrap items already having a comma with special characters to escape them.    This is perfect for my SQL in list query!  What would have taken me several minutes or longer is now done in the blink of an eye  (while also escaping illegal characters).  The below video demonstrates it’s usage and further down the page is the AutoHotkey code that cranks it out!

 

Video demonstrating how the SQL in list works

 

AutoHotkey code:

Converting Yes-No Strings to Numerics with an SPSS Macro

SPSS macro

Often I have strings in my SPSS data set which have values like: Y,Yes,yes,No,n, etc. and I want them to be a numeric value so I can take their mean. This SPSS macro makes converting them a breeze!  You might need to adapt it to your data set but the principle can be applied for many uses.

The below video demonstrates it usage.  Definitely saves a lot of time if o

SPSS macro

These SPSS macros should regularly categorize the string variables correctly.

 
*/////////////////////.
DEFINE !con2Num (vnames=!CMDEND)
!DO !vname !IN (!vnames)
If INDEX(Upcase(!Vname),Upcase("Y"))>0 Temp1234= 1 .
If INDEX(Upcase(!Vname),Upcase("N"))>0 Temp1234= 0 .
FORMAT temp1234(F3.0).
Val Labels Temp1234 0"No" 1"Yes".
Var level Temp1234 (Scale).
MATCH FILES FILE=* /DROP=!vname .
RENAME VARIABLE (temp1234=!vname).
!DOEND
exe.
!ENDDEFINE.
*/////////////////////.
!con2Num vnames=var1 var2 var3 .

*/////////////////////.
DEFINE !con2NumNA (vnames=!CMDEND)
!DO !vname !IN (!vnames)
If INDEX(Upcase(!Vname),Upcase("N"))>0 Temp1234= 0 .
If INDEX(Upcase(!Vname),Upcase("Y"))>0 Temp1234= 1 .
If INDEX(Upcase(!Vname),Upcase("N/A"))>0 OR INDEX(Upcase(!Vname),Upcase("NA"))>0 Temp1234= 2 .
FORMAT temp1234(F4.0).
Var level Temp1234 (Nominal).
Val Labels Temp1234 0"No" 1"Yes" 2"NA".
MATCH FILES FILE=* /DROP=!vname .
RENAME VARIABLE (temp1234=!vname).
!DOEND
exe.
!ENDDEFINE.
*/////////////////////.

!con2NumNA vnames=var1 var2 var3 .

Here are some convenient references to learn more.

Learning SPSS Macros | Raynald’s SPSS Tools

This page is a simple introduction. There are several fully (line- by-line) commented examples of macro

Text file explorer-Determine file headers & delimiters without opening file

Text file explorer

Text File ExplorerText file explorer

I often work with large text files in which the file extension (.txt, .dat, .csv, .tab) doesn’t always indicate what type of delimiter is used in the file.  When the file is small, I’ll typically just “pop” it open in SciTE / Notepad.  Large files (anything over 20 megs) often take a fair amount of time to read and very large files ( a gig or more) will often run into out of memory issues.

On top of wanting to know the delimiter, I also frequently want to know what fields / Headers are in the file.   This normally means I have to open it in a text editor or Excel and review.  I wrote the below Text File Explorer in AutoHotkey script to simplify the above.  I can highlight a file from Windows Explorer and quickly detect the type of delimiter plus display headers if I care to.

Text file explorer AutoHotkey code:


Here’s a video demonstrating the usage of the Text file Explorer

  • Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey