DATA Step, Macro, Functions and more

How to display dataset record in email template

Accepted Solution Solved
Reply
Contributor
Posts: 63
Accepted Solution

How to display dataset record in email template

Hi Expert,

 

I want to display total records available for particular dataset in the email template. If not available, it should display zero. Can you please help me. I tried with macro but it is not displaying.

data class;
set sashelp.class;
run;

data school;
input stud_id;
cards;
;
run;

FILENAME myemail2 EMAIL  to=("abc@yahoo.com" )
 Subject = "count check";

data _null_;
file myemail2;
Put "Total number of records in dataset class=" & 19;
Put "Total number of records in dataset school=" & 0;
run;

Accepted Solutions
Solution
‎08-11-2017 02:59 AM
SAS Employee
Posts: 9

Re: How to display dataset record in email template

Using the original code, add a check for the automatic variable &sysnobs (which captures the number of obs processed in that data step).  I tweaked the subject so you can also see the counts in the subject without opening the message:

 

data class;                                                                                                                             
set sashelp.class;                                                                                                                      
run;                                                                                                                                    
%let obs_class=&sysnobs;                                                                                                                
                                                                                                                                        
data school;                                                                                                                            
input stud_id;                                                                                                                          
cards;                                                                                                                                  
;                                                                                                                                       
run;                                                                                                                                    
%let obs_school=&sysnobs;                                                                                                               
                                                                                                                                        
FILENAME myemail2 EMAIL  to=("abc@yahoo.com" )                                                                                          
 Subject = "count check: class=&obs_class school=&obs_school";                                                                          
                                                                                                                                        
data _null_;                                                                                                                            
file myemail2;                                                                                                                          
Put "Total number of records in dataset class=&obs_class";                                                                              
Put "Total number of records in dataset school=&obs_school";                                                                            
run;

View solution in original post


All Replies
PROC Star
Posts: 1,322

Re: How to display dataset record in email template

Can you show how yout tried, with macros? 

 

I would expect the sample code you posted to throw an error, caused by the & outside of the quoted text.  But if you put the counts inside the quotes it should work fine, e.g.:

 

9    data _null_;
10   Put "Total number of records in dataset class=19";
11   Put "Total number of records in dataset school=0";
12   run;

Total number of records in dataset class=19
Total number of records in dataset school=0
Contributor
Posts: 63

Re: How to display dataset record in email template

This is just an example I have given. I don't want to write manually. How to write the code such that it will display the exact line in email template.
PROC Star
Posts: 1,322

Re: How to display dataset record in email template

So you have the emailing part working like you want?

 

If so, then I would probably use a function-style macro that returns a count of the number of records in a dataset, see e.g. http://www2.sas.com/proceedings/sugi26/p095-26.pdf .

 

With that, you should be able to get what you want with something like:

 

data _null_ ;
  file myemail2 ;
  put "Total number of records in dataset class= %countobs(class)" ;
  put "Total number of records in dataset school=%countobs(school)" ;
run ;
Contributor
Posts: 63

Re: How to display dataset record in email template

Thanks for the information.

 

I use the suggested code but the email body do not resolve the code. It displays like below

 

Total number of records in dataset class= %countobs(class) Total number of records in dataset school=%countobs(school)

 

PROC Star
Posts: 1,322

Re: How to display dataset record in email template

Did you define a macro named %countobs() before you ran that code?  That is the first step.  The paper I linked to has an example.  You need to compile the macro first.

Solution
‎08-11-2017 02:59 AM
SAS Employee
Posts: 9

Re: How to display dataset record in email template

Using the original code, add a check for the automatic variable &sysnobs (which captures the number of obs processed in that data step).  I tweaked the subject so you can also see the counts in the subject without opening the message:

 

data class;                                                                                                                             
set sashelp.class;                                                                                                                      
run;                                                                                                                                    
%let obs_class=&sysnobs;                                                                                                                
                                                                                                                                        
data school;                                                                                                                            
input stud_id;                                                                                                                          
cards;                                                                                                                                  
;                                                                                                                                       
run;                                                                                                                                    
%let obs_school=&sysnobs;                                                                                                               
                                                                                                                                        
FILENAME myemail2 EMAIL  to=("abc@yahoo.com" )                                                                                          
 Subject = "count check: class=&obs_class school=&obs_school";                                                                          
                                                                                                                                        
data _null_;                                                                                                                            
file myemail2;                                                                                                                          
Put "Total number of records in dataset class=&obs_class";                                                                              
Put "Total number of records in dataset school=&obs_school";                                                                            
run;
Contributor
Posts: 63

Re: How to display dataset record in email template

Posted in reply to DaveHorne
Thanks for the solution
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 158 views
  • 2 likes
  • 3 in conversation