BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
afiqcjohari
Quartz | Level 8
%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?

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

4 REPLIES 4
Shmuel
Garnet | Level 18

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);

 

afiqcjohari
Quartz | Level 8
I was trying to factor the common condititions but couldn't work it out. I'm glad you helped with it as well :). Thanks.
Astounding
PROC Star

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.

afiqcjohari
Quartz | Level 8

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

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
  • 4 replies
  • 1077 views
  • 2 likes
  • 3 in conversation