SAS/Macro Modulo Function

From sasCommunity
< SAS
Jump to: navigation, search

Description

I could not find the famous modulo operator in SAS/Macro system. So I wrote a quick one. it seems to work on most cases. Result has the same sign as the Dividend (a).

Code

%macro modulo_debug(a, b);                           
%let divi=%eval(&a/&b);                        
%let tmp=%eval(&divi * &b);                    
%let final=%eval(&a - &tmp);                   
%put divi = &divi : tmp = &tmp : final : &final;
&final    
%mend;                                         
       
%macro modulo(a, b);                  
%eval(&a - %eval(%eval(&a/&b) * &b)); 
%mend;                                

As an alternative one could also make use of %SYSFUNC to call DATA Step function MOD().

%sysfunc( mod(-677, -100) )


Usage

%put %modulo(3,1);   
0                               
%put %modulo(33,2);  
1                               
%put %modulo(33,33); 
0                               
%put %modulo(33,3);  
0                               
%put %modulo(55,10); 
5                               
%put %modulo(173,3); 
2                               
%put %modulo(233,5); 
3                               
%put %modulo(233,30);
23                              
%put %modulo(233,2); 
1                                       
%put %modulo(233,50);        
33                                      
%put %modulo(677,100);       
77                                      
%put %modulo(-677,100);      
-77                                     
%put %modulo(677,-100);      
77                                      
%put %modulo(-677,-100);     
-77                                     
%put %modulo(677,100);       
77                                      

By

User:Mathieu