BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Abraham
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
DaveHorne
SAS Employee

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

7 REPLIES 7
Quentin
Super User

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
The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
Abraham
Obsidian | Level 7
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.
Quentin
Super User

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 ;
The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
Abraham
Obsidian | Level 7

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)

 

Quentin
Super User

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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
DaveHorne
SAS Employee

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;
Abraham
Obsidian | Level 7
Thanks for the solution

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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