Is there a function to check if an element exist in a macro variable? - sql

i have a table Z with two columns a(string) and b(integer)
the elements in a change for every session so i crate a macro variable to each session !
%Let Fe='abc','def','mno';
%let mylist = &session_1. &session_2. &session_3.;
%macro print_session_3(mylist) / minoperator ;
%let session_3=session_3;
%IF &session_3. in &mylist. %THEN %DO;
proc sql;
create table DATA.session_3 as
select a , b
from source.Z
where a in &Fe.
and b>4
;quit;
%end;
%mend;
%print_session_3(&mylist);
I hope that's clear enough for u , The error is :"Expecting a )"

It means that you missed a ).
You should use in operator with list of values in () in where statement.
It will look like:
where a in (&Fe.)

Related

Conditional PROC SQL on SAS

I have a SAS Script containing multiple PROC SQLs. The question is that the SQL Query should be "adapted" based on a SAS variable value, for example :
%let COD_COC =(52624, 52568, 52572);
%let COD_BLOC = ();
proc sql;
create table work.abordados as
select t1.cd_acao,
t1.cd_bloc,
t1.cd_tip_cnl,
t1.cd_cmco,
t1.cd_cli,
datepart(t1.ts_ctt) format=date9. as data_abordagem,
intnx('day',datepart(t1.ts_ctt), &Prazo_Compra) format=date9. as data_limite
from db2coc.ctt_cli_cmco t1
where (t1.cd_acao in &COD_COC)
and (t1.cd_bloc in &COD_BLOC) <<<<<<< facultative filter
;quit;
The question is that the second filter (t1.cd_bloc in &COD_BLOC) should be applied only if the %let COD_BLOC = (); is different of "()".
I´ve been reading about "match/case" on SQL but as far as I know, this test applies to results of queries/values. On my case, what I must test is the SAS variable.
How handle this?
Knowing you want to apply the COD_BLOC in-list filter only when there are one or more values, AND that a proper in-list will have at least 3 source code characters (*), you can test the length as the criteria for using the macro variable.
When the %IF is in open code you need a %do %end block as follows:
...
%if %length(&COD_BLOC) > 2 %then %do;
and t1.cd in &COD_BLOC
%end;
...
When the code is inside a macro, you can use the above or the below
...
%if %length(&COD_BLOC) > 2 %then
and t1.cd in &COD_BLOC
;
...
Another possible coding solution is to use %sysfunc(IFC(...)) to conditionally generate code
...
%sysfunc(ifc(%length(&COD_BLOC) > 2
, and t1.cd in &COD_BLOC
, %str()
))
...
Two good ways to do this, I think.
First: the easy hack.
%let COD_COC =(52624, 52568, 52572);
%let COD_BLOC = (and t1.cd_bloc in (...));
%let COD_BLOC = ;
proc sql;
create table work.abordados as
select t1.cd_acao,
t1.cd_bloc,
t1.cd_tip_cnl,
t1.cd_cmco,
t1.cd_cli,
datepart(t1.ts_ctt) format=date9. as data_abordagem,
intnx('day',datepart(t1.ts_ctt), &Prazo_Compra) format=date9. as data_limite
from db2coc.ctt_cli_cmco t1
where (t1.cd_acao in &COD_COC)
&COD_BLOC <<<<<<< facultative filter
;quit;
Tada, now it is just ignored. Comment out or delete the second line if you want it to be used (and put values in the ... ).
Second, the more proper way, is to use the macro language. This is more commonly done in a macro, but in 9.4m7 (the newest release, and a few years old now) you can do this in open code.
%let COD_COC =(52624, 52568, 52572);
%let COD_BLOC = ();
proc sql;
create table work.abordados as
select t1.cd_acao,
t1.cd_bloc,
t1.cd_tip_cnl,
t1.cd_cmco,
t1.cd_cli,
datepart(t1.ts_ctt) format=date9. as data_abordagem,
intnx('day',datepart(t1.ts_ctt), &Prazo_Compra) format=date9. as data_limite
from db2coc.ctt_cli_cmco t1
where (t1.cd_acao in &COD_COC)
%if %sysevalf(%superq(COD_BLOC) ne %nrstr(%(%)),boolean) %then %do;
and (t1.cd_bloc in &COD_BLOC) <<<<<<< facultative filter
%end;
;quit;
You have to be careful with the ne () bit because () are macro language syntax elements, hence the long %nrstr to make sure they're properly considered characters. (%str would be okay too, I just default to %nrstr.)

