I have a parameterized query with various criteria specified for the WHERE clause, set by macro variables:
%LET criteria1 = 'XXXX';
%LET criteria2 = 'YYYY';
Is there a wildcard I can use to essentially remove the criteria for the second variable?
PROC SQL;
SELECT *
FROM Table
WHERE
Field1 IN (&criteria1.)
AND Field2 IN (&criteria2.)
;QUIT;
I do not want users to change the query themselves at all, but have an option when paramterizing to bypass the second filter.
You will need to add some code generation logic. First decide on a special value to use, such as ALL, or just test if the parameter is empty. Then you can use macro code to conditionally add the clauses to the WHERE.
PROC SQL;
SELECT *
FROM Table
WHERE 1=1
%if (%length(&criteria1) and "&criteria1" ne "ALL") %then %do;
AND Field1 IN (&criteria1.)
%end;
%if (%length(&criteria2) and "&criteria2" ne "ALL") %then %do;
AND Field2 IN (&criteria2.)
%end;
;
QUIT;
One option would be to reformulate your approach and parameterize the entire predicate, as well as introduce a mechanism for default values. I will draft my proposal in Pseudo-code.
Given:
%LET criteria1 = 'XXXX';
%LET criteria2 = 'YYYY';
Your query would then be similar to (note the pseudo syntax: is_defined(...) ? {predicate} else {pass_through}):
SELECT *
FROM Table
WHERE
is_defined(&criteria1) ? Field1 IN (&criteria1) else 1=1
AND is_defined(&criteria2) ? Field2 IN (&criteria2) else 1=1
If either criteria is left out, then all rows filter through for the corresponding predicate.
Related
I have a libY.tableX that have for each record some SQL strings like the ones below and other fields to write the result of their execution.
select count(*) from libZ.tableK
select sum(fieldV) from libZ.tableK
select min(dsitact) from libZ.tableK
This my steps:
the user is prompted to select a lib and table and the value is passed to the vars &sel_livraria and &sel_tabela;
My 1st block is a proc sql to get all the sql string from that record.
My 2nd block is trying to concrenate all that strings to use further on to update my table with the results. The macro %isBlank is the one recommended by Chang CHung and John King in their sas papper;
My 3th block is to execute that concrenated sql string and update the table with results.
%macro exec_strings;
proc sql noprint ;
select livraria, tabela, sql_tot_linhas, sql_sum_num, sql_min_data, sql_max_data
into :livraria, :tabela, :sql_tot_linhas, :sql_sum_num, :sql_min_data, :sql_max_data
from libY.tableX
where livraria='&sel_livraria'
and tabela='&sel_tabela';
quit;
%LET mystring1 =%str(tot_linhas=(&sql_tot_linhas));
%LET separador =%str(,);
%if %isBlank(&sql_sum_num) %then %LET mystring2=&mystring1;
%else %LET mystring2= %sysfunc(catx(&separador,&mystring1,%str(sum_num=(&sql_tot_linhas))));
%if %isBlank(&sql_min_data) %then %LET mystring3=&mystring2 ;
%else %LET mystring3= %sysfunc(catx(&separador,&mystring2,%str(min_data=(&sql_min_data))));
%if %isBlank(&sql_max_data) %then %LET mystring0=&mystring3;
%else %LET mystring0= %sysfunc(catx(&separador,&mystring3,%str(max_data=(&sql_min_data))));
%PUT &mystring0;
proc sql noprint;
update libY.tableX
set &mystring0
where livraria='&sel_livraria'
and tabela='&sel_tabela';
quit;
%mend;
My problem with the code above is that iam getting this error in my final concrenated string, &mystring0.
tot_linhas=(&sql_tot_linhas),sum_num=(&sql_tot_linhas),min_data=(&sql_min_data),max_data=(&sql_min_data)
_ _ _ _
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, a missing value, BTRIM, INPUT, PUT, SUBSTRING, USER.
Any help appreciated
Ok, so i follow Tom comments and ended with a proc sql solution that works!
proc sql;
select sql_tot_linhas,
(case when sql_sum_num = '' then "0" else sql_sum_num end),
(case when sql_min_data = '' then "." else sql_min_data end),
(case when sql_max_data = '' then "." else sql_max_data end)
into:sql_linhas, :sql_numeros, :sql_mindata, :sql_mxdata
from libY.tableX
where livraria="&sel_livraria"
and tabela="&sel_tabela";
quit;
proc sql;
update libY.tableX
set tot_linhas = (&sql_linhas),
sum_num =(&sql_numeros),
min_data = (&sql_mindata),
max_data = (&sql_mxdata)
where livraria="&sel_livraria"
and tabela="&sel_tabela";
quit;
Tks Tom :)
It is very hard to tell from your description what it is you are trying to do, but there are some clear coding issues in the snippets of code you did share.
First is that macro expressions are not evaluated in string literals bounded by single quotes. You must use double quotes.
where livraria="&sel_livraria"
Second is you do not want to use any of the CAT...() SAS functions in macro code. Mainly because you don't need them. If you want to concatenate values in macro code just type them next to each other. But also because they do not work well with %SYSFUNC() because they allow their arguments to be either numeric or character so %SYSFUNC() will have to guess from the strings you pass it whether it should tell the SAS function those strings are numeric or character values.
So perhaps something like:
%let mystring=tot_linhas=(&sql_tot_linhas);
%if not %isBlank(&sql_sum_num) %then
%LET mystring=&mystring,sum_num=(&sql_tot_linhas)
;
%if not %isBlank(&sql_min_data) %then
%LET mystring=&mystring,min_data=(&sql_min_data)
;
%if not %isBlank(&sql_max_data) %then
%LET mystring=&mystring,max_data=(&sql_max_data)
;
Note that I also cleaned up some obvious errors when modifying that code. Like the extra & in the value passed to the %ISBLANK() macro and the assignment of the min value to the max variable.
But it would probably be easier to generate the strings in a data step where you can test the values of the actual variables and if needed actually use the CATX() function.
I am writing a SAS macro that takes two params, the first is the name of a dataset, the second param is a string that will actually determine one of the output columns:
%macro test(data, input_mth);
%if &comp_mth.=October %then %do;
cmp_basis=Last Fiscal End;
%end;
proc sql;
create table final as select &cmp_basis. as col1, data.col2 from data;
quit;
%mend;
%test(data, October);
basically, I pass in a dataset and if I pass the string 'October', then the output will show 'Last Fiscal End; as the first column. If I pass in January, it will show 'Calendar beginning' etc etc.
The %if block gives me error:
Statement is not valid or it is used out of proper order.
Just reading through your code in order let's identify some of the issues.
First your %IF statement is referencing a macro variable COMP_MTH that is not defined anywhere in your program. I assume you meant to refer to one of your input parameters instead.
%if &input_mth.=October %then %do;
Second you have data step statements inside your %DO/%END block but you never started a data step. I assume that you mean to create a macro variable there. So use a %let statement.
%let cmp_basis=Last Fiscal End;
But you also need to define that macro variable as local or else your macro will overwrite any macro variable with the same name in the calling program's environment.
You also need to make sure that your macro is generating valid SAS code. So in your SQL code you have
select &cmp_basis. as col1
But if we just replace the macro variable with the value you are assigning above we this gibberish statement:
select Last Fiscal End as col1
I assume you meant to create a character variable there, so add quotes so that you are generating a character constant.
select "&cmp_basis." as col1
You also have a logic problem. What value do you want for COL1 when it is not October? One way to deal with that is to set a default value to the macro variable before your logic. But perhaps you meant to use the input month? So perhaps you just need to add a %else clause?
You are also never using your other input parameter. Let's assume that you mean to pass in the name of the dataset that the SQL should query. So you want to use
from &data
But then your SQL table alias in DATA.col2 is never defined. So make sure to either assign an alias to your input table, or for this simple one table query just drop the alias when referencing the column name.
So we end up with something like this:
%macro test(data, input_mth);
%local cmp_basis;
%if &input_mth.=October %then %do;
%let cmp_basis=Last Fiscal End;
%end;
%else %let cmp_basis=&input_mth;
proc sql;
create table final as
select "&cmp_basis." as col1
, x.col2
from &data x
;
quit;
%mend test;
Of for such simple logic we could dispense with the extra macro variable and just use the macro logic to conditionally generate the constant value that you want to use as the value of COL1.
%macro test(data, input_mth);
proc sql;
create table final as
select
%if &input_mth.=October %then "Last Fiscal End";
%else "&input_mth";
as col1
, x.col2
from &data x
;
quit;
%mend test;
I figure it out, correct syntax is:
%macro test(data, input_mth);
%if &comp_mth.=prv %then %do;
%let cmp_basis=Last Fiscal End;
%end;
proc sql;
create table final as select "&cmp_basis." as col1, data.col2 from data;
quit;
%mend;
%test(data, October);
Your code have numerous errors of statement concept. If you more give me more details about you need, I help you better.
But I try understanded you problem, and I sugested two soluctions.
option I
%macro test(data, input_mth);
%if &comp_mth. = "October" %then %do;
cmp_basis = Last /*Fiscal End*/;
%end;
proc sql;
create table final as select
"&cmp_basis." as col1,
col2
from &data.;
quit;
%mend;
%test(data, October);
option II
%macro test(data, input_mth);
%if &comp_mth.=prv %then %do;
%let cmp_basis = Last Fiscal End; /* this is a Vector that contains string at Last, Fiscal and End*/
%end;
proc sql;
create table final as select /* You create a table call final */
"&cmp_basis." as col1, /* column call October */
/* data.col2 */ /* this no have sense - what is this ? */
col2 /*Correct way to call col2 if it exists on data*/
from &data.; /* Your data set, you call in macro */
quit;
%mend;
%test(data, October);
Could someone please explain why it is happening?
I'm trying to find objects dependencies tree in a DB.
Let's say view5 is a view sits on top view4 which sits on top view1.
Also,
view3 sits on top view2 sits on top view1.
So,
the when I query the macro for view1, I should get back view4, view5, view2 and view3.
This is the macro:
%macro dependencies(obj=);
%let dependent_objectname =;
proc sql noprint;
select "'"||trim(dependent_objectname)||"'"
into :dependent_objectname separated by ", "
from &_input.
where src_objectname in (&obj.);
quit;
%put &dependent_objectname.;
%let dependent_objectname = (&dependent_objectname.);
%put &dependent_objectname.;
%if %length("&dependent_objectname")>0 %then
%dependencies(obj = &dependent_objectname.);
%mend dependencies;
%let source = 'ditemp.depend_test1';
%put &source.;
%dependencies(obj = &source.);
First iteration works well,
I get the objects sit on top depend_test1
in a form of "('ditemp.depend_test2','ditemp.depend_test3')"
then I'm checking for the length of variable dependent_objectname (greater than zero)
and calling the macro again,
only it never stops...
I see a couple problems.
The statement:
%if %length("&dependent_objectname")>0 %then %do;
will always return true, even if the value of &dependent_objectname is null. Because the quotes are part of the value in the macro language. You probably want:
%if %length(&dependent_objectname)>0 %then %do;
That test for nullness usually works. Or see this paper for better methods. http://support.sas.com/resources/papers/proceedings09/022-2009.pdf
Before that, the statement:
%let dependent_objectname = (&dependent_objectname.);
is adding parentheses to your value. So again, even if &dependent_objectname were null, it would be () after this. It looks like you don't need these parentheses, so I would skip this statement.
I would also add:
%local dependent_objectname ;
to the top of the macro. That way each invocation of the macro will have its own local macro variable, rather than having them all use the macro variable created in the first iteration (or worse yet, all use a global macro variable).
You have sensibly added %PUT statements to help with debugging. I would expect they would show that the value of &dependent_objectname is always non-null as currently written. You could also add:
%put The length is: %length(&dependent_objectname.) ;
Since you are using an SQL query to generate the dependent list you can use the automatic variable SQLOBS in your test to break the recursion.
%if &sqlobs %then %do;
%dependencies(obj = &dependent_objectname.);
%end;
Also do NOT use a comma as the delimiter between the items listed in the OBJ parameter. The IN operator in SAS doesn't need them and they will cause trouble in the macro call.
select * from sashelp.class where name in ('Alfred' 'Alice') ;
So your macro could look like this:
%macro dependencies(object_list);
%local dependent_list ;
proc sql noprint;
select catq('1as',dependent_objectname)
into :dependent_list separated by ' '
from &_input.
where src_objectname in (&object_list)
and dependent_objectname is not null
;
quit;
%put Dependent Objects of (&object_list) = (&dependent_list);
%if &sqlobs %then %dependencies(&dependent_list);
%mend dependencies;
And here is a test case.
%let _input=sample;
data sample;
length src_objectname dependent_objectname $41 ;
input (_all_) (:) ;
cards;
object1 object2
object2 object3
object2 object4
;;;;
%dependencies('object1');
I have a list of IDs that contain multiple "XO codes". I want to create a macro that will loop through these IDs and create a table for each using a where statement that corresponds to the appropriate XO code. EX:
%let ID_77= '35X02','35X04';
%let DnO_IDs= &ID_77; /intends to add more &ID_ numbers/
%macro loop;
proc sql;
%do k=1 %to %sysfunc(countw(&DnO_IDs_Ids.));
%let ID= %scan(&DnO_IDs.,&k.);
create table EP_&ID as
select * from table
where XO in ("&ID.") and AY>=(&CurrY-14);
%end;
quit;
%mend;
%loop;
I am receiving this error:
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
'35X04'
ERROR: Argument 2 to macro function %SCAN is not a number.
ERROR: The macro LOOP will stop executing.
The quotes are an issue and should not be needed. Using comma as your delimiter can also cause trouble. It would work better to use spaces.
%let ID_77= 35X02 35X04;
%let DnO_IDs= &ID_77; /intends to add more &ID_ numbers/
%let CurrY=2015;
%macro loop;
%local k id ;
proc sql;
%do k=1 %to %sysfunc(countw(&DnO_IDs));
%let ID= %scan(&DnO_IDs,&k);
create table EP_&ID as
select * from table
where XO in ("&ID") and AY>=(&CurrY-14)
;
%end;
quit;
%mend;
%loop;
If you do want to use the values as quoted comma separated lists then you need to modify your COUNTW() and %SCAN() function calls appropriately and add a call to DEQUOTE() to remove the quotes.
%let ID_77= '35X02','35X04';
... %sysfunc(countw(%superq(DnO_IDs),%str(,)));
%let ID= %sysfunc(dequote(%scan(%superq(DnO_IDs),&k,%str(,))));
I think this should resolve the issue:
%do k=1 %to %eval(%sysfunc(countw(&DnO_IDs_Ids.)));
Might be %sysfunc return value is considered non integer.
I am using %SYMEXIST to check if a macro variable exists and then continue or skip based on the result. It sounds so simple but SAS is throwing errors for all the approaches I have tried so far.
&num_tables is a macro created from a dataset based on certain conditions.
proc sql noprint;
select distinct data_name into :num_tables separated by ' '
from TP_data
where trim(upcase(Data_Name)) in
(select distinct(trim(upcase(Data_Name))) from Check_table
where COALESCE(Num_Attri_DR,0)-COALESCE(Num_Attri_Data,0) = 0
and Name_Missing_Column eq ' ' and Var_Name eq ' ');
quit;
If this macro var is not resolved or not created (no rows selected from the dataset), I would like to skip. When I used,
%if %symexist(num_tables) %then %do;
SAS gives an error with the message "MACRO variable name X must start with a letter or underscore". So I tried removing leading spaces using all of the following approaches:
%let num_tables = &num_tables; /* approach 1 */
%let num_tables = %sysfunc(trim(&num_tables)) /* approach 2 */
%let num_tables = %trim(&num_tables) /* approach 3 */
But none of these worked. I still get the error "MACRO variable name X must start with a letter or underscore"
Likely you are prefacing the num_tables in symexist with a &. This is the correct way to implement %SYMEXIST in the manner you ask. Note that the argument to %symexist is not &num_Tables but num_tables (the actual name of the macro variable). &num_tables would resolve to whatever its contents would be if you used it with the &.
%macro testshort(char=);
proc sql noprint;
select distinct name into :num_tables separated by ' '
from sashelp.class
where substr(name,1,1)="&char.";
quit;
%if %symexist(num_tables) %then %do;
%put Tables: &num_tables;
%end;
%mend testshort;
%testshort(char=A);
%testshort(char=B);
%testshort(char=Z);