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

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 ? 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

proc fcmp allows you to create custom functions in SAS.

See if it fulfills your needs.

 

 

ChrisNZ
Tourmaline | Level 20

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

 

UdayGuntupalli
Quartz | Level 8

@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 ? 

ChrisNZ
Tourmaline | Level 20

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;

.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 791 views
  • 1 like
  • 2 in conversation