- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That extra information about the single/double quote is helpful.