Vars to cases & retain variable/value labels Tableau setup - restructure data for Tableau, flip data

前端 未结 2 531
粉色の甜心
粉色の甜心 2021-01-16 10:35

I am flipping my survey data so I can use it in Tableau. Here is example data in SPSS (keep in mind that each variable has value & variable labels).

ID          


        
2条回答
  •  攒了一身酷
    2021-01-16 11:08

    This is the solution based on the other answer using OMS, and I added few other things. This flips the vars you want and converts any other var you want to string.

    dataset close all.
    new file.
    
    get file 'C:\Users\nicholas\Desktop\testFile.sav'.
    
    ************************************************************************************************ 
    TABLEAU SETUP
    ********************************************** 
    
    insert file="C:/Users/nicholas/Desktop/Type2syntax.sps".
    
    !toString vars = visitorType.
    
    !flipAndMatch vars = rate1 rate2 rate3 mr_1 mr_2 mr_3.
    
    exe.
    
    *CATEGORIZE FLIPPED VARS
    
    String filter (a150).
    !groupingBy 'Rating satis' rate1 rate2 rate3.
    !groupingBy 'MR with' mr_1 mr_2 mr_3.
    
    save outfile 'C:\Users\nicholas\Desktop\OtherTableauTest2.sav'.
    

    "C:/Users/nicholas/Desktop/Type2syntax.sps" is :

    * Encoding: UTF-8.
    
    save outfile 'C:\Users\nicholas\Desktop\tempSav.sav'.
    
    DATASET DECLARE  varlab.
    OMS   /SELECT TABLES   /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information']
      /DESTINATION FORMAT=SAV OUTFILE='varlab' VIEWER=YES.
    
    DATASET DECLARE  vallab.
    OMS  /SELECT TABLES   /IF COMMANDS=['File Information'] SUBTYPES=['Variable Values']
      /DESTINATION FORMAT=SAV OUTFILE='vallab' VIEWER=YES.
    display dictionary.
    omsend.
    
    DATASET ACTIVATE varlab.
    rename variables var1= varName / label = Question.
    alter type varName (a20).
    alter type Question (a1000).
    sort cases by varName.
    SAVE OUTFILE='C:\Users\nicholas\Desktop\varlabsTemp.sav'
    /keep varName Question.
    
    DATASET ACTIVATE vallab.
    rename variables var1=varName / var2 = AnswerNumb / Label = AnswerText.
    alter type varName (a20).
    alter type AnswerText (a120).
    sort cases by varName AnswerNumb.
    SAVE OUTFILE='C:\Users\nicholas\Desktop\vallabsTemp.sav'
    /keep varName AnswerNumb AnswerText.
    
    dataset close all.
    new file.
    
    get file  'C:\Users\nicholas\Desktop\tempSav.sav'.
    compute UNIQUE_ID = $casenum.
    
    
    DEFINE !toString (vars=!CMDEND)
    !DO !var !IN (!vars)
    !LET !varDelete=!CONCAT("Delete", !var)
    rename variables !var = !varDelete.
    String !var (a120).
    compute !var = valuelabels(!varDelete).
    exe.
    delete variables !varDelete.
    !DOEND
    !ENDDEFINE.
    
    DEFINE !groupingBy (!POSITIONAL !TOKENS(1)
                   /!POSITIONAL !CMDEND)
    !DO !var !IN (!2)
    !LET !varString=!CONCAT("'", !var,"'")
    if varName eq !varString filter eq !1.
    !DOEND
    exe.
    !ENDDEFINE.
    
    DEFINE !flipAndMatch (vars=!CMDEND)
    VARSTOCASES
     /MAKE AnswerNumb FROM !vars
     /INDEX=VarName (AnswerNumb)
     /KEEP=ALL
     /NULL=KEEP.
    EXECUTE.
    
    sort cases by varName AnswerNumb.
    alter type varName (a20).
    
    match files files*
    /table='C:\Users\nicholas\Desktop\vallabsTemp.sav'
    /by varName AnswerNumb.
    
    match files files*
    /table='C:\Users\nicholas\Desktop\varlabsTemp.sav'
    /by varName.
    
    if AnswerText eq '' AnswerText = string(AnswerNumb, f).
    !ENDDEFINE.
    

    Output looks something like this. I didn't flip age or visitorType, but I certainly could have.

    UNIQUE_ID    VarName    AnswerNumb    AnswerText   Question           filter          age       VisitorType
    1             'rate1'        8           '8'      'Rate food'     'Rating group'       35    'Overnight Visitor'
    1             'rate2'        3           '3'      'Rate wait time''Rating group'       35    'Overnight Visitor'
    1             'rate3'        2           '2'      'Rate bathroom' 'Rating group'       35    'Overnight Visitor' 
    1             'mr_1'         1         'White'    'Ethnicity'      'MR group'          35    'Overnight Visitor'
    ...
    

提交回复
热议问题