BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ujjawal
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

 

View solution in original post

2 REPLIES 2
LinusH
Tourmaline | Level 20

Having that many variables seldom makes sense.

Transpose the data set, and then you can use regular data assignment logic to the renaming.

Data never sleeps
ballardw
Super User

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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 8362 views
  • 3 likes
  • 3 in conversation