Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions labkey-api-sas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# The LabKey Remote API Library for SAS - Change Log

## version 1.1.0
*Released* : TBD
* `%labkeyMakeFilter` accepts every operator that takes a value; previously all of them failed ([GH Issue 1800](https://github.com/LabKey/internal-issues/issues/1800))
* `%labkeyMakeFilter` keeps quotes, spaces, and commas in quoted values instead of stripping or splitting on them
* `%labkeyMakeFilter` no longer resolves `&` or `%` in single-quoted arguments; quote column names and values with single quotes
* `%labkeyMakeFilter` reports an invalid operator when the data step runs rather than when the macro is called
* Untested against a live SAS installation; verify before release

## version 1.0.1
*Released* : 26 July 2022
* Update labkeyClientApi to v1.5.2
Expand Down
12 changes: 6 additions & 6 deletions labkey-api-sas/macros/_labkeysendcommand.sas
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
/*
If viewName, filter, columns, or sort params have been specified then set them on the command
*/
%if &viewName ne %then %do;
%if %length(%superq(viewName)) %then %do;
command.callVoidMethod('setViewName', &viewName);
%end;

%if &filter ne %then %do;
%if %length(%superq(filter)) %then %do;
&filter;
%end;

%if &columns ne %then %do;
%if %length(%superq(columns)) %then %do;
command.callVoidMethod('setColumns', &columns);
%end;

%if &sort ne %then %do;
%if %length(%superq(sort)) %then %do;
command.callVoidMethod('setSorts', &sort);
%end;
%end;
Expand Down Expand Up @@ -67,9 +67,9 @@
/*
Container filter is totally optional, so we check here instead of setting containerFilter = &lk_containerFilter in the params list.
*/
%if (&containerFilter eq) and %symexist(lk_containerFilter) %then %let containerFilter = &lk_containerFilter;
%if %length(%superq(containerFilter)) = 0 and %symexist(lk_containerFilter) %then %let containerFilter = &lk_containerFilter;

%if &containerFilter ne %then %do;
%if %length(%superq(containerFilter)) %then %do;
command.callVoidMethod('setContainerFilter', &containerFilter);
%end;

Expand Down
100 changes: 39 additions & 61 deletions labkey-api-sas/macros/labkeymakefilter.sas
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,53 @@
Creates a filter to pass as the %labkeySelectRows filter= parameter. Multiple filters can be specified.
The MISSING and NOT_MISSING operators don't require a value; all other operators require a value.

The string generated by this macro consists of calls to the command.addFilter() methods.
Quote column names and values with single quotes: SAS resolves & and % inside double quotes before this
macro runs. Embed a single quote by doubling it. Quoted values may contain spaces and commas.

The macro saves its arguments unresolved in a global macro variable. The generated data step code reads
them back with SYMGET, parses them, and calls command.addFilter() for each filter.
*/
%macro labkeyMakeFilter/parmbuff;
%global _lk_filter_count;
%local name;

/*
Eliminate the parentheses
A distinct variable per call keeps filters that are saved in macro variables separate
*/
%let params = %qsubstr(&syspbuff, 2, %length(&syspbuff) - 2);
%let _lk_filter_count = %eval(0&_lk_filter_count + 1);
%let name = _lk_filter&_lk_filter_count;
%global &name;
%let &name = %superq(syspbuff);

%let code = ;
%let num = 1;
%let column = %scan(&params, &num, %str( ,));
%nrstr(length _lk_args _lk_column _lk_operator _lk_value $32767; drop _lk_:;)
%nrstr(_lk_args = symget%(%')&name%nrstr(%'%);)
%nrstr(
_lk_args = strip(_lk_args);
_lk_args = substrn(_lk_args, 2, length(_lk_args) - 2);
_lk_i = 1;
_lk_column = dequote(scan(_lk_args, _lk_i, ' ,', 'q'));

/*
Loop until no more columns
*/
%do %while(&column ne);
/*
Get the operator
*/
%let num = %eval(&num + 1);
%let operator = %scan(&params, &num, %str( ,));
do while (not missing(_lk_column));
_lk_operator = upcase(dequote(scan(_lk_args, _lk_i + 1, ' ,', 'q')));

%if %index("EQUAL" "DATE_EQUAL"
"NOT_EQUAL" "DATE_NOT_EQUAL"
"NOT_EQUAL_OR_MISSING"
"GT" "GREATER_THAN" "DATE_GT" "DATE_GREATER_THAN"
"LT" "LESS_THAN" "DATE_LT" "DATE_LESS_THAN"
"GTE" "GREATER_THAN_OR_EQUAL" "DATE_GTE" "DATE_GREATER_THAN_OR_EQUAL"
"LTE" "LESS_THAN_OR_EQUAL" "DATE_LTE" "DATE_LESS_THAN_OR_EQUAL"
"STARTS_WITH" "DOES_NOT_START_WITH"
"CONTAINS" "DOES_NOT_CONTAIN"
"CONTAINS_ONE_OF" "CONTAINS_NONE_OF"
"IN" "NOT_IN"
"BETWEEN" "NOT_BETWEEN"
"Q" "WHERE"
"MEMBER_OF" &operator) %then
%do;
if indexw('MISSING NOT_MISSING MV_INDICATOR NO_MV_INDICATOR', strip(_lk_operator)) then do;
command.callVoidMethod('addFilter', strip(_lk_column), strip(_lk_operator));
_lk_i = _lk_i + 2;
end;
else if indexw('EQUAL DATE_EQUAL NOT_EQUAL DATE_NOT_EQUAL NOT_EQUAL_OR_MISSING GT GREATER_THAN DATE_GT DATE_GREATER_THAN LT LESS_THAN DATE_LT DATE_LESS_THAN GTE GREATER_THAN_OR_EQUAL DATE_GTE DATE_GREATER_THAN_OR_EQUAL LTE LESS_THAN_OR_EQUAL DATE_LTE DATE_LESS_THAN_OR_EQUAL STARTS_WITH DOES_NOT_START_WITH CONTAINS DOES_NOT_CONTAIN CONTAINS_ONE_OF CONTAINS_NONE_OF IN NOT_IN BETWEEN NOT_BETWEEN Q WHERE MEMBER_OF', strip(_lk_operator)) then do;
/*
For operators that require a value, set the column, operator, and value.
Values are always sent as strings; the server chokes on a double value for an integer column
*/
%let num = %eval(&num + 1);
%let value = %scan(&params, &num, %str( ,));
_lk_value = dequote(scan(_lk_args, _lk_i + 2, ' ,', 'q'));
command.callVoidMethod('addFilter', strip(_lk_column), strip(_lk_operator), trim(_lk_value));
_lk_i = _lk_i + 3;
end;
else do;
put 'ERROR: Invalid operator: ' _lk_operator;
abort;
end;

/*
Get the value and ensure it's surrounded with quotes. We need to send all values
(even numerics) as strings, otherwise the server will choke when filtering on integer
columns with a double value.
*/
%let value = "%sysfunc(compress(&value, %str(%'%")))";
%let code = &code command.callVoidMethod('addFilter', &column, &operator, &value)%str(;);
%end;
%else
%do;
%if %index("MISSING" "NOT_MISSING" "MV_INDICATOR" "NO_MV_INDICATOR", &operator) %then
%do;
/*
For operators that don't require a value, just set the column and operator.
*/
%let code = &code command.callVoidMethod('addFilter', &column, &operator)%str(;);
%end;
%else
%do;
%put ERROR: Invalid operator: &operator;
%abort;
%end;
%end;

%let num = %eval(&num + 1);
%let column = %scan(&params, &num, %str( ,));
%end;
%quote(&code);
_lk_column = dequote(scan(_lk_args, _lk_i, ' ,', 'q'));
end;
)
%mend labkeyMakeFilter;
2 changes: 1 addition & 1 deletion labkey-api-sas/macros/labkeyselectrows.sas
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
length title $1000;
title = cat("Schema: '", &schemaName, "', Query: '", &queryName, "'");

%if &viewName ne %then %do;
%if %length(%superq(viewName)) %then %do;
title = cat(title, ", View: '", &viewName, "'");
%end;

Expand Down
Loading