This sample demonstrates logic to determine whether a specified year is a leap year or not.
PROC FCMP is used to create a user-defined function using the logic from the DATA step code.
Usage Note 41809: Using user-defined functions in the DATA Step created with PROC FCMP
SAS 9.3 PROC FCMP Documentation
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* show DATA step code for a few years */
Data _NULL_ ;
do x = 1900 to 1920;
length leap $3;
leap='no';
If mod(x,4)=0 then leap='YES'; /* divisible by 4 */
If mod(x,100)=0 and mod(x,400) ne 0 then leap='NO';
put _all_;
end;
Run;
/* define LEAPYR function */
proc fcmp outlib=work.fcmpfuncs.func print;
function sas_leapyr(x) $3;
length leap $3;
leap = 'NO';
If mod(x,4)=0 then leap='YES'; /* divisible by 4 */
If mod(x,100)=0 and mod(x,400) ne 0 then leap='NO';
return(leap);
endsub;
run;
options cmplib=work.fcmpfuncs; /* specify where function is stored */
/* demonstrate use of function */
data two;
do i = 1900 to 2015;
leap = sas_leapyr(i);
output;
end;
run;
proc print data=two; run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Log output: 2 3 /* show DATA step code for a few years */ 4 Data _NULL_ ; 5 do x = 1900 to 1920; 6 length leap $3; 7 leap='no'; 8 If mod(x,4)=0 then leap='YES'; /* divisible by 4 */ 9 If mod(x,100)=0 and mod(x,400) ne 0 then leap='NO'; 10 put _all_; 11 end; 12 Run; x=1900 leap=NO _ERROR_=0 _N_=1 x=1901 leap=no _ERROR_=0 _N_=1 x=1902 leap=no _ERROR_=0 _N_=1 x=1903 leap=no _ERROR_=0 _N_=1 x=1904 leap=YES _ERROR_=0 _N_=1 x=1905 leap=no _ERROR_=0 _N_=1 x=1906 leap=no _ERROR_=0 _N_=1 x=1907 leap=no _ERROR_=0 _N_=1 x=1908 leap=YES _ERROR_=0 _N_=1 x=1909 leap=no _ERROR_=0 _N_=1 x=1910 leap=no _ERROR_=0 _N_=1 x=1911 leap=no _ERROR_=0 _N_=1 x=1912 leap=YES _ERROR_=0 _N_=1 x=1913 leap=no _ERROR_=0 _N_=1 x=1914 leap=no _ERROR_=0 _N_=1 x=1915 leap=no _ERROR_=0 _N_=1 x=1916 leap=YES _ERROR_=0 _N_=1 x=1917 leap=no _ERROR_=0 _N_=1 x=1918 leap=no _ERROR_=0 _N_=1 x=1919 leap=no _ERROR_=0 _N_=1 x=1920 leap=YES _ERROR_=0 _N_=1 NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 13 14 /* define LEAPYR function */ 15 proc fcmp outlib=work.fcmpfuncs.func print; 16 function sas_leapyr(x) $3; 17 length leap $3; 18 leap = 'NO'; 19 If mod(x,4)=0 then leap='YES'; /* divisible by 4 */ 20 If mod(x,100)=0 and mod(x,400) ne 0 then leap='NO'; 21 return(leap); 22 endsub; 23 run; NOTE: Function sas_leapyr saved to work.fcmpfuncs.func. NOTE: PROCEDURE FCMP used (Total process time): real time 0.07 seconds cpu time 0.04 seconds 24 25 options cmplib=work.fcmpfuncs; /* specify where function is stored */ 26 /* demonstrate use of function */ 27 data two; 28 do i = 1900 to 2015; 29 leap = sas_leapyr(i); 30 output; 31 end; 32 run; NOTE: The data set WORK.TWO has 116 observations and 2 variables. NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.03 seconds 33 proc print data=two; run; NOTE: There were 116 observations read from the data set WORK.TWO. NOTE: PROCEDURE PRINT used (Total process time): real time 0.00 seconds cpu time 0.00 seconds NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414 NOTE: The SAS System used: real time 1:06.81 cpu time 1.57 seconds Proc PRINT output; Obs i leap 1 1900 NO 2 1901 NO 3 1902 NO 4 1903 NO 5 1904 YES 6 1905 NO 7 1906 NO 8 1907 NO 9 1908 YES 10 1909 NO 11 1910 NO 12 1911 NO 13 1912 YES 14 1913 NO 15 1914 NO 16 1915 NO 17 1916 YES 18 1917 NO 19 1918 NO 20 1919 NO 21 1920 YES 22 1921 NO 23 1922 NO 24 1923 NO 25 1924 YES 26 1925 NO 27 1926 NO 28 1927 NO 29 1928 YES 30 1929 NO 31 1930 NO 32 1931 NO 33 1932 YES 34 1933 NO 35 1934 NO 36 1935 NO 37 1936 YES 38 1937 NO 39 1938 NO 40 1939 NO 41 1940 YES 42 1941 NO 43 1942 NO 44 1943 NO 45 1944 YES 46 1945 NO 47 1946 NO 48 1947 NO 49 1948 YES 50 1949 NO 51 1950 NO 52 1951 NO 53 1952 YES 54 1953 NO 55 1954 NO 56 1955 NO 57 1956 YES 58 1957 NO 59 1958 NO 60 1959 NO 61 1960 YES 62 1961 NO 63 1962 NO 64 1963 NO 65 1964 YES 66 1965 NO 67 1966 NO 68 1967 NO 69 1968 YES 70 1969 NO 71 1970 NO 72 1971 NO 73 1972 YES 74 1973 NO 75 1974 NO 76 1975 NO 77 1976 YES 78 1977 NO 79 1978 NO 80 1979 NO 81 1980 YES 82 1981 NO 83 1982 NO 84 1983 NO 85 1984 YES 86 1985 NO 87 1986 NO 88 1987 NO 89 1988 YES 90 1989 NO 91 1990 NO 92 1991 NO 93 1992 YES 94 1993 NO 95 1994 NO 96 1995 NO 97 1996 YES 98 1997 NO 99 1998 NO 100 1999 NO 101 2000 YES 102 2001 NO 103 2002 NO 104 2003 NO 105 2004 YES 106 2005 NO 107 2006 NO 108 2007 NO 109 2008 YES 110 2009 NO 111 2010 NO 112 2011 NO 113 2012 YES 114 2013 NO 115 2014 NO 116 2015 NO
Type: | Sample |
Topic: | Common Programming Tasks ==> Reusing Code Common Programming Tasks SAS Reference ==> Procedures ==> FCMP |
Date Modified: | 2011-09-09 15:08:47 |
Date Created: | 2011-09-07 15:10:51 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | N/A | z/OS | 9.3 TS1M0 | |
Z64 | 9.3 TS1M0 | |||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.3 TS1M0 | |||
Microsoft Windows XP 64-bit Edition | 9.3 TS1M0 | |||
Microsoft® Windows® for x64 | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 for x64 | 9.3 TS1M0 | |||
Microsoft Windows Server 2008 | 9.3 TS1M0 | |||
Microsoft Windows Server 2008 for x64 | 9.3 TS1M0 | |||
Microsoft Windows XP Professional | 9.3 TS1M0 | |||
Windows 7 Enterprise 32 bit | 9.3 TS1M0 | |||
Windows 7 Enterprise x64 | 9.3 TS1M0 | |||
Windows 7 Home Premium 32 bit | 9.3 TS1M0 | |||
Windows 7 Home Premium x64 | 9.3 TS1M0 | |||
Windows 7 Professional 32 bit | 9.3 TS1M0 | |||
Windows 7 Professional x64 | 9.3 TS1M0 | |||
Windows 7 Ultimate 32 bit | 9.3 TS1M0 | |||
Windows 7 Ultimate x64 | 9.3 TS1M0 | |||
Windows Vista | 9.3 TS1M0 | |||
Windows Vista for x64 | 9.3 TS1M0 | |||
64-bit Enabled AIX | 9.3 TS1M0 | |||
64-bit Enabled HP-UX | 9.3 TS1M0 | |||
64-bit Enabled Solaris | 9.3 TS1M0 | |||
HP-UX IPF | 9.3 TS1M0 | |||
Linux | 9.3 TS1M0 | |||
Linux for x64 | 9.3 TS1M0 | |||
Linux on Itanium | 9.3 TS1M0 | |||
Solaris for x64 | 9.3 TS1M0 |