BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Sathish_jammy
Lapis Lazuli | Level 10

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

 

View solution in original post

4 REPLIES 4
sbxkoenk
SAS Super FREQ

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

Patrick
Opal | Level 21

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?

Patrick_0-1698791666009.png

 

Sathish_jammy
Lapis Lazuli | Level 10

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

 

Patrick
Opal | Level 21

@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.

Patrick_0-1698826830000.png

 

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;

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1574 views
  • 3 likes
  • 3 in conversation