Vars to cases&保留变量/值标签Tableau设置 - Tableau的重组数据,翻转数据

时间:2017-04-20 15:45:38

标签: database tableau spss

我正在翻转我的调查数据,因此我可以在Tableau中使用它。以下是SPSS中的示例数据(请记住,每个变量都有值和变量标签)。

ID    age   rate1   rate2   rate3   mr_1    mr_2    mr_3     ...
1      35    8        3       2      1       2        
2      40    2        2       3              2       
3      41    6        3       5              2       3
4      43    3        3       1         

其中rate1-3是3个评级问题。 Mr_1到mr_3是多重回复检查所有申请问题(你的种族是什么?1 =白人2 =西班牙裔,3 =黑人)

我使用这个来翻转数据:

VARSTOCASES
  /MAKE answer FROM age rate1 rate2 rate3 mr_1 mr_2 mr_3
  /INDEX=Index1(7)
  /KEEP= All
  /NULL=KEEP.

结果如下:

ID Index1 answer
1    1      35
1    2      8
1    3      3
1    4      2
1    5      1
...
...
...

将此连接到Tableau时可以正常工作。但是,我想要的不仅仅是Index1作为已翻转的每个变量的标识符。我想要的是这个(Var,VarLab,ValueLabel只是字符串变量):

ID   Var           VarLab            answer    ValueLabel
1    'age'      'What is your age?'      35         '35'                             
1    'rate1'    'Rate food'               8         '8'
1    'rate2'    'Rate wait time'          3         '3'
1    'rate3'    'Rate bathroom'           2         '2'
1    'mr_1'     'Ethnicity'               1         'White'
1    'mr_2'     'Ethnicity'               2         'Hispanic'
...
...  
...    

如您所见,我为每个翻转变量保留了变量标签,值标签和变量名称本身。这是理想的Tableau设置,因为Tableau需要“高”数据集。此外,我可以使用响应的字符串或数字表示。最后,我不再需要在Tableau中编辑别名。任何想法如何实现这一目标?也许这需要python或宏?非常感谢任何想法。

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要使用OMS将字典读入两个数据集 - 一个用于变量标签,另一个用于值标签。 然后,您可以通过变量名称将重构的数据集与变量标签匹配,然后通过变量名称和值将其与值标签匹配。

运行此命令以获取两个数据集 - 当然,重组之前:

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.

现在重组和匹配文件 - (重命名适当的变量以便在两个新数据集中进行匹配)。

答案 1 :(得分:1)

这是基于使用OMS的其他答案的解决方案,我添加了一些其他的东西。 这会翻转您想要的变量并转换您想要变换的任何其他变量。

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”是:

* 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.

输出看起来像这样。我没有翻过年龄或者访问者类型,但我当然可以。

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