Working with the DataSource Element in Velocity

About the DataSource Element

You can specify only one DataSource element in the common task model. (You can also have a task with no DataSource element.) If you define the DataSource element, a Velocity variable is created to access the name of the specified data source. The value of the variable is the same as the value of the name attribute for the DataSource element.
If you reference the name of the data source in Velocity (for example, $datasource), you see the value of the active Library.Table. You can use the columnExists, getLibrary, getRowsCount, and getTable methods to get more information about the data source. For more information, see Common Utilities for CTM Writers.

columnExists Method

Short Description
determines whether the specified value already exists as the name of a column in the data source.
Parameter
input
the input string that you want to check to see whether it exists.
Return Value
returns a Boolean value that specifies whether the column already exists.
Example
<DataSource name="DATASOURCE">
   <Roles>
      <Role name="analysisVariables" type="A" maxVars="0" minVars="0">
         Analysis variables:</Role>
   <Roles>
</DataSource>

#if ($DATASOURCE.columnExists("MAKE")) ... #end /* If data set is
   Sashelp.Cars, the return value is true. */

getDistinctCount Method

To use this method, specify fetchDistinct = “true” in the Role element.
Short Description
returns the count of distinct values for a given column name for the current data source.
Note: For optimal performance, the maximum number of distinct values is 100.
Return Value
returns –1 if the number of distinct values for a column is not available.
Example
<DataSource name="DATASOURCE">
   <Roles>
      <Role name="VAR" fetchDistinct="true" type="A" maxVars="0" minVars="0">
         Analysis variables:</Role>
   <Roles>
</DataSource>

<Dependencies>
   <Dependency condition="$VAR.size() &gt; 0 &amp;&amp; 
      $DATASOURCE.getDistinctCount($VAR[0]) &gt; 0">
      <Target action="show" conditionResult="true" option="targetComboBox"/>
      <Target action="hide" conditionResult"false" option="targetComboBox"/>
   </Dependency>
</Dependencies>

#if ($VAR.size() > 0 && $DATASOURCE.getDistinctCount($VAR[0]) > 0  ... #end 

getLibrary Method

Short Description
returns the name of the library for the data source.
Return Value
returns a string that contains the name of the library for the data source.
Example
<DataSource name="DATASOURCE">
   <Roles>
      <Role name="analysisVariables" type="A" maxVars="0" minVars="0">
         Analysis variables:</Role>
   <Roles>
</DataSource>

$DATASOURCE.getLibrary() /* If data set is Sashelp.Cars,
   the return value is Sashelp. */

getRowsCount Method

Short Description
returns the number of rows in the data source.
Return Value
returns a value of 0 or greater if the data source is available. If this information is not available, –1 is the return value. For example, in SAS Studio when the selected data source is a data view, the row count is not available, so the return code for this function is –1.
Example
<DataSource name="DATASOURCE">
   <Roles>
      <Role name="analysisVariables" type="A" maxVars="0" minVars="0">
   Analysis variables:</Role>
   <Roles>
</DataSource>

#if ($DATASOURCE.getRowsCount() > 0) ... #end /* If data set
   is Sashelp.Cars, the return value is 19. */

getTable Method

Short Description
returns the table name for the data source.
Return Value
returns a string that contains the table name for the data source.
Example
<DataSource name="DATASOURCE">
   <Roles>
      <Role name="analysisVariables" type="A" maxVars="0" minVars="0">
   Analysis variables:</Role>
   <Roles>
</DataSource>

$DATASOURCE.getTable() /* If data set is 
   Sashelp.Cars, the return value is Cars. */

getWhereClause Method

To use this method, you must specify where = “true” in the DataSource element.
Short Description
returns the filter of the currently assigned data source.
Return Value
returns a string that contains the filter of the currently assigned data source.
Example
<DataSource name="DATASOURCE" where="true">
   <Roles>
      <Role name="analysisVariables" type="A" maxVars="0" minVars="0">
   Analysis variables:</Role>
   <Roles>
</DataSource>

$DATASOURCE.getWhereClause()/* If data set
   is Sashelp.Cars, the return value is the filter value
that the user specifies. */

getDataType Method

Short Description
returns the type of data set. This value corresponds to the ‘typemem’ value in Sashelp.Vtable.
Return Value
is the type of data set. This method defaults to null if the value is not available.
Example
<DataSource name="DATASOURCE" where="true">
   </DataSource>

$DATASOURCE.getDataType()