DATA Step, Macro, Functions and more

How to test whether macro variable exist in macro if statement?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 99
Accepted Solution

How to test whether macro variable exist in macro if statement?

[ Edited ]
%macro extract(beginDate='01JAN2017'd,
	                        endDate='05JAN2017'd,
	                        cPvderVAR=);
%put &cPvderVAR.;
	data want_&cPvderVAR.;
		set have;
		%if '&cPvderVAR.' ne %then %do; 
			where dTran >= &beginDate. and dTran <= &endDate. and cPvder='&cPvderVAR.' ;
		%end;
		%else %do;
			where dTran >= &beginDate. and dTran <= &endDate.;
		%end;
	run;
%mend extract;

%extract(cPvderVAR=PING);

Keep getting this error

WHERE 0 /* an obviously FALSE WHERE clause */ ;

 

What I want is, if the macro variable cPvderVAR has a value then add the where statement with cPvder = &cPvderVAR. 

Note that cPvder is a string.

 

Does SAS has something to test whether the macro variable is of length null or positive?


Accepted Solutions
Solution
‎03-15-2017 02:51 AM
Trusted Advisor
Posts: 1,372

Re: How to test whether macro variable exist in macro if statement?

[ Edited ]

You defined the macro as: 

%macro extract(beginDate='01JAN2017'd, endDate='05JAN2017'd, cPvderVAR=);

I understand that you want to check does the 2nd argument is not empty,

then can use either %if  &cPvderVAR NE (without quotes)   or  %if %length(&cPvderVAR) > 0

 

You could alse use next code:

%macro extract(beginDate='01JAN2017'd,
	                        endDate='05JAN2017'd,
	                        cPvderVAR=);
%put &cPvderVAR.;
	data want_&cPvderVAR.;
		set have;
		%do;
			where dTran >= &beginDate. and dTran <= &endDate. 
                %end;
                %if &cPvderVAR. ne %then %do; 
                              and cPvder="&cPvderVAR."      /* changed to double quotes */ 
		%end;
; /* semicolon to close the where statement */ run; %mend extract; %extract(cPvderVAR=PING);

 

View solution in original post


All Replies
Solution
‎03-15-2017 02:51 AM
Trusted Advisor
Posts: 1,372

Re: How to test whether macro variable exist in macro if statement?

[ Edited ]

You defined the macro as: 

%macro extract(beginDate='01JAN2017'd, endDate='05JAN2017'd, cPvderVAR=);

I understand that you want to check does the 2nd argument is not empty,

then can use either %if  &cPvderVAR NE (without quotes)   or  %if %length(&cPvderVAR) > 0

 

You could alse use next code:

%macro extract(beginDate='01JAN2017'd,
	                        endDate='05JAN2017'd,
	                        cPvderVAR=);
%put &cPvderVAR.;
	data want_&cPvderVAR.;
		set have;
		%do;
			where dTran >= &beginDate. and dTran <= &endDate. 
                %end;
                %if &cPvderVAR. ne %then %do; 
                              and cPvder="&cPvderVAR."      /* changed to double quotes */ 
		%end;
; /* semicolon to close the where statement */ run; %mend extract; %extract(cPvderVAR=PING);

 

Frequent Contributor
Posts: 99

Re: How to test whether macro variable exist in macro if statement?

I was trying to factor the common condititions but couldn't work it out. I'm glad you helped with it as well Smiley Happy. Thanks.
Super User
Posts: 5,081

Re: How to test whether macro variable exist in macro if statement?

[ Edited ]

You can't use single quotes around a reference to a macro variable.  It won't resolve.  Try it this way:

 

%macro extract (beginDate='01JAN2017'd, endDate='05JAN2017'd, cPvderVar=);

   %put &cPvderVAR.;

   data want_&cPvderVAR.;

      set have;

      where (&beginDate. <= dTran <= &endDate.)

      %if %length(&cPvderVar) %then and cPvder="&cPvderVAR.";

      ;

   run;

%mend extract;

 

%extract(cPvderVAR=PING)

 

Switching to double quotes lets &cPvderVAR resolve.  The other changes aren't mandatory, just coding that makes it easier to see how the program changes when cPvderVAR is specified vs. not specified.

Frequent Contributor
Posts: 99

Re: How to test whether macro variable exist in macro if statement?

That extra information about the single/double quote is helpful.

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 150 views
  • 2 likes
  • 3 in conversation