DATA Step, Macro, Functions and more

Maximum number of characters in a macro variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 181
Accepted Solution

Maximum number of characters in a macro variable

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;


Accepted Solutions
Solution
‎01-19-2016 06:48 AM
Super User
Posts: 10,483

Re: Maximum number of characters in a macro variable

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


All Replies
Super User
Posts: 5,255

Re: Maximum number of characters in a macro variable

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
Solution
‎01-19-2016 06:48 AM
Super User
Posts: 10,483

Re: Maximum number of characters in a macro variable

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

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 571 views
  • 1 like
  • 3 in conversation