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; |