FORMAT Procedure

Using a Function to Format Values

SAS provides a way to format a value by first performing a function on a value. By using a function to format values, you can create customized formats. For example, SAS provides four formats to format dates by quarters, YYQw., YYQxw., YYQRw., and YYQRxw. None of these formats satisfy your requirement to use Q1, Q2, Q3, or Q4. You can create a function using the FCMP procedure that creates the Q1, Q2, Q3, and Q4 values based on a date and a SAS format, and then use that function in the FORMAT procedure to create a format.
Here are the steps to create and use a function to format values:
  1. Use the FCMP procedure to create the function.
  2. Use the OPTIONS statement to make the function available to SAS by specifying the location of the function in the CMPLIB= system option.
  3. Use the FORMAT procedure to create a new format.
  4. Use the new format in your SAS program.
Here is an example:
/* Create a function that creates the value Qx from a formatted value. */

proc fcmp outlib=work.functions.smd;

   function qfmt(date) $;
      length qnum $4;
      qnum=put(date,yyq4.);
      if substr(qnum,3,1)='Q'
         then return(substr(qnum,3,2));
      else return(qnum);
   endsub;
run;

/* Make the function available to SAS. */

options cmplib=(work.functions);

/* Create a format using the function created by the FCMP procedure. */

proc format; 
   value qfmt 
         other=[qfmt()]; run;

/* Use the format in a SAS program.  */

data djia2009;
      input closeDate date7. close;
      datalines;
01jan09 800.86
02feb09 7062.93
02mar09 7608.92
01apr09 8168.12
01may09 8500.33
01jun09 8447.00
01jul09 9171.61
03aug09 9496.28
01sep09 9712.28
01oct09 9712.73
02nov09 10344.84
02dec09 10428.05
run;

proc print data=djia2009;
format closedate qfmt. close dollar9.;
run;
Here is the output:
Dow Jones Closings by Quarter
Dow Jones Closings by Quarter
You can use the function format as the value to OTHER=.