I have more than 400 variables in my dataset with mixed case (some upper and some lower). I want to change all of them to lowercase. I tried following but doesn't work. Can someone please help?
data want;
set have;
array vars(*) _character_;
do i=1 to dim(vars);
vars(i)=lowcase(vars(i));
end;
drop i;
run;
The functions you are using address the values of the variable not the name of the variable.
You would use proc datasets to change characteristics of variables in place.
Of course one question is "Why is this important?".
Subsequent to this, I run some models in STATA which is case sensitive.
So far I was using SAS Enterprise 4.3 but recently shifted to SAS 7.1 and am wondering if something has changed.
@buckeyefisher wrote:
Subsequent to this, I run some models in STATA which is case sensitive.
So far I was using SAS Enterprise 4.3 but recently shifted to SAS 7.1 and am wondering if something has changed.
I assume you mean Enterprise Guide 7.1? I doubt anyone ever ran SAS version 7.1 as version 7 was quickly replaced with version 8 and that was, what 20 years ago?
You can try just try setting validvarname option to UPCASE.
Explain "doesnt work". Supply example data that illustrates the issue when your code is run on it.
To clarify, I want to change case of variable labels and not the contents in those variables. The code posted above is changing the contents and not the labels.
The following does what you ask for a copy of the data set SASHELP.CLASS that you should have available to test on.
It creates a control data set to get the names of the varaibles in the data set and make an uppercase version. Then uses that set to create Proc Datasets code to change the names of the variable in the Work.Class copy.
The where clause in the Proc Sql requires uppercase library and memname values of the set you want to modify. I have included two Proc contents steps to show the before and after names of the variables.
proc sql noprint; create table work.namechange as select name, upcase(name) as newname from dictionary.columns where libname='SASHELP' and Memname='CLASS' ; quit; data work.class; set sashelp.class; run; proc contents data=work.class;run; data _null_; set work.namechange end=last; if _n_=1 then call execute("proc datasets library=work nodetails; modify class; rename "); call execute( name|| '='|| newname); if last then call execute('; run;quit;'); run; proc contents data=work.class;run;
Depending on how you export the data for STATA use you likely have other options.
Here another coding option which writes the executed statements in a nice way into the SAS log.
%macro upcaseVarnames(lrefTbl);
%local lref tbl;
%let lref=%upcase(%scan(work.&lrefTbl,-2,.));
%let tbl=%upcase(%scan(&lrefTbl,-1,.));
filename codegen temp;
data _null_;
file codegen;
/* file print;*/
length _cmd $65;
set sashelp.vcolumn(where=(libname="&lref" and memname="&tbl")) end=_last;
if _n_=1 then
do;
put "proc datasets lib=&lref nolist;"
/ " modify &tbl;"
/ " rename"
;
end;
_cmd=cats(name,'=',upcase(name));
put " " _cmd;
if _last then
do;
put " ;"
/ " run;"
/ " contents data=&tbl;"
/ " run;"
/ "quit;"
;
end;
run;
%include codegen / source2;
filename codegen clear;
%mend;
data sample sample2;
set sashelp.class;
run;
%upcaseVarnames(sample)
%upcaseVarnames(sample2)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.