Example: Modify the UserAnalysis Module

You can modify the UserAnalysis module to call your own analyses from the Analysis User Analysis menu item.

To create your own UserAnalysis module:

  1. Copy the UserAnalysis.sxs file distributed with SAS/IML Studio to your personal modules directory. The UserAnalysis.sxs file is distributed in the Modules\System subdirectory of the SAS/IML Studio installation directory. Your personal modules directory is usually the Modules subdirectory of your personal files directory. (See the The Personal Files Directory section in Chapter 34 for more information about the personal files directory.)

  2. Edit your personal copy of the UserAnalysis.sxs file. Modify the body of the UserAnalysis module so that it performs an analysis of your choosing.

  3. Save the file.

  4. Select Program Run to store the module.

  5. Open any data set, and select Analysis User Analysis to run the module.

The UserAnalysis module must take a DataObject variable as its single argument. When you select Analysis User Analysis, the module is called. The currently active DataObject is used as the argument to the module.

Table 33.1 lists a few of the methods in the DataObject class. You might find these methods useful in writing your analyses. These and other IMLPlus class methods are documented in the SAS/IML Studio online Help, in the "DataObject" section of the "IMLPlus Class Reference" chapter.

Table 33.1 Frequently Used DataObject Methods

Method

Description

AddAnalysisVar

Adds a new variable to the DataObject.

GetNumObs

Returns the number of observations in the DataObject.

GetSelectedObsNumbers

Gets the index of observations selected in the DataObject.

GetSelectedVarNames

Gets the names of variables selected in the DataObject.

GetVarData

Gets the data for a variable in the DataObject.

IsNominal

Returns true if the named variable is nominal.

IsNumeric

Returns true if the named variable is numeric.

SelectObs

Selects observations in the DataObject.

SetMarkerColor

Sets the color of observation markers.

SetMarkerShape

Sets the shape of observation markers.

For example, you could modify the body of the UserAnalysis module to include the following statements. If you select a nominal variable from a data table and then select Analysis User Analysis, these statements assign a distinct marker shape to each unique value of the nominal variable. (If there are more unique values than marker shapes, the shapes are reused.) The NCOL, UNIQUE, LOC, and MOD functions are all part of the SAS/IML language, as are the IF and DO statements.

start UserAnalysis(DataObject dobj);

dobj.GetSelectedVarNames(VarName); /* get selected var name */
if ncol(VarName) = 0 then return;  /* return if no selected variable */
if dobj.IsNominal(VarName) then do;/* if it is nominal... */
   shapes = MARKER_SQUARE || MARKER_PLUS || MARKER_CIRCLE ||
            MARKER_DIAMOND ||MARKER_X || MARKER_TRIANGLE ||
            MARKER_INVTRIANGLE || MARKER_STAR;
   dobj.GetVarData(VarName, x);    /* get the data */
   ux = unique(x);                 /* find the unique values */
   do i = 1 to ncol(ux);           /* for each unique value... */
      idx = loc(x = ux[<i]);        /* find obs with that value */
      iShape = 1 + mod(i-1, 8);    /* choose next shape (mod 8) */
      /* set the shape of the relevant observations */
      dobj.SetMarkerShape(idx, shapes[iShape]);
   end;
end;

finish;
store module=UserAnalysis;