I have a SAS macro that I need to convert to R and since I am new to SAS I am having a little trouble understanding one part of the code.

At the beginning of the macro, I have this line that defines the macro -

%MACRO macroname(sdate=01JAN2011,edate=01JAN2013, FILTERS=platform in (1,2,3),OUT=data_final)

%if %length(&filters) > 2 %then %let filters = and &filters; 
%else %let filters = %str( );

In one of the stages in the macro, I have the following command -

proc sort data = A noduplicates;
  where 1 &filters;
  by id, date;
  run;

I understand that in the where condition, the filter conditions will be applied. But what does the 1 do? similarly, what does the if else condition on the filter column do?

Thanks!

有帮助吗?

解决方案

This can also be written as 1=1, and is essentially a static TRUE condition.

This is actually a neat little trick that gets around the problem of adding multiple where clauses (as macro variables). Each where condition must be separated by an operator (AND / OR, usually AND), but it is not always known if another condition precedes the one you are adding. For instance, you may want the flexibility to add the same &filter to both of these where claueses:

where x=1 &filter;

where &filter;

of course the first example must resolve to x=1 AND some=filter; and the second must not resolve to where AND some=filter;

Remember, any condition in a where clause ultimately resolves to true or false. By adding "1", we have a static "TRUE" condition that has no effect on the query but does give us flexibility (and readability) in the code (basically avoids more macro %if %else statements) if we always prepend the "AND" to &filter.

where 1 &filter  

instead of

where %if %length(&filter)>4 and "%substr(%upcase(&filter))" ne AND %then %do;
   where 1 and &filter
%end;
%else %do;
   where &filter
%end;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top