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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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