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

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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