Language Reference

TABLEADDVAR Call

CALL TABLEADDVAR (table, colnames, matrix) ;

CALL TABLEADDVAR (table1, table2) ;

This subroutine is supported by the IML procedure and the iml action.

The TableAddVar subroutine adds new columns to an existing table. The source of the columns can be a numeric or character matrix. The new columns are appended after the existing columns.

You can use the TableAddVar subroutine to horizontally concatenate two tables. The columns of the second table are appended to the first table. The second table is unchanged.

The subroutine takes the following input arguments:

table

specifies an existing table.

colnames

specifies a character vector that contains p strings. The strings specify the names for the new columns in the table. It is an error to specify a column name that already exists in the table.

matrix

specifies a numeric or character matrix that has p columns.

The following example calls the TableCreate function to create an empty table and then calls the TableAddVar subroutine twice: first to add character columns, and then to add numeric columns. Each row in the table contains data about historic hurricanes that struck the United States. Figure 454 shows the names and types of the columns of the table.

tbl = TableCreate();   /* create empty table */

Hurr  = {"Katrina", "Ike",       "Andrew", "Wilma"};
Month = {"August",  "September", "August", "October"};
call TableAddVar(tbl, {"Name" "Month"}, Hurr||Month); /* add char cols */

Yr      = {2005, 2008, 1992, 2005};
Wind = { 175,  145,  175,  185};   /* mph */
call TableAddVar(tbl, {"Year" "MaxWind"}, Yr||Wind);  /* add numeric cols */

colNames = TableGetVarName(tbl);
colTypes = TableGetVarType(tbl);
print colTypes[c=colNames L="Table Names and Types"];

Figure 454: Names and Types of Table Columns

Table Names
and Types
NameMonthYearMaxWind
CCNN


You can use the TableAddVar subroutine to horizontally concatenate two tables. You can also use the horizontal concatenation operator (||) to horizontally concatenate two or more tables. The following statements show two ways to concatenate tables. The number of rows in the tables do not have to be equal, although usually they will be.

/* Method 1: Concatenation operator */
tbl1 = TableCreate("Letters", {"A", "B", "C"});   /* create a character table */
tbl2 = TableCreate("x1":"x2", {1 2, 3 4, 5 6} );  /* create a numeric table */
concatTbl = tbl1 || tbl2;

/* Method 2: Copy the first table; append columns of the second table */
concatTbl = tbl1;
call TableAddVar(concatTbl, tbl2);
Last updated: May 07, 2026