I have to run the below query weekly once. If the record_cnt > 0 then it needs to send an automatic email to lead.
proc sql;
select count(*) into :record_cnt
from table1 where id not in (select id from table2);
I have tried the below code, however it not worked well.
data _null;
%if &record_cnt > 0 %then %do;
filename msg email
to="lead@core.com"
from="replies-disabled@company.com"
subject = "New record_found";
data _null_;
file msg;
put "%trim(&record_cnt.) record found.";
end;
run;
Kindly suggest the appropriate solution,
Thanks, in advance!
Thanks for your suggestions @sbxkoenk and @Patrick
The below one works.
proc sql;
select count(*) into :record_cnt
from table1
where id not in (select id from table2);
%let count=&record_cnt;
quit;
%macro test;
%if &count. > 0 %then %do;
filename msg email
to="leademail@company.com"
from="myemail@company.com"
subject = "Alert: New Record Found";
data _null_;
file msg;
put "Hi,";
put "%trim(&count.) new records found in the dataset. Please take action.";
%end;
run;
%mend;
%test
Make sure SMTP e-mail server is set up!
Then look here:
Then do something like this, but write to mail and not to print destination!
%LET record_cnt=7;
%IF &record_cnt.>0 %THEN %DO;
data _null_;
file print;
record_cnt=strip(&record_cnt.);
put record_cnt 'record found.';
run;
%END;
/* end of program */
Koen
What do you mean by "not worked well"?
In the code you shared make sure the Proc SQL ends with a QUIT;
The first "data _null_;" statement doesn't make sense and should get removed.
I assume the missing macro %end statement just got "cut off" when you copy/pasted your code?
Thanks for your suggestions @sbxkoenk and @Patrick
The below one works.
proc sql;
select count(*) into :record_cnt
from table1
where id not in (select id from table2);
%let count=&record_cnt;
quit;
%macro test;
%if &count. > 0 %then %do;
filename msg email
to="leademail@company.com"
from="myemail@company.com"
subject = "Alert: New Record Found";
data _null_;
file msg;
put "Hi,";
put "%trim(&count.) new records found in the dataset. Please take action.";
%end;
run;
%mend;
%test
@Sathish_jammy I strongly recommend to amend your code as per the highlighted lines below.
If your condition &count>0 is false then your macro will currently still generate a run; statement.
As for the SQL the following code would suffice and populate macro var &record_cnt the same as your code.
proc sql;
select count(*) into :record_cnt trimmed
from table1
where id not in (select id from table2);
quit;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.