APPEND Method
Appends the specified source data grid to the specified target data grid.
| Applies to: | Datagrid Package |
|---|---|
| Returned data type: | INTEGER |
| Notes: | The columns in the two data grids do not need to match. This method returns the number of rows in the resulting target data grid. If an error occurs, this method returns -1. |
| When you use the append method with a headless data grid, it is important to consider the column data types. If the column data types do not match between the two data grids, the source column type is inferred by the type of the corresponding column in the target data grid. See Example 3 for more information. |
Table of Contents
Syntax
Package Variable
package
specifies a name that references the datagrid package instance.
Required Arguments
dgSource
specifies the variable name of the source data grid.
JSON_string
specifies a JSON character string that contains the data for the data grid. You can specify a literal value in single quotation marks, or you can specify a variable that evaluates to a JSON character string. For more information about the data grid JSON format, see About Data Grid JSON Representation in SAS DS2 Programmer’s Guide for SAS Viya Workbench.
Examples
Example 1: Append
In this example, dataGrid_A is the target and dataGrid_B is the source. Here are the contents of each data grid:
|
a |
b |
c |
|---|---|---|
|
Aa1 |
Ab1 |
Ac1 |
|
Aa2 |
Ab2 |
Ac2 |
|
e |
d |
c |
a |
|---|---|---|---|
|
Be1 |
Bd1 |
Bc1 |
Ba1 |
|
Be2 |
Bd2 |
Bc2 |
Ba2 |
|
Be3 |
Bd3 |
Bc3 |
Ba3 |
The following operation is performed:
dataGrid_A.append(dataGrid_B)
Here is the resulting data grid:
|
a |
b |
c |
e |
d |
|---|---|---|---|---|
|
Aa1 |
Ab1 |
Ac1 |
null |
null |
|
Aa2 |
Ab2 |
Ac2 |
null |
null |
|
Ba1 |
null |
Bc1 |
Be1 |
Bd1 |
|
Ba2 |
null |
Bc2 |
Be2 |
Bd2 |
|
Ba3 |
null |
Bc3 |
Be3 |
Bd3 |
Example 2: Headless Data Grid
In this example, the data grid that is shown in dataGrid_B includes only row data. The column header metadata is not included. This is known as a headless data grid.
Here is dataGrid_B presented as a headless data grid in JSON format:
[{"data":[ ["Be1","Bd1","Bc1","Ba1"], ["Be2","Bd2","Bc2","Ba2"], ["Be3","Bd3","Bc3","Ba3"] ]}]
Here is dataGrid_B presented as a headless data grid in a table format:
|
COL_1 |
COL_2 |
COL_3 |
COL_4 |
|---|---|---|---|
|
Be1 |
Bd1 |
Bc1 |
Ba1 |
|
Be2 |
Bd2 |
Bc2 |
Ba2 |
|
Be3 |
Bd3 |
Bc3 |
Ba3 |
When you append a headless data grid, the append method maps the destination columns to the source columns based on position. Therefore, when headless dataGrid_B is appended to dataGrid_A, here is the mapping that occurs:
dataGrid_A.a <-- dataGrid_B.COL_1
dataGrid_A.b <-- dataGrid_B.COL_2
dataGrid_A.c <-- dataGrid_B.COL_3
dataGrid_A.COL_4 <-- dataGrid_B.COL_4
This is the resulting dataGrid_A JSON string:
[
{"metadata":[{"A":"string"},{"B":"string"},{"C":"string"},{"COL_4":"string"}]},
{"data":[["Aa1","Ab1","Ac1",null],["Aa2","Ab2","Ac2",null],
["Be1","Bd1","Bc1","Ba1"],["Be2","Bd2","Bc2","Ba2"],["Be3","Bd3","Bc3","Ba3"]]}
]
This is the resulting dataGrid_A table:
|
A |
B |
C |
COL_4 |
|---|---|---|---|
|
Aa1 |
Ab1 |
Ac1 |
null |
|
Aa2 |
Ab2 |
Ac2 |
null |
|
Be1 |
Bd1 |
Bc1 |
Ba1 |
|
Be2 |
Bd2 |
Bc2 |
Ba2 |
|
Be3 |
Bd3 |
Bc3 |
Ba3 |
Example 3: Source Column Type Inferred
This example shows that when you apply the APPEND method to a headless data grid, the source column type is inferred by the type of the corresponding column in the target data grid. Here are the data grid JSON strings that are used in this example:
dataGrid_A_json:
[
{"metadata":[{"A":"string"},{"B":"double"}]},
{"data":[["A1",1.11],["A2",2.22],["A3",3.33]]}
]
dataGrid_B_json:
[{"data":[["B1",10],["B2",20],["B3",30]}]
Suppose you apply the deserialization method to dataGrid_B_json:
dgB.deserialize(dataGrid_B_json)
It produces this data grid (dgB). Note that COL_2 is a long type.
|
COL_1 (string) |
COL_2 (long) |
|---|---|
|
B1 |
10 |
|
B2 |
20 |
|
B3 |
30 |
Assume that dataGrid_A_json is deserialized to a data grid called dgA. Now suppose you use the append method to append dataGrid_B_json to dgA:
dgA.append(dataGrid_B_json)
It produces this data grid. Note that the long values in dataGrid_B_json have been converted to double values.
|
A (string) |
B (double) |
|---|---|
|
A1 |
1.11 |
|
A2 |
2.22 |
|
A3 |
3.33 |
|
B1 |
10.00 |
|
B2 |
20.00 |
|
B3 |
30.00 |