Make use of Qualtrics’ exported csv data in Stata

Qualtrics dumps its data to Excel is a format that’s not quite ready for Stata, but it’s pretty close. You get variable names in the first row and variable labels in the second row. Variable labels longer than XX characters get truncated in the middle in the case of sub-items on ‘matrix’ and other survey questions with multiple components, such that the title of the sub-item is listed in full if possible. We can extract the sub-item label, if it exists, by splitting the string on either of the delimiters Qualtrics (currently!) uses: …- and ?-

import excel filename.xlsx, firstrow clear
foreach v of varlist _all {
   local l`v' = `v'[1]
   local l`v' = subinstr("`l`v''","...-","|",.)
   local l`v' = subinstr("`l`v''","?-","|",.)
   tokenize "`l`v''", parse("|")
   if "`3'"=="" {
      label variable `v' "`1'"
   }
   else {
      label variable `v' "`3'"
   }
   note `v' : "`l`v''"
}
drop in 1