As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Dynamic formats in PROC REPORT

From sasCommunity
Jump to: navigation, search

This code shows how to apply a dynamically defined format in PROC REPORT. In this simplified example, the format will be 10.0 if the variable DECIMALS has a value of 0, 10.1 if DECIMALS has a value of 1, and so forth.

data test (keep=decimals value displayed);
   do i = 1 to 5;
      do decimals = 0 to 3;
         value = ranuni(95605) * 10;
         displayed = value;
         output;
      end;
   end;
   stop;
run;
 
proc report data=test nowindows missing nocenter;
   columns value decimals displayed;
   define _all_ / display;
   compute displayed;
      call define(_col_, 'format', '10.' || put(decimals, z1.0));
   endcomp;
run;

prints:

      value   decimals  displayed
  3.4648784          0          3
  3.3165982          1        3.3
  2.0278367          2       2.03
  8.0749122          3      8.075
  8.1445687          0          8
  7.6102304          1        7.6
  5.2384895          2       5.24
  3.8716015          3      3.872
  5.5787562          0          6
   0.951658          1        1.0
  9.5194012          2       9.52
  6.4120504          3      6.412
  7.6308912          0          8
  1.8146458          1        1.8
  2.6590652          2       2.66
   2.289426          3      2.289
  0.5009372          0          1
  7.2236493          1        7.2
  6.0467117          2       6.05
  2.5131059          3      2.513