I use both SAS and Stata and often need to transfer data between the two. SAS is case-sensitive and Stata is not. I always prefer working with lowercase variable names in Stata. The following code is used to export a SAS dataset to Stata with all variables names converted to lowercase.
The macro I use is borrowed from Adrian’s work. Thanks Adrian.
A related post can be found here: http://kaichen.work/?p=1365.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
libname local "path_to_folder"; options mprint; %macro lowcase(dsn); %let dsid=%sysfunc(open(&dsn)); %let num=%sysfunc(attrn(&dsid,nvars)); %put # data &dsn; set &dsn(rename=( %do i = 1 %to # /*function of varname returns the name of a SAS data set variable*/ %let var&i=%sysfunc(varname(&dsid,&i)); &&var&i=%sysfunc(lowcase(&&var&i)) /*rename all variables*/ %end;)); %let close=%sysfunc(close(&dsid)); run; %mend lowcase; data temp; set local.filename; run; %lowcase(temp) proc export data= temp outfile= "path_to_folder/filename" dbms= dta replace; run; |
Hi Kai,
I would suggest using “rename *, lower” in Stata.
Adrian is actually a friend of mine and he is also using this now.
Hello Kai,
When I use proc export for .dta files, it automatically lowers the case for all variables. Is there a way to preserve the Upper or Proper case variable names? Thanks!
I would like to know this, too!
I am exporting a lot of large data sets from SAS to STATA. If I use STAT-transfer, it retains the case of the variables, but my company decided not to pay for STAT-transfer because proc export in SAS can do the job. Only that it can’t, because it translates all variable names to lower case.
So if, say, 80 percent of appr. 2000 variables are uppercase, using rename *, upper in STATA works but still creates trouble for the remaining 20 percent. So ideally I would like to know if variable cases can be preserved in proc export. Thank you!