// Preliminaries clear all set more off cap log close // Set path global path "D:\files\UNI current\Research\In progress\correlation over time\experiment time correlation\analysis" // Open log log using "${path}\tocleandata-C.log", replace // Load data import delimited "${path}\RT_Cid.csv", varnames(1) encoding(utf8) clear //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%cleaning format%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%// //delete useless cells drop in 1 drop v* drop q* drop pra* drop location* drop name drop email drop bank //%%%%%%%%%%%%%%%%%%%correcting errors%%%%%%%%%%%%%%%%%%%%%%%% //wrong input exp_ID replace expid = "C410" if (expid == "CA10" ) replace expid = "C599" if (expid == "c499" ) replace expid = "C204" if (expid == "204" ) replace expid = "C709" if (expid == "C7o9" ) replace expid = "C807" if (expid == "C8o7" ) replace expid = "C899" if (expid == "A810" ) replace expid = "C1199" if (expid == "12" ) replace expid = "C1210" if (expid == "C1Z10" ) replace expid = "C1204" if (expid == "c1z04" ) //uppercase exp_ID replace expid = upper(expid) //get the birth-year right replace yearofbirth=strtrim(yearofbirth) gen reversed= reverse(yearofbirth) gen newyearofbirth=substr(reversed,1,4) replace yearofbirth=reverse(newyearofbirth) drop reversed newyearofbirth replace yearofbirth="1997" if (yearofbirth=="8-97") //%%%%%%%%%%%%%%%%%%formating%%%%%%%%%%%%% //rename varname rename (*) x#, addnumber rename x1 id rename x782-x786 y#, addnumber //detect version gen v=1 replace v=2 if missing(x2) //turn numeric destring x2-x781 y1-y3, replace //adjust column width format %5s id format %4.0g x2-x781 y1-y3 v //reshape reshape long x, i(i) j(ques_deci) rename x AorB gen q_d=ques_deci -1 drop ques_deci //generate question and decision as seperate variable gen qn = int((q_d-1)/20+1) gen dn = q_d-(qn-1)*20 drop q_d //%%%%%%%%%%%%%%%%%%%%%%%%detect switch & compute PCE%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //detect switch bysort id qn: gen switch= AorB - AorB[_n-1] tab switch //calculate PCE bysort id qn: egen PCE= sum(AorB) //mark null PCE gen flag = 0 replace flag=1 if switch==1 bysort id qn: egen nwrong_swit=sum(flag) replace PCE=. if nwrong_swit!=0 replace PCE=. if AorB==. //back to "wide" form drop AorB dn flag switch duplicates drop //output dta save "${path}\cleandata-C.dta"