SAS - Replacing substrings in a macro variable containing semicolons

I need to be able to copy paste a long SQL script that contains variables back and forth between Aginity Workbench and SAS. To make this easier, I've been storing the SQL query in a macro variable like this:
(take note of the Netezza style variables)
%let myQuery = %str(
DROP TABLE this;
SELECT *
INTO SomeTable
FROM OtherTable
WHERE field = ${myVariable};
UPDATE TABLE foo
SET x = 1
WHERE
field = ${anotherVariable};
);
When my SAS program runs, I need it to replace the ${netezzaVariables} with text from other macro variables that are determined earlier in the process flow. So far, I've not been able to successfully replace text within this macro variable and I am suspicious that the semicolons are causing issues.
Here's what I'm attempting to do below:
%let formattedText = %sysfunc(tranwrd(&myQuery,'${myVariable}','replacementText'));
The log for that shows:
NOTE: Line generated by the macro function "SYSFUNC".
DROP TABLE this;
! SELECT <the rest of the query is printed to console here>
I didn't go on writing the rest of the log above, because the error is on the word SELECT, with a red line underneath it. Just below this red line is the text:
ERROR 180-322: Statement is not valid or it is used out of proper order.
In fact, if I go on scrolling through the log, every first set of characters following a semicolon in the query is underlined with that same exact error code.
This leads me to believe that SAS picked up the semicolon before that SELECT, used it to terminate what I was doing, and now thinks the text following this semicolon is out in the open code.
In conclusion, I need to know how to replace substrings within a macro variable who's value is a large string containing semicolons.
Thanks in advance!
You don't need to use quotes when using string functions with %sysfunc(). In this case %qsysfunc is what you're looking for, I think:
%let myQuery = %str(
DROP TABLE this;
SELECT *
INTO SomeTable
FROM OtherTable
WHERE field = ${myVariable};
UPDATE TABLE foo
SET x = 1
WHERE
field = ${anotherVariable};
);
%put &myQuery;
%let formattedText = %qsysfunc(tranwrd(&myQuery,${myVariable},replacementText));
%put &formattedText;
Not the most elegant of solutions but it does the job:
%let myQuery = %str(
DROP TABLE this;
SELECT *
INTO SomeTable
FROM OtherTable
WHERE field = ${myVariable};
UPDATE TABLE foo
SET x = 1
WHERE
field = ${anotherVariable};
);
data _null_;
call symput('formattedtext',tranwrd("%quote(%superq(myquery))","${myVariable}","replacementText"));
run;
%put %superq(formattedText);
There's probably a way to do it using only macro functions but I couldn't get it to work.
For your particular example, the call symput could have been simplified to
call symput('formattedtext',tranwrd("&myquery","${myVariable}","replacementText"));
but this would fail if your query contained double quotes whereas the way I wrote it above supports that.
Joshua:
A general purpose resolver is useful if your 'templated' expression has many parameters. Note: a template is different than a parameterized query, and potentially more dangerous.
Without a resolver you will need to code a TRANWRD for each parameter.
Consider this macro that assumes a template contains parameters that are specified by ${macro-var} and parameters are replaced with the macro-var value. Also presume there are no parameters that start with underscore (_) which could collide with the macros internal variables.
%macro resolver(_template);
%local _result;
%local _tokenRx;
%local _start _stop _position _length _token _macrovar _guard;
%let _tokenRx = %sysfunc(prxparse(m/\${([^}]+)}/));
/*%put &=_tokenRx;*/
%let _guard = 0;
%let _start = 1;
%let _stop = %length(&_template);
%let _position = 0;
%let _length = 0;
%let _result = &_template;
%syscall prxnext(_tokenRx, _start, _stop, _template, _position, _length);
%do %while (&_position > 0);
/* %put &=_start &=_stop &=_position &=_length; */
%let _token = %qsubstr(&_template,&_position,&_length);
%let _macrovar = %substr(&_token,3,%eval(%length(&_token)-3));
/*
%put &=_token;
%put &=_macrovar;
*/
%if %symexist(&_macrovar) %then %do;
%let _result = %qsysfunc(tranwrd(&_result,&_token,&&&_macrovar));
%end;
%syscall prxnext(_tokenRx, _start, _stop, _template, _position, _length);
%let _guard = %eval (&_guard+1);
%if &_guard > 1000 %then %let _position = 0;
%end;
%syscall prxfree(_tokenRx);
%superq(_result)
%mend;
Here is the resolver applied to your templated SQL query (adjusted to Proc SQL).
%let myQuery = %str(
DROP TABLE this
;
INSERT INTO SomeTable /* sas insert syntax */
SELECT * FROM OtherTable
WHERE ${field} = ${target}
;
UPDATE foo
SET x = 1
WHERE
field = ${anotherVariable}
;
);
%let field = name;
%let target = 'Jane';
%let myVariable = XYZ;
%let anotherVariable = 'John';
%put %resolver (%superq(myQuery));
proc sql;
create table this (id int);
create table SomeTable like sashelp.class;
create table OtherTable as select * from sashelp.class;
create table foo as select name as field, 0 as x from sashelp.class;
%unquote(%resolver(%superq(myQUery)))
quit;

