%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?
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);
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);
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.
That extra information about the single/double quote is helpful.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.