How to Delete Macro Variables in SAS?

You can use the %SYMDEL statement or the CALL SYMDEL routine to delete SAS macro variables.

%SYMDEL and CALL SYMDEL is a data step call routine that deletes macro variables from the global symbol table.

How to Delete a single macro variable?

You can use both methods – %SYMDEL and CALL SYMDEL to delete a macro variable.

SAS symdel example

To delete a macro variable using the CALL SYMDEL, use the macro variable name within quotes in the Call routine.

Syntax:

CALL SYMDEL(macro-variable<, option>);

Example:

%let mvar = xyz;
data _null_;
	call symdel('mvar');
run;

Note that CALL SYMDEL issues a warning when an attempt is made to delete a non-existent macro variable. To suppress this message, use the NOWARN option. call symdel('mvar')

Delete a macro variable with the %SYMDEL Macro Statement

The %SYMDEL routine deletes the specified variable from the macro global symbol table.

Syntax:

%SYMDEL variable-name ;

%SYMDEL statement warns when an attempt is made to delete a non-existent macro variable. To suppress this warning in the log, you can use the NOWARN option.

%symdel variable-name / nowarn;

Example:

%let mname = ABC;
%put &mname;
%symdel mname;
%put &mname

Delete All Macro Variables

For example, assume you want to remove all macro variables from SAS. This may seem to be a viable solution.

%SYMDELL _ALL_;

This, unfortunately, is not going to work out. As a result, you’ll require a macro function to remove all macro variables.

%macro delvars;
  data vars;
    set sashelp.vmacro;
  run;

  data _null_;
    set vars;
    temp=lag(name);
    if scope='GLOBAL' and substr(name,1,3) ne 'SYS' and temp ne name then
      call execute('%symdel '||trim(left(name))||';');
  run;

%mend delvars;

%delvars

In a DATA step, the SET statement refers to the data set Vars, which has the names of the macro variables.

The LAG function gets the previous value of the name and puts it into a data set variable called Temp.

Long, macro variables will cover more than one observation in the view. This method is used to make sure that the values are unique.

Use an IF statement to make sure the following occurs:

  • the scope of the macro variable is GLOBAL
  • The first three characters do not start with SYS, because SAS only uses macro variables that start with SYS.
  • Temp is not the same as the last value.

If all three of these conditions are met, use the CALL EXECUTE routine to build the %SYMDEL statement with the name of the macro variable to be deleted. End the DATA step with a RUN statement. Invoke the macro.

Key Takeaway

So, this is the way you can delete macro variables in SAS. We hope that you must have found it useful.

Moreover, if you have other suggestions regarding tips or tricks, suggest them below the comment section. We would take those lists in our further blog post.

Thanks for reading!

If you liked this article, you might also want to read CALL SYMPUT in SAS and Create macro variables from the SAS dataset.

Do you have any tips to add Let us know in the comments.

Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.