A faster way for creating aggregates in SAS SQL

I have a data set named test with variables region, X1, X2, X3, X4......X20.
I would like to take the aggregates of X1-X20 by region level. My code is as follow:
proc sql;
create table test2 as
select sum(X1) as X1, sum(X2) as X2,......, sum(X20) as X20
from test
group by region;
quit;
Would there a faster way to get the sum as opposed to manually typing the sum for 20 of each variables? Thank you!
Why use PROC SQL instead of PROC MEANS?
proc means noprint nway data=test ;
class region ;
var x1-x20;
output out=test2 sum=;
run;
A good use case for SAS macros. Here's a link to a tutorial, but I also highly recommend the purple Carpenter book.
%MACRO do_test2;
proc sql;
create table test2 as
select
%DO i = 1 %TO 20;
sum(X&i) as X&1
%IF &i ^= 20 %THEN , ;
%END;
from test
group by region;
quit;
%MEND;
As #user667489 points out, because the macro code is using a %DO loop, the code block must be defined as a named, invokable macro (%MACRO do_test2 . . . %MEND). Once you've defined it as above, you call it like this:
%do_test2;
Since we have this requirement, we can take advantage of the situation and make the number of repetitions a parameter, in case you ever want to vary it. Then the definition is:
%MACRO do_test2(num_reps);
proc sql;
create table test2 as
select
%DO i = 1 %TO &num_reps;
sum(X&i) as X&1
%IF &i ^= &num_reps %THEN , ;
%END;
from test
group by region;
quit;
%MEND;
Which you would call like this:
%do_test2(20);

Two variables in a macro [SAS]

