Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » ADVANCED SAS » SAS Macros » How to Delete Macro Variables in SAS?

How to Delete Macro Variables in SAS?

BySubhro Posted onSeptember 23, 2020October 11, 2022 Last Updated:October 11, 2022
0 Comments

CALL SYMDEL is a data step call routine which is used to delete macro variables from the global symbol table.

symdel 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.

Page Contents

  • How to Delete a single macro variable?
    • SAS symdel example
    • Delete a macro variable with the %SYMDEL Macro Statement
  • Delete All Macro Variables

How to Delete a single macro variable?

To delete a macro variable, you can use both methods – %SYMDEL and CALL SYMDEL.

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 in the comment section. We will 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!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #call symdel#call symdel example#call symdel sas#delete macro variable sas#symdel#symdel _user_#symdel all variables#symdel example#symdel example sas#symdel function in sas#symdel macro sas#symdel statement#symdel syntax
Subhro

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.

Facebook Twitter Linkedin

Post navigation

Previous Previous
Column Input in SAS
NextContinue
The Ultimate Guide to SUBSTR in SAS

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You