program transpose
syntax [, vars]
n display "Note: by specifying option " as result "vars" as input ", the first column will be retained as variable names"
global tempdir=c(tmpdir)

qui file open csv using "$tempdir\transpose.csv", write replace

foreach var of varlist * {
	local vtype: type `var'
	if substr("`vtype'",1,3)=="str" qui replace `var'=`"""'+`var'+`"""' if regexm(`var',",")
}

	local obs=c(N)
	foreach var of varlist * {
		file write csv "`var',"
		forv i=1/`obs' {
			local str=`var'[`i']
			if `i'<`obs' file write csv `"`str',"'
			if `i'==`obs' file write csv `"`str'"'
		}
	file write csv _newline
	}

file close csv

if "`vars'"=="" local arg_var="varnames(nonames)"
import delimited using "$tempdir\transpose.csv", clear `arg_var'
//drop in l
erase "$tempdir\transpose.csv"

end