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
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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 ;
BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 2038 views
  • 2 likes
  • 3 in conversation