BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

I want to conditionally delete macro variable based on the value of other variable. In this case it is based on the value of the macro variable 'fnctl_cmpnt'. The macro variable 'FILE_TYPE' should be deleted as value of the macro variable 'fnctl_cmpnt' is not in 'AD' or 'DQ'

 

/**macro variables**/
%let fnctl_cmpnt=GT;
%let FILE_TYPE=;

/*Delete macro variable conditionally*/
%macro del_mac_var;
data _null_;
%if &fnctl_cmpnt not in ('AD','DQ') %then %do;
%symdel FILE_TYPE;
%end;
run;
%mend;
%del_mac_var;

%put #### &FILE_TYPE.;

Log:

 

27         %let fnctl_cmpnt=GT;
28         %let FILE_TYPE=;
29         
30         /*Delete macro variable conditionally*/
31         %macro del_mac_var;
32         data _null_;
33         %if &fnctl_cmpnt not in ('AD','DQ') %then %do;
34         %symdel FILE_TYPE;
35         %end;
36         run;
37         %mend;
38         %del_mac_var;
MLOGIC(DEL_MAC_VAR):  Beginning execution.
MPRINT(DEL_MAC_VAR):   data _null_;
SYMBOLGEN:  Macro variable FNCTL_CMPNT resolves to GT
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 
       &fnctl_cmpnt not in ('AD','DQ') 
ERROR: The macro DEL_MAC_VAR will stop executing.
MLOGIC(DEL_MAC_VAR):  Ending execution.
39         
40         %put #### &FILE_TYPE.;
SYMBOLGEN:  Macro variable FILE_TYPE resolves to 
####

Any leads to understand the cause for the issue?

6 REPLIES 6
novinosrin
Tourmaline | Level 20
options MINOPERATOR MINDELIMITER=',';
/**macro variables**/
%let fnctl_cmpnt=GT;
%let FILE_TYPE=;

/*Delete macro variable conditionally*/
%macro del_mac_var ;

%if not(&fnctl_cmpnt   in AD,DQ) %then %do;
%symdel FILE_TYPE;
%end;
%mend;
%del_mac_var
%put &=FILE_TYPE;

LOG:

254  options MINOPERATOR MINDELIMITER=',';
255  /**macro variables**/
256  %let fnctl_cmpnt=GT;
257  %let FILE_TYPE=;
258
259  /*Delete macro variable conditionally*/
260  %macro del_mac_var ;
261
262  %if not(&fnctl_cmpnt   in AD,DQ) %then %do;
263  %symdel FILE_TYPE;
264  %end;
265  %mend;
266  %del_mac_var
267  %put &=FILE_TYPE;
WARNING: Apparent symbolic reference FILE_TYPE not resolved.
FILE_TYPE

The warning message is because the macro variable file_type has been successfully deleted

David_Billa
Rhodochrosite | Level 12
Thanks. Any other ways to tackle this? I just wanted to know.
novinosrin
Tourmaline | Level 20

Your idea seems pretty straight forward. However, if your question is to do with making a process efficient would lead to far reaching questions as to what and how you are trying to do? Why would you need those macro variables? And then why you want to delete those macro variables considering the fact macro variables are any deleted at the end of the session. It all depends on what you are trying to do.

 

From the standpoint of making a macro IN operator to work, the solution is just what was posted. 

David_Billa
Rhodochrosite | Level 12

How to handle it without the Options Statement  in your code below?

options MINOPERATOR MINDELIMITER=',';
/**macro variables**/
%let fnctl_cmpnt=GT;
%let FILE_TYPE=;

/*Delete macro variable conditionally*/
%macro del_mac_var ;

%if not(&fnctl_cmpnt   in AD,DQ) %then %do;
%symdel FILE_TYPE;
%end;
%mend;
%del_mac_var
%put &=FILE_TYPE;
SASKiwi
PROC Star

@David_Billa  - What is the reason for deleting the macro variable? In all the years I've been using SAS I've never had a reason to do so. Setting a macro variable to blank, yes, but removing it, no. I'm curious to know why you think you need to do this. As @novinosrin has demonstrated deleting a macro variable and then trying to refer to it results in log warnings which is not advisable.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1004 views
  • 1 like
  • 4 in conversation