DATA Step, Macro, Functions and more

Is there an equivalent of JMP Namespaces in SAS ?

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Is there an equivalent of JMP Namespaces in SAS ?

Hello all, 
          I am a JMP user trying to learn SAS. One of the cool ways to manage user developed functions in JMP is namespaces.

          Below is a sample namespace that I created in JMP. Now, in any JMP script, I can include this namespace and leverage the IsLeapYear function. 

ExcelFunctions = New Namespace("Excel"); 
							 
// IsLeapYear
ExcelFunctions:IsLeapYear = function({Input_Year},{Default Local},
										// Inputs : 1. Input_Year - Integer that represents the year to test if leap or not 
										// Output : Returns 0 or 1 - 0 indicates not a leap year and 1 indicates that it is a leap year 
										If(Is String(Input_Year),
											Input_Year = Num(Input_Year);
										  );
										If( (Mod(Input_Year,400) == 0 | (Mod(Input_Year,4) == 0  & Mod(Input_Year,100)!= 0)),
											return(1);
											,
											return(0);
										  );
								   );// end of IsLeapYear Function 
					 

Is there an equivalent of this in SAS ? Can anybody kindly illustrate with an example ? 


Accepted Solutions
Solution
‎04-13-2018 01:02 PM
Super User
Posts: 2,512

Re: Is there an equivalent of JMP Namespaces in SAS ?

Posted in reply to UdayGuntupalli

You can have as many functions as you want and store them where you want


libname TEST 'c:\temp';

proc fcmp outlib = TEST.FUNC.NUM;
 function IsLeapYear(InputYear);
   return ( Modz(InputYear,400)=0 | (Modz(InputYear,4)=0 & Modz(InputYear,100)~=0) ) ;
 endsub;
 function IsEven(Integer);
   return ( Modz(Integer,2) = 0 ) ;
 endsub;
run;

options cmplib = TEST.FUNC;

data _null_;
   Res = IsLeapYear(2000);   put Res=;
   Res = IsLeapYear(2016);   put Res=;
   Res = IsLeapYear(2015);   put Res=;
   Res = IsEven(2016);       put Res=;
   Res = IsEven(2015);       put Res=;
run;

.

 

 

View solution in original post


All Replies
Super User
Posts: 2,512

Re: Is there an equivalent of JMP Namespaces in SAS ?

Posted in reply to UdayGuntupalli

proc fcmp allows you to create custom functions in SAS.

See if it fulfills your needs.

 

 

Super User
Posts: 2,512

Re: Is there an equivalent of JMP Namespaces in SAS ?

[ Edited ]
Posted in reply to UdayGuntupalli

Note that to test for a leap year, I would simply test if Feb 29th exists. Just a detail though.

IS_LEAP_YEAR=(input(catt('29feb',YEAR),?? date9.)>.);

 

Contributor
Posts: 43

Re: Is there an equivalent of JMP Namespaces in SAS ?

[ Edited ]

@ChrisNZ
            Thank you for offering your guidance. If I understand your suggestion correctly, you are advising that I do something like this ?  

 

proc fcmp outlib = work.funcs.Test;
  function IsLeapYear(InputYear);
    If (Modz(Input_Year,400) = 0 | (Modz(Input_Year,4) = 0 & 
      Modz(Input_Year,100)^= 0)) Then
      Res = 1; 
    Else
      Res = 0; 
    Return(Res);
  endsub;
options cmplib = work.funcs.Test;

data _null_;
 TestYear = 2016; 
 Test = IsLeapYear(TestYear); 
  put Test = ;
run;

However, if I had 10 such user defined functions - by creating a namespace and "including" that namespace in my project, I get to use all of them while keeping my project clean. I am wondering if there is an equivalent for managing multiple functions like that in SAS ? 

 

Also, a question that has come up as I try to write the above is - work.funcs.trial is an example I used from the documentation, I wonder if I can rename that to a folder location  like say "C:\Users\Test" or something ? 

Solution
‎04-13-2018 01:02 PM
Super User
Posts: 2,512

Re: Is there an equivalent of JMP Namespaces in SAS ?

Posted in reply to UdayGuntupalli

You can have as many functions as you want and store them where you want


libname TEST 'c:\temp';

proc fcmp outlib = TEST.FUNC.NUM;
 function IsLeapYear(InputYear);
   return ( Modz(InputYear,400)=0 | (Modz(InputYear,4)=0 & Modz(InputYear,100)~=0) ) ;
 endsub;
 function IsEven(Integer);
   return ( Modz(Integer,2) = 0 ) ;
 endsub;
run;

options cmplib = TEST.FUNC;

data _null_;
   Res = IsLeapYear(2000);   put Res=;
   Res = IsLeapYear(2016);   put Res=;
   Res = IsLeapYear(2015);   put Res=;
   Res = IsEven(2016);       put Res=;
   Res = IsEven(2015);       put Res=;
run;

.

 

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 101 views
  • 1 like
  • 2 in conversation