SAS Institute. The Power to Know

SAS/GRAPH(R) 9.2: Graph Template Language Reference

Previous | Next
Overview

Conditional Logic

Using conditional logic, you can create templates that have multiple code paths (can execute alternate statements, depending on existing conditions). The evaluation of a logical expression must generate one or more complete statements (not portions of statements). All conditional logic uses one of the followinge constructs:

if ( condition ) if ( condition ) if ( condition1 )
   statement(s);    statement(s);    statement(s);
endif; else else if (condition2)
     statement(s);    statement(s);
  endif; else
       statement(s);
    endif;

In all cases, condition must be enclosed in parentheses and may be any standard SAS expression involving arithmetic, logical operators, comparison operators, Boolean operators, or concatenation operators; the expression can also use SAS DATA step functions. The expression resolves to a single scalar value, which is true or false .

In the following example, a histogram is conditionally overlaid with a normal distribution curve, a Kernel Density Estimate distribution curve, both, or neither:

  
 proc template; 
   define statgraph conditional; 
    dynamic VAR VARLABEL BINS CURVE; 
    begingraph; 
     entrytitle "Histogram of " VAR; 
     layout overlay / xaxisopts=(label=VARLABEL); 
      histogram VAR /  nbins=BINS; 
  
      if (upcase(CURVE) in ("ALL"  "KERNEL")) 
         densityplot VAR / kernel() name="k" 
                           legendlabel="Kernel" 
                           lineattrs=(pattern=dash); 
      endif; 
  
      if (upcase(CURVE) in ("ALL" "NORMAL")) 
         densityplot VAR / normal() name="n" 
                           legendlabel="Normal"; 
      endif; 
  
      discretelegend "n" "k"; 
    endlayout; 
   endgraph; 
  end; 
 run; 
  
 proc sgrender data=sashelp.class 
               template=conditional; 
   dynamic var="Height" varlabel="Height in Inches" 
           curve="all"; 
 run; 
  
 proc sgrender data=sashelp.class 
               template=conditional; 
   dynamic var="Weight" varlabel="Weight in Pounds"; 
 run;
 


ovcurveall.gif (37543 bytes)

Figure 1.4: Result of setting CURVE="all"


ovnocurve.gif (29149 bytes)

Figure 1.5: Result of not setting CURVE


Note that the legend syntax does not have to be made conditional. At runtime each plot name in the legend is checked. If the plot does not exist, its name is removed from the legend name list. If no names appear on the DISCRETELEGEND statement, the legend "drops out" and the histogram is resized to fill the remaining space.

Most of the supplied templates for statistical procedures use conditional logic and dynamics to enable a single template to produce many variations of a standard statistical graph.

Previous | Next | Top of Page