So, i want to have a macro that has others macros inside.
Here is the code: `
proc sql NOPRINT ;
select id into :l_id separated by ' ' from work.AMOSTRACHU;
select count(*) into :nr_reg separated by ' ' from tdata.work.AMOSTRACHU;
quit;
* check;
%put l_id=&l_id nr_reg=&nr_reg;
%macro ciclo_first();
%do n=1 %to &nr_reg;
%let ref=%scan(&l_id,&n);
%put ref=&ref;
proc sql;
select recetor into : lsus&ref separated by ' ' from tdata.5pct_&ref;
select count(*) into :nrsus&ref separated by ' ' from tdata.5pct_&ref;
quit;
%put lsus&ref=&lsus&ref;
%put nrsus&ref=&nrsus&ref;
%MACRO CICLO_PF_SUSref();
%do n=1 %to &nrsus&ref %by 1;
%let sus=%scan(&lsus&ref,&n);
%put sus=&sus;
%LET I = %EVAL(14);
%DO %WHILE (&I<=24);
*my code (depends on &i and &sus)* (works fine alone)
%LET I = %EVAL(&I+1);
%END;
%END;
%MEND;
%CICLO_PF_SUSref;
%MACRO CICLO_PF_SUS_CSRANK();
%do n=1 %to &nrsus&refm %by 1;
%let sus=%scan(&lsus&ref,&n);
%put sus=&sus;
%CICLO_PF_SUSPEITOSrefmsisdn;
%CICLO_PF_SUS_CSRANK;
my code ( just depends on &sus)/
%END;
%MEND;
%CICLO_PF_SUS_CSRANK;
%end;
%mend;
%ciclo_first;`
I think the major problem is in this part:
%put lsus&ref=&lsus&ref;
%put nrsus&ref=&nrsus&ref;
And the error about that is:
A character operand was found in the %EVAL function or %IF condition
where a numeric operand is required. The condition was:
&nrsus&ref
How can i change this in order to work? I understand that it doesn't make all the sense to have something depending on two, like &nrsus&ref.
the first warnings and errors appears here:
ref=15
WARNING: Apparent symbolic reference LSUS not resolved.
lsus15=&lsus15 WARNING: Apparent symbolic
reference NRSUS not resolved.
nrsus15=&nrsus15 ERROR: Expected semicolon not
found. The macro will not be compiled.
How can i solve this? Have no ideas and it would be really useful to make this macro functional in order to avoid to run this 100 times.
UPDATE [06.08.2015]
I have a table with 100 numbers, that's in
'work.amostrachu'.
I created the macro ciclo_first in order to run the other 2 macros for this list. because, if i replace manually the &ref by the number i want it works fine.
Let's suppose 'work.amostrachu' has:
ID 1 2 3 (...) till n=100
Then, with this part:
proc sql;
select recetor into : lsus&ref separated by ' ' from work.5pct_&ref;
select count(*) into :nrsus&ref separated by ' ' from work.5pct_&ref;
quit;
I want to get the elements that are on the column 'recetor' of work.5pct_&ref.
For ID=1 i would obtain lsus1 composed by, for example, 3 numbers (124,564,859)
And, then, the %MACRO CICLO_PF_SUSref(); will have as input these 3 numbers (that could be 4 or 5 or sometingh else).
(here, i might be calling badly the list of elements i want from 'work.5pct_&ref).
Then, the output of the previous macro would be the input of this one: %MACRO CICLO_PF_SUS_CSRANK.
And that would be all.
The %MACRO CICLO_PF_SUSref() and %MACRO CICLO_PF_SUS_CSRANK works ok if i just replace the &ref by the id. that's why i tried to create a macro that would run these 2 macros for the initial list. if you have best ideas, i would be thankful.
So, i want something that allows me to run this two macros (%MACRO CICLO_PF_SUSref() and `%MACRO CICLO_PF_SUS_CSRANK) for the list i get in the beginning:
proc sql NOPRINT ;
select id into :l_id separated by ' ' from work.AMOSTRACHU;
select count(*) into :nr_reg separated by ' ' from tdata.work.AMOSTRACHU;
quit;
[UPDATE 10.08.2015]
Ok, just read the suggested answers and worked on it.
I have a list, with the identification(numerical) of 100 clients, let's call each client : ref. That's on WORK.AMOSTRACHU.
I wroted the following code and it worked, and will help me explain you what i want:
proc sql NOPRINT ;
select id into :l_id separated by ' ' from work.AMOSTRACHU;
select count(*) into :nr_reg separated by ' ' from work.AMOSTRACHU;
quit;
* check;
%put l_id=&l_id nr_reg=&nr_reg;
%macro lista_ent();
%do n=1 %to &nr_reg;
%put n=&n;
%let ref=%scan(&l_id,&n);
%put ref=&ref;
proc sql;
select recetor into :listae&ref SEPARATED BY ' ' from work.e5pct_id&ref;
select count(*) into :nre&ref separated by ' ' from work.e5pct_id&ref;
quit;
%end;
%mend;
%lista_ent;
Will show you the output for the first 3 cases (of 100, the beggining list in work.amostrachu), it's the results part in SAS:
Recetor
507
723
955
-page break-
3
-page break-
380
500
675
977
984
-page break-
5
-page break-
200
225
351
488
698
781
927
-page break-
7
So, i have the 'values' of the column 'recetor' of the data work.e5pct_id&ref and how many values i have for each ref. (i've showed you results for the first 3 refs, but i have it for the 100).
Now, the first macro:
%MACRO CICLO_M_PF_ref();
%local me n i;
%do n=1 %to nre&ref %by 1;
%let me=%scan(listae&ref,&n);
%put me=&me;
%LET I = %EVAL(14);
%DO %WHILE (&I<=24);
proc sql;
create table work.smthng_&I as
select * from
work.wtv&I
WHERE A=&me OR B=&me;RUN;
PROC APPEND
DATA=work.smthng_&I
BASE=work.pf_&me
FORCE;
RUN;
%LET I = %EVAL(&I+1);
%END;
%END;
%MEND;
%CICLO_M_PF_ref;
My all doubts in the & and && are around here.
So, with the data: I have my first ref whose results of column 'recetor' are
Recetor
507
723
955
-page break-
3
So, i want to run that code for each one of this values. First for '507', then for '723' and then for '955', and i want to do it for all the refs.
So, when the macro finishes to run my code for this 3, i want the macro to skip to the second ref and then run my code for the values of the column 'recetor' for the second ref: 380,500,675,977 and 984.
i used this code:
proc sql;
select recetor into :listae&ref SEPARATED BY ' ' from work.e5pct_id&ref;
select count(*) into :nre&ref separated by ' ' from work.e5pct_id&ref;
quit;
because each one of the refs have different values and the number of them could be different, just as i showed you. so, this whas to tell the macro to run it nre&ref times and for all values in the list listae&ref.
the error is the following:
ERROR: A character operand was found in the %EVAL function or %IF
condition where a numeric operand is required. The condition was:
nre&ref ERROR: The %TO value of the %DO T loop is invalid. ERROR: The macro CICLO_M_PF_REF will stop executing.
I can't quite follow your desired output and macro but here are some things I noticed.
None of your macros take parameters. If you change your macro to take parameters you can call them individually which may help to stream line your process.
I think you want something like this:
%macro def1(param1);
...
%mend;
%macro def2(param2);
...
%mend;
%macro execute();
%do i=1 to 100;
%def1(param1);
%def2(param2);
%end;
%mend;
This still seems a bit awkward, so if you can explain your process with your data there may be a better way overall.
I see a number of issues you could address, but without test data it is hard to evaluate.
When trying to show the value for macro variable x&i you need to double up on the prefix &. So if I=1 and X1 = FRED then &&x&i = FRED.
When pushing values into macro variables from SQL use the automatic macro variable SQLOBS to get the record count. No need to run the query again to get the count.
You cannot select COUNT(*) into multiple macro variables. SQL will just return one count.
SAS dataset or variable names cannot start with a digit (tdata.5pct_&ref) or contain periods (tdata.work.AMOSTRACHU).
Do NOT nest macro definitions. You can nest the calls, but nesting
the definitions is just going to lead to confusion.
Your actual nested macros do not make much sense. What is this variable I that is introduced? It appears to be a constant.
Why not just code them as part of the outer macro? Not much need to make them separate macros if they are only called at one place.
If you do nest them then make sure to define your local macro variables as local to prevent overwriting the values of macro variables with the same name that might exist in an outer macro scope. The N looping variable for your %DO loops for example.
First define your subroutine macros.
%MACRO CICLO_PF_SUSref(ref_list);
* CICLO_PF_SUSref ;
%local n sus;
%do n=1 %to %sysfunc(countw(&ref_list,%str( )));
%let sus=%scan(&ref_list,&n);
%put NOTE: &sysmacroname N=&n SUS=&sus;
%end;
%MEND CICLO_PF_SUSref;
%MACRO CICLO_PF_SUS_CSRANK(ref_list);
* CICLO_PF_SUS_CSRANK ;
%local n sus ;
%do n=1 %to %sysfunc(countw(&ref_list,%str( )));
%let sus=%scan(&ref_list,&n);
%put NOTE: &sysmacroname N=&n SUS=&sus;
%put NOTE: Call macro named: CICLO_PF_SUSPEITOSrefmsisdn;
%end;
%MEND CICLO_PF_SUS_CSRANK;
Then your main macro.
%macro ciclo_first(id_list);
* Start ciclo_first ;
%local n id ;
%do n=1 %to %sysfunc(countw(&id_list,%str( )));
%let id=%scan(&id_list,&n);
proc sql noprint;
select recetor into : lsus&id separated by ' ' from pct_&id;
%let nrsus&id = &sqlobs ;
quit;
%put NOTE: Current ID=&id ;
%put NOTE: &&nrsus&id records read from PCT_&ID ;
%put NOTE: Value List LSUS&id = &&LSUS&id ;
%CICLO_PF_SUSref(&&lsus&id);
%CICLO_PF_SUS_CSRANK(&&lsus&id);
%end;
* End ciclo_first ;
%mend ciclo_first;
Then setup some data and call the main macro.
* Setup test data ;
data AMOSTRACHU;
do id=1 to 2; output; end;
run;
data PCT_1 ;
do recetor='A','B';
output;
end;
run;
data PCT_2 ;
do recetor='C','D';
output;
end;
run;
options mprint;
%ciclo_first(1 2);

SAS macros: using macros in proc sql

How to use macros in SQL? (for every thing, that was selected)
I mean something like this:
&VarTable is a table, which have two variables: (for example) Lib and Table
Each observation in &VarTable is the name of table: Lib.Table
I want to do things for every table:
1) exist?
2) sort it
and last condition:
each table, if it exist, have a variable &VarField.
%macro mSortedTable(vLib,vTab,vVar);
%if %sysfunc(exist(&vLib..&vTab)) %then %do;
proc sort data = &vLib..&vTab;
by &vVar;
run;
&vLib..&vTab
%end;
%else %do; "" %end;
%mend mSortedTable;
proc sql noprint;
select %mSortedTable(vLib=Lib,vTab=Table,vVar=&VarField)
into: AccumVar separated by " "
from &VarTable;
quit;
how to do this with sql and macros?
Do you have to use sql and macros? A simple data step and call execute would do what you need here.
Below is an example that takes a data set that has a list of tables to process, checks to see if the table exists and if it does, sorts it by &VarField. This could be easily extended to sort each table by a custom set of variables if desired.
If the table does not exist, it generates a warning message.
/* create fake data */
data testdat;
length lib $8 table $32;
input lib $ table $;
datalines;
work test1
work test2
work test3
work doesnotexist
;
run;
/* create 3 data sets */
data work.test1 work.test2 work.test3;
input var1 var2 var3;
datalines;
1 34 8
2 54 5
12 5 6
;
run;
/* end create data */
%let VarTable=work.testdat;
%let VarField=var2 var3;
data _null_;
set &VarTable;
dsname=catx('.',lib,table);
if exist(dsname) then do;
call execute("proc sort data=" || strip(dsname) || "; by &VarField; run;");
end;
else do;
put "WARNING: The data set does not exist: " lib= table=;
end;
run;
Call execute is a good solution, however if the data step code being "executed" is complicated (which it is not in this example), I find it hard to debug.
Another method is to put all the variables into macro variables and then loop through them in a macro do-loop;
(building on #cmjohns data)
/* create fake data */
data testdat;
length lib $8 table $32;
input lib $ table $;
datalines;
work test1
work test2
work test3
work doesnotexist
;
run;
/* create 3 data sets */
data work.test1 work.test2 work.test3;
input var1 var2 var3;
datalines;
1 34 8
2 54 5
12 5 6
;
run;
/* end create data */
%let VarTable=work.testdat;
%let VarField=var2 var3;
proc sql noprint;
select count(lib)
into :cnt
from &vartable;
%Let cnt=&cnt;
select strip(lib), strip(table)
into :lib1 - :lib&cnt, :table1 - :table&cnt
from &vartable;
quit;
%Macro test;
%Do i = 1 %to &cnt;
%Let lib=&&lib&i;
%Let table=&&table&i;
%Let dsn=&lib..&table;
%if %sysfunc(exist(&dsn)) %then %do;
Proc sort data=&dsn;
by &varfield;
run;
%end;
%else %do;
%put WARNING: The data set does not exist: &dsn;
%end;
%end;
%Mend;
%test