SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  Merging all tabs from Excel data

    Posted Mon May 02, 2022 09:32 AM
    I am trying to import an Excel spreadsheet that has 25+ tabs of related data into one merged data file in SPSS. Can anyone tell me the easiest way to automate this in SPSS without having to import data from each tab separately? I would prefer not to copy and paste each time I do this. Thank you!

    ------------------------------
    Rosy Chang Weir
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Merging all tabs from Excel data

    Posted Mon May 02, 2022 09:46 AM
    Hi. I think Jon Peck has written an extension procedure that does this and a variety of other things. It's called SPSSINC_PROCESS_FILES. Check  the Extension Hub. Start here if you aren't in the program - like I'm not right now... Extension Hub
    Ibm remove preview
    Extension Hub
    View this on Ibm >


    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: Merging all tabs from Excel data

    Posted Mon May 02, 2022 10:06 AM
    That extension iterates over a list or wildcard-specified set of files, so you would need to split the tabs into separate files first, so it might be easier just to repeat the import syntax naming each tab and then merge them with a single ADD CASES command.

    One potential hitch, however, is that if you have string variables where the variable size might vary from tab to tab, ADD CASES won't work (STAR JOIN might).  There is an extension command, STATS ADJUST WIDTHS, that can synchronize the widths of a batch of sav files in preparation for merging. 

    If you are adding different variables, i.e., MATCH FILES, this wouldn't be a problem.

    If you just want to specify a list of tab names, a macro or short Python program could simplify the import and merge code.

    --





  • 4.  RE: Merging all tabs from Excel data

    Posted Mon May 02, 2022 12:27 PM
    Here is a pretty automated solution.  The attached code opens all the sheets in
    the specified Excel file and merges them.

    First, you need to go to Extensions > Extension Hub and install the extension
    command STATS PACKAGE INSTALL.

    Then, run the first command in the attached file, which uses this extension, to add a Python library module that can read Excel files. You only need to do that once.

    In the code that follows, change this line
    filename=r"c:/temp/twosheets.xlsx"
    to specify the name of the file you want to import.  Be sure to preserve the r in front of
    the quoted text.

    Then select everything from begin program through end program and execute it.
    It will read in all the sheets in the Excel file and merge them and then close all of them
    leaving just the merged file.

    There are two important qualifications.  In the Excel sheets, the column names, which
    will become variable names, must be legal SPSS variable names, and they should all
    be distinct.  And the sheet names must also be legal as SPSS variable names except
    that any blanks will be ignored.

    This code should work with SPSS 27 or later.

    Let me know how this works for you.

    --





  • 5.  RE: Merging all tabs from Excel data

    Posted Mon May 02, 2022 11:46 PM
    Thank you so much! I will try this out and report back.

    Best,
    Rosy




  • 6.  RE: Merging all tabs from Excel data

    Posted Tue May 03, 2022 08:31 AM
    I want to elaborate a bit on the conditions for the merge to be successful and correct.
    The code I posted is for adding variables.  In that case, besides the variable names being distinct, the cases must be in the same order.  If some rows don't line up, a key variable would need to be specified and be present in all sheets.

    If the merge is adding cases with the same variables, then the variable types, including length for strings, need to match across all the sheets.

    If these conditions are not met, let me know the details, and I'll see what can be done.

    --