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
This method 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. */

getLibrary Method

Short Description
Returns the name of the library for the data source.
Return Value
This method 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
If the data source is available, a value of 0 or greater is returned. 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, and 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
This method 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. */