Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail On a Condition

Status
Not open for further replies.

ApocY85

Programmer
Dec 20, 2007
36
US
I am just starting out with SAS, and have a project to work on. I really do not know SAS very well at all, so please bear with me :)

I would like to send an e-mail only if there are no observations in a data set (cust2). If someone could tell me why an e-mail is always being sent regardless of the count of observations, I'd greatly appreciate it. On top of that, the e-mail is being sent with no message (body).

Also, it would be very helpful if anyone could point me to a website/book/other source where I can teach myself SAS.

The code for my current problem is below:



filename cust "/datatmp2/Customer Survey Raw Data.csv";

filename mail email ' '
TO = ("abc@123.com")
FROM = ("123@abc.com")
SUBJECT = "[Test] 11/30";

data cust; infile cust dlm='~' dsd truncover missover firstobs=2;
input A B: $10. C: $10. D: $25. E: $1. F: $1. G: $1. H: $500.;
run;

data cust; set cust;
newAformatted = put(mdy(01,01,1900)+A-2,date9.);
newA = mdy(01,01,1900)+A-2;
run;

proc sql;
create table cust2 as
select *
from cust
where newA >= mdy(11,30,2009);
run;

data _null_;
dsid = open('cust2','i');
n_obs = attrn(dsid,'nobs');

if n_obs = 0 then
do;
file mail;
put "This is a test."; put n_obs;
end;
run;
 
I would imagine that it is n_obs is probably 0 hence it callling the statement. What does your put statement say?

put n_obs=; should output it to the log. Also, your email is blank probably because you aren't placing anything in the variable test. Best would to use a macro variable. Called as &test and you can use a the following statement to put your statement into the macro variable:

CALL SYMPUT('nobs',n_obs);
CALL SYMPUT('test',"Number of variables" &nobs");
 

First off, thanks for your response :)

I will elaborate to hopefully clarify things a bit more. There is no variable 'test'.

Based off of the data being used, with the query looking for all records with 'newA' >= mdy(11,30,2009), there should result several records. I opened up the 'cust2' dataset, and sure enough, there is 137 records. If I change the query to look for records where 'newA' >= mdy(12/1/2009), there should be 0 results. When I open up 'cust2', a message box pops up that says "Data set has 0 observations". From this point, everything appears to work as intended.

Now, if I run the SAS program with the 12/1 query, an e-mail is sent with a body of "This is a test. 0", which again is as expected. If I run the program with the 11/30 query, an e-mail is still sent, of which contains no body. It does, however, contain a subject, to, and from value. I was hoping the 11/30 version of the program would produce no e-mail whatsoever.

My theory is that the if statement is working correctly because the line 'put "This is a test."; put n_obs;' doesn't appear to be executed for the 11/30 version, but does for the 12/1 version (as expected). However, somehow the mail is still being sent (and has the subject, to and from fields populated because of the 'filename mail email' line. Of course, this is just a theory from a SAS newbie :)

I hope by elaborating I have helped someone in pinpointing the problem I am having. Thank you once again for your time and patience, and any replies I may get :)
 
Another thing to note, I just commented out the "file mail;" line and ran the program for both versions (11/30 and 12/1). The 'put "This is a test."; put n_obs;' line only produced output for the 12/1 version, as expected. However, no mail was sent for either version. Although this is what you would expect considering the fact that the line was commented out, this demonstrates that the "file mail;" line is being executed whether the if statement is true or false.

Any thoughts?
 
the File statement is a type of command (non-executable?) which is run before the step is compiled, therefore it creates the output file no matter WHAT happens, but only writes to it if the conditions are met.
The way to do this is to use macros.
Code:
%macro condml;
  %if nobs=0 %then 
  %do ;
    data _null_;
       file mail;
       put "This is a test.";
    run;
  %end;
%mend condml;

%condml;
That should give oyu the structure you need. You just need to create a macro variable nobs with the number of records in it.
One thing to note is that macro variables are text strings so from memory (I'm a bit rusty) you can use < or >. If you want to do that you'll need to use the %eval macro function.
Let me know if you need further guidance on that.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top