When you want to create a SAS date solely from a year value, you can use the MDY function to assign the desired month and day values that are required to construct a SAS date. The FCMP procedure enables you to create custom functions to address a variety of uses and purposes. If this function could also serve as an informat, the newly created function can be called as a label within the FORMAT procedure in the INVALUE statement. The function now acts as an informat to read data values.
The example code on the Full Code tab illustrates how to use both techniques so that a year value is made into a SAS date with month and day values of 1. You can adapt this code in many ways to fit your needs.
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.
/* Create a function called YTOD that uses MDY function to read */
/* a year value and create a SAS date where month=1 and day=1. */
proc fcmp outlib=work.functions.smd;
function ytod(c);
newdate=mdy(1,1,c);
return (newdate);
endsub;
run;
options cmplib=(work.functions);
/* Call the function and use the year value of 2013. */
/* Write the value of the F variable to the SAS log with a date format. */
data _null_;
f=ytod(2014);
put f=mmddyy10.;
run;
/*------------------------------------------------------------*/
/* Create informat YRTODT that uses the instructions in the YTOD function */
proc format;
invalue yrtodt (default=10) other=[ytod()];
run;
/* Read the year values with the YRTODT informat */
data a;
input sasdt yrtodt.;
datalines;
2013
2014
;
run;
proc print;
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.
/* The first DATA _NULL_ step produces this SAS date in the SAS log */ f=01/01/2014 /* The second DATA step produces this PROC PRINT output */ Obs sasdt 1 19359 2 19724
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> FORMAT SAS Reference ==> Procedures ==> FCMP SAS Reference ==> Informats ==> Date and Time Common Programming Tasks ==> Reading and Writing External Data ==> Multiple Files Common Programming Tasks ==> Reading and Writing External Data |
Date Modified: | 2014-01-20 10:43:58 |
Date Created: | 2014-01-16 11:27:23 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | 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 | |||
Solaris 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 | |||
Microsoft Windows Server 2008 for x64 | 9.3 TS1M0 | |||
Microsoft Windows Server 2008 R2 | 9.3 TS1M0 | |||
Microsoft Windows Server 2008 | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 for x64 | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.3 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.3 TS1M0 | |||
Microsoft® Windows® for x64 | 9.3 TS1M0 | |||
Z64 | 9.3 TS1M0 | |||
z/OS | 9.3 TS1M0 |