I have a dataset containing more than 5000 numeric variables. I need to rename all the numeric variables to var1- var5000 (Not exactly 5000 variables). I am labeling them as well so that one has mapping after renaming them.
The problem is SAS can store max number of characters 65k.
%macro numrename;
*Getting numeric variable names
proc sql noprint;
select name into : nvars separated by " "
from dictionary.columns
where LIBNAME = "JS"
and MEMNAME = "OUTWOE"
and type = "num";
quit;
*Count number of variables;
%let varn=%sysfunc(countw(&nvars%str( )));
*Run for all the numeric variables;
%DO i=1 %TO &varn;
*Selecting Variable one by one;
%let varName =%scan(%sysfunc(compbl(&nvars)),&i,%str( ));
%let vara = var&i.;
*Renaming variables
data outputt;
set out_woe (rename = (&varName. = &vara.));
label &vara. = "&varName.";
run;
%mend;
The SAS option MEXECSIZE limits the maximum size of a macro that can be executed in memory, default65536. You can change with with an options statement example:
Options mexecsize=200000;
However you also have to consider the option MSYMTABMAX for total memory of the macro table and MVARSIZE which limits size of any single macro variable, default varies by operating system.
These can also be set using Options statement.
If you want to rename or set charateristics in an existing dataset then examine the DATASETS procedure.
And that approach isn't needed:
proc sql;
create table Vars as
select name
from dictionary.columns
where LIBNAME = "SASHELP"
and MEMNAME = "CLASS"
and type = "num";
quit;
data _null_;
set vars end=eof;;
if _n_ = 1 then do;
call execute ("data work.output;");
call execute ("set sashelp.class;");
end;
newname = cats("Var",_n_);
call execute ("label "||strip(name)||' = "'||strip(name)||'";');
call execute ("rename "||strip(name)||' = '||strip(newname)||';');
if eof then do;
call execute ("run;");
end;
run;
I submit that you might want to consider pulling the label from the original variable if any and then possibly either using that or appending the old variable name and previous label if other than the variable name
Having that many variables seldom makes sense.
Transpose the data set, and then you can use regular data assignment logic to the renaming.
The SAS option MEXECSIZE limits the maximum size of a macro that can be executed in memory, default65536. You can change with with an options statement example:
Options mexecsize=200000;
However you also have to consider the option MSYMTABMAX for total memory of the macro table and MVARSIZE which limits size of any single macro variable, default varies by operating system.
These can also be set using Options statement.
If you want to rename or set charateristics in an existing dataset then examine the DATASETS procedure.
And that approach isn't needed:
proc sql;
create table Vars as
select name
from dictionary.columns
where LIBNAME = "SASHELP"
and MEMNAME = "CLASS"
and type = "num";
quit;
data _null_;
set vars end=eof;;
if _n_ = 1 then do;
call execute ("data work.output;");
call execute ("set sashelp.class;");
end;
newname = cats("Var",_n_);
call execute ("label "||strip(name)||' = "'||strip(name)||'";');
call execute ("rename "||strip(name)||' = '||strip(newname)||';');
if eof then do;
call execute ("run;");
end;
run;
I submit that you might want to consider pulling the label from the original variable if any and then possibly either using that or appending the old variable name and previous label if other than the variable name
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.