* Import Open Clinica SPSS file with variable and value labels version 12.0 clear capture clear matrix set more off set linesize 190 pause on * To do before you can use this file: * Identify and download the necessary data from open clinica database in SPSS format * Create Directory for these data, and extract zip file with SPSS data to this directory * Please make sure that you only extract ONE .dat and ONE .sps file to this directory, and * that these come from the same zip file with SPSS data. * When you execute this do-file: * Whenever there is a "pause" (i.e. the do file stops without error message), please read the * on-screen instruction, do, what needs to be done, and then continue (q, ENTER). * If everything goes well, this do file should result in a Stata file of the same name as the * exported SPSS file, which should contain all the variable and value labels that were exported * in the accompanying .sps file. * However, if the procedure fails, it is usually during the labelling process, which means that * the resulting do-file for labelling ("LabVarsAndValues.do") needs to be changed. This can * either be done directly in that do-file, or, if the change should be more permanent, in this * file here, under the heading "* Implement Dataset specific label changes below if required:" below. * If you also need to rename variables (e.g. because the OC varnames are too long for Stata varnames), * this should be done below under "* Implement Dataset specific Variable name changes below if required:". * However, in this case you will need to change both, the variable names in the dataset, and the * accompanying labelling file. * Now please CHANGE WORKING DIRECTORY to where your SPSS .dat and .sps files are located * (e.g. cd "D:\Documents\OpenClinica\StudyXYZ" * or via GUI: File > Change Working Directory...) and carry on (q, ENTER) pause quietly { capture findfile "ingap.ado" if _rc~=0 { di as err _new(2) "This file requires the userwritten - ingap - command to be installed" di as err `"Please type "net describe ingap, from(http://fmwww.bc.edu/RePEc/bocode/i)" and follow instructions to install."' di as err "Then rerun this do-file." } } findfile "ingap.ado" * Generate Variable labels: local SpsFile : dir . files "*.sps", respect insheet using `SpsFile', delimiter(" ") clear gen Keep = 1 if v1=="VARIABLE" & v2=="LABELS" replace Keep=0 if v1=="VALUE" & v2=="LABELS" replace Keep=Keep[_n-1] if Keep==. keep if Keep==1 keep if v3=="/" | v4=="/" drop if v1=="VARIABLE" & v2=="LABELS" capture noisily assert _N==0 if _rc==0 { set obs 1 gen CodeVar = "* No Variable Labels defined, or error in do file, please check" di as err "No Variable Labels defined, or error in do file, please check" pause } capture gen CodeVar="" replace v3=v2 if v3=="/" replace CodeVar= "lab var " + v1 +`" ""' + v3 + `"""' list v1 v2 v3 v4 CodeVar keep CodeVar ingap replace CodeVar=`"* Generate Variable labels from `SpsFile' "' in 1 save VariableLabels.dta, replace * Generate value labels: insheet using `SpsFile', delimiter(" ") clear gen Keep = 1 if v1=="VARIABLE" & v2=="LABELS" replace Keep=0 if v1=="VALUE" & v2=="LABELS" replace Keep=Keep[_n-1] if Keep==. keep if Keep==0 drop if v1=="VALUE" & v2=="LABELS" drop if v1=="." drop if v1=="EXECUTE." capture noisily assert _N==0 if _rc==0 { set obs 1 gen CodeVar = "* No Value Labels defined, or error in do file, please check" di as err "No Value Labels defined, or error in do file, please check" pause } capture gen CodeVar="" gen Var=v1 if v2=="" & v1~="/" replace Var=Var[_n-1] if Var=="" replace CodeVar="label define " + v1 if v2=="" & v1~="/" & CodeVar=="" drop if CodeVar=="label define " gen Quot=`"""' replace CodeVar= " " + v1 + " " + Quot + v2 + Quot if CodeVar=="" replace CodeVar= "; " + "label values " + Var + " " + Var + " " + ";" if v1=="/" ingap replace CodeVar="#delimit ;" in 1 ingap -1, after replace CodeVar="#delimit cr ;" in l list v1 v2 CodeVar, sepby(Var) keep CodeVar ingap replace CodeVar=`"* Generate Value Labels from `SpsFile' "' in 1 save ValueLabels.dta, replace clear use VariableLabels.dta gen VarLab=1 append using ValueLabels.dta gen ValLabNum=_n if strmatch(CodeVar, "*label define *") replace ValLabNum=ValLabNum[_n-1] if ValLabNum==. capture erase VariableLabels.dta capture erase ValueLabels.dta * Get rid of prefixed $ symbol in varnames replace CodeVar=subinstr(CodeVar, "v$", "v", .) * Get rid of ' quotation mark around numerical codes * (below needs changing if you have coded variables <-1000 or >1000) forvalues Num = -1000/1000 { replace CodeVar=subinstr(CodeVar, "'`Num''", "`Num'", .) } * Implement Dataset specific label changes below if required: replace CodeVar=subinstr(CodeVar, "InterviewDateE", "InterviewDate_E", .) format CodeVar %-20s * Visual inspection: list CodeVar if VarLab==1 pause Please check whether variable labelling commands look ok! list CodeVar if VarLab==., sepby(ValLabNum) pause Please check whether value labelling commands look ok! drop VarLab ValLabNum outfile using "LabVarsAndValues.do", noquote replace clear local DatFile : dir . files "*.dat", respect insheet using `DatFile', clear case * Implement Dataset specific Variable name changes below if required: do "LabVarsAndValues.do" local StataFile=subinstr(`DatFile', ".dat", ".dta",1) save "`StataFile'", replace