Sunday, December 5, 2010

Easy way to UPCASE variable names of SAS dataset

option VALIDVARNAME=UPCASE;

Use trhe above option statement to upcase the variable name of the SAS dataset irrespective of type of variable in the dataset (character or numeric).


The following example shows how the option sattement VALIDVARNAME=UPCASE works.

proc contents data=sashelp.class out=test;

run;




Note: Propcase variable names. 

*Upcasing the variables;

option validvarname=upcase;
proc sort data=sashelp.class out=test; run;



Because of the option statement. Ex:  'Age'  becomes 'AGE' and 'Height' becomes 'HEIGHT' etc.

See the SAS Language Reference dictionary to get more details.


Another way to do this is to use a macro and I call it as UPCASE macro.

proc sort data=sashelp.class out=test;

by name;
run;
 
%macro upcase (lib,dsn);


*Create a macro variable with the total number of variable count;
data _null_;
set sashelp.vtable(where=(libname="&LIB" and memname="&DSN"));
call symput('nvars',nvar);
run;

*Create a macro variable with variable names;data _null_;
set sashelp.vcolumn(where=(libname="&LIB" and memname="&DSN"));
call symput(cats("var",_n_),name);
run;

proc datasets library=&LIB;
modify &DSN;
%do i = 1 %to &nvars;
rename &&var&i=%upcase(&&var&i);
%end;
;
quit;
run;
%mend;
%upcase(WORK,TEST);
ShareThis