如何在SPSS中创建Python宏每次给变量名加1

How to Create Python Macro in SPSS to Add 1 to Variable Names Each Time

我有一个 SPSS22 数据文件(已附上),想将其分成 4 个文件并分别保存。我现在有了 SPSS 语法,正在尝试使用 SPSS 中内置的 Python 来制作宏,因此语法会更短。

稍微解释一下我的SPSS文件,它有四组变量,代表四个空位。例如,第一组变量全部以“_1v”结尾,第二组变量全部以“_2v”结尾,以此类推。现在我想打开原文件下载072617.sav,删除第一组变量末尾的“_1v”,只保留第一组变量和两个变量OrganizationNumber 和 VacantPositions,并将其保存为 One Vacancy.sav。接下来我想去原文件重新下载072617.sav,删除第二组变量末尾的“_2v”,只保留第二组变量和OrganizationNumber和VacantPositions两个变量,保存为TwoVacancies.sav。第三组和第四组变量同理。我在这个问题的底部包含了我写的 SPSS 语法。

现在要使用内置的 Python 创建宏,我想我需要从以下内容开始:

import spss,spssaux
def breakfiles ():
    counter=1
    for i in range(16):
    spss.Submit(r"""

然而,我对Python的了解太有限,无法再写下去了。我以为我可以附上一个文件,但不知怎么的我只能附上一张图片。

SPSS 语法:

1 个职位空缺

    GET
          FILE='I:\First Download 072617.sav'.    
    DATASET NAME Test WINDOS=FRONT.  
    RENAME VARIABLES (TenureTrack_1v NontenureTrack_1v NoTenureSystem_1v TenureOther_1v TenureOtherSpec_1v DoctoralDegree_1v MasterDocPreferred_1v MasterDegree_1v DegreeOther_1v DegreeOtherSpec_1v
Prof_1v ProfOrAssoProf_1v AssoProf_1v AssoOrAssi_1v AssiProf_1v AssiProfOrInstr_1v Instr_1v RankOther_1v RankOtherSpec_1v withAdmin_1v NoAdmin_1v AdminOther_1v AdminOtherSpec_1v BacOnly_1v
MasOnly_1v DocOnly_1v BacAndMas_1v BacAndDoc_1v MasAndDoc_1v BacMasDoc_1v NoTeachingLevel_1v TeachingLevelOther_1v TeachingLevelOtherSpec_1v DidacticOnly_1v ClinicalOnly_1v DidacticAndClinical_1v
NoTeachingArea_1v TeachingAreaOther_1v TeachingAreaOtherSpec_1v GoToNextPage_1v = 
TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec
Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther AdminOtherSpec BacOnly
MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage).  
    FILTER OFF.  
    USE ALL.  
    SELECT IF (VacantPositions = 1).  
    EXECUTE.  
    SAVE OUTFILE='I:\One Vacancy.sav'
  /KEEP= 
SchoolCo Organization Completer_Name Completer_Title Completer_Phone Completer_Email Completer_Address TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree
MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther
AdminOtherSpec BacOnly MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage
  /COMPRESSED.  
    DATASET ACTIVATE DATASET20.  
    DATASET CLOSE Test.  

2 个职位空缺

    GET
      FILE='I:\First Download 072617.sav'.  
    DATASET NAME Test WINDOW=FRONT.  
    RENAME VARIABLES (TenureTrack_2v NontenureTrack_2v NoTenureSystem_2v TenureOther_2v TenureOtherSpec_2v DoctoralDegree_2v MasterDocPreferred_2v MasterDegree_2v DegreeOther_2v DegreeOtherSpec_2v
Prof_2v ProfOrAssoProf_2v AssoProf_2v AssoOrAssi_2v AssiProf_2v AssiProfOrInstr_2v Instr_2v RankOther_2v RankOtherSpec_2v withAdmin_2v NoAdmin_2v AdminOther_2v AdminOtherSpec_2v BacOnly_2v
MasOnly_2v DocOnly_2v BacAndMas_2v BacAndDoc_2v MasAndDoc_2v BacMasDoc_2v NoTeachingLevel_2v TeachingLevelOther_2v TeachingLevelOtherSpec_2v DidacticOnly_2v ClinicalOnly_2v DidacticAndClinical_2v
NoTeachingArea_2v TeachingAreaOther_2v TeachingAreaOtherSpec_2v GoToNextPage_2v = 
TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec
Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther AdminOtherSpec BacOnly
MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage).  
    FILTER OFF.  
    USE ALL.  
    SELECT IF (VacantPositions = 2).  
    EXECUTE.  
    SAVE OUTFILE='I:\Two Vacancies.sav'
  /KEEP= 
SchoolCo Organization Completer_Name Completer_Title Completer_Phone Completer_Email Completer_Address TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree
MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther
AdminOtherSpec BacOnly MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage
  /COMPRESSED.  
    DATASET ACTIVATE DATASET20.  
    DATASET CLOSE Test.  

3 个职位空缺

    GET
      FILE='I:\First Download 072617.sav'.  
    DATASET NAME Test WINDOW=FRONT.  
    RENAME VARIABLES (TenureTrack_3v NontenureTrack_3v NoTenureSystem_3v TenureOther_3v TenureOtherSpec_3v DoctoralDegree_3v MasterDocPreferred_3v MasterDegree_3v DegreeOther_3v DegreeOtherSpec_3v
Prof_3v ProfOrAssoProf_3v AssoProf_3v AssoOrAssi_3v AssiProf_3v AssiProfOrInstr_3v Instr_3v RankOther_3v RankOtherSpec_3v withAdmin_3v NoAdmin_3v AdminOther_3v AdminOtherSpec_3v BacOnly_3v
MasOnly_3v DocOnly_3v BacAndMas_3v BacAndDoc_3v MasAndDoc_3v BacMasDoc_3v NoTeachingLevel_3v TeachingLevelOther_3v TeachingLevelOtherSpec_3v DidacticOnly_3v ClinicalOnly_3v DidacticAndClinical_3v
NoTeachingArea_3v TeachingAreaOther_3v TeachingAreaOtherSpec_3v GoToNextPage_3v = 
TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec
Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther AdminOtherSpec BacOnly
MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage).  
    FILTER OFF.  
    USE ALL.  
    SELECT IF (VacantPositions = 3).  
    EXECUTE.  
    SAVE OUTFILE='I:\Three Vacancies.sav'
  /KEEP= 
SchoolCo Organization Completer_Name Completer_Title Completer_Phone Completer_Email Completer_Address TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree
MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther
AdminOtherSpec BacOnly MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage
  /COMPRESSED.  
    DATASET ACTIVATE DATASET20.  
    DATASET CLOSE Test.  

4 个职位空缺

    GET
      FILE='I:\First Download 072617.sav'.  
    DATASET NAME Test WINDOW=FRONT.  
    RENAME VARIABLES (TenureTrack_4v NontenureTrack_4v NoTenureSystem_4v TenureOther_4v TenureOtherSpec_4v DoctoralDegree_4v MasterDocPreferred_4v MasterDegree_4v DegreeOther_4v DegreeOtherSpec_4v
Prof_4v ProfOrAssoProf_4v AssoProf_4v AssoOrAssi_4v AssiProf_4v AssiProfOrInstr_4v Instr_4v RankOther_4v RankOtherSpec_4v withAdmin_4v NoAdmin_4v AdminOther_4v AdminOtherSpec_4v BacOnly_4v
MasOnly_4v DocOnly_4v BacAndMas_4v BacAndDoc_4v MasAndDoc_4v BacMasDoc_4v NoTeachingLevel_4v TeachingLevelOther_4v TeachingLevelOtherSpec_4v DidacticOnly_4v ClinicalOnly_4v DidacticAndClinical_4v
NoTeachingArea_4v TeachingAreaOther_4v TeachingAreaOtherSpec_4v GoToNextPage_4v = 
TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec
Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther AdminOtherSpec BacOnly
MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage).  
    FILTER OFF.  
    USE ALL.  
    SELECT IF (VacantPositions = 4).  
    EXECUTE.  
    SAVE OUTFILE='I:\Three Vacancies.sav'
  /KEEP= 
SchoolCo Organization Completer_Name Completer_Title Completer_Phone Completer_Email Completer_Address TenureTrack NontenureTrack NoTenureSystem TenureOther TenureOtherSpec DoctoralDegree
MasterDocPreferred MasterDegree DegreeOther DegreeOtherSpec Prof ProfOrAssoProf AssoProf AssoOrAssi AssiProf AssiProfOrInstr Instr RankOther RankOtherSpec withAdmin NoAdmin AdminOther
AdminOtherSpec BacOnly MasOnly DocOnly BacAndMas BacAndDoc MasAndDoc BacMasDoc NoTeachingLevel TeachingLevelOther TeachingLevelOtherSpec DidacticOnly ClinicalOnly DidacticAndClinical
NoTeachingArea TeachingAreaOther TeachingAreaOtherSpec GoToNextPage
  /COMPRESSED.  
    DATASET ACTIVATE DATASET20.  
    DATASET CLOSE Test.

`

给你写了一个小宏(SPSS 宏,不是 python),应该可以大大缩短你的工作时间:

define DoList (nv=!tokens(1) !default(""))
!do !vrnm !in ("TenureTrack NontenureTrack NoTenureSystem TenureOther [continue with the rest of your variables here]")
!if (!nv<>"") !then !concat(!vrnm,"_",!nv,"v") !else !vrnm !ifend
!doend
!enddefine.

在 运行 宏定义之后你可以这样使用它:

GET FILE='I:\First Download 072617.sav'.  
RENAME VARIABLES (DoList nv=1 =  DoList ).  
SELECT IF (VacantPositions = 1).  
SAVE OUTFILE='I:\one Vacancy.sav' /KEEP= DoList   /COMPRESSED.  

GET FILE='I:\First Download 072617.sav'.  
RENAME VARIABLES (DoList nv=2 =  DoList ).  
SELECT IF (VacantPositions = 2).  
SAVE OUTFILE='I:\two Vacancies.sav' /KEEP= DoList   /COMPRESSED.  

等等。

我还会考虑使用 varstocases 重组数据 - 这可能会让您有一个很好的起点(也许您可以在一个文件中处理您的数据,一旦它安排得更好,如果您仍然想单独的文件,从这里开始要容易得多)。