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 ?
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;
.
proc fcmp allows you to create custom functions in SAS.
See if it fulfills your needs.
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.)>.);
@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 ?
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;
.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.