The examples use generated data (DATA step with direct assignment) to create temporary CAS tables.
1 Code Block
DATA STEP / PROC CAS Data
Explanation : This example shows the simplest use of `table.append`. Two tables, `t1` (source) and `t2` (target), are created with identical structures. The `table.append` action is then called to add all rows from `t1` to the end of `t2`. Finally, `proc print` is used to verify the content of table `t2` after the append operation.
Copied!
/* Préparation de l'environnement CAS */
options cashost="cloud.example.com" casport=5570;
cas casauto;
libname mycas cas caslib="CASUSER(your-username)";
/* Création de la table cible (t2) */
data mycas.t2;
length id 8 nom $10 ville $10;
id = 1;
nom = 'Alice';
ville = 'Paris';
output;
id = 2;
nom = 'Bob';
ville = 'Lyon';
output;
run;
/* Création de la table source (t1) */
data mycas.t1;
length id 8 nom $10 ville $10;
id = 3;
nom = 'Charlie';
ville = 'Marseille';
output;
run;
/* Ajout de la table t1 à la table t2 */
proc cas;
table.append /
source={caslib='CASUSER(your-username)',name='t1'}
target={caslib='CASUSER(your-username)',name='t2'};
run;
/* Affichage de la table t2 mise à jour */
proc print data=mycas.t2; run;
1
/* Préparation de l'environnement CAS */
2
options cashost="cloud.example.com" casport=5570;
3
cas casauto;
4
LIBNAME mycas cas caslib="CASUSER(your-username)";
Explanation : This example uses the `table.append` action with tables of slightly different structures. The source table `nouveaux_clients` contains a `pays` column that does not exist in the target table `clients`. CAS will handle this incompatibility by ignoring the `pays` column during the append. The `promote=TRUE` option on the target table ensures that the updated table is visible to all users with appropriate permissions in the CAS session. The `noAutoPromote=FALSE` option is included to ensure that if 'promote' is used, it overrides automatic promotion if it is disabled by default.
Copied!
/* Préparation de l'environnement CAS */
options cashost="cloud.example.com" casport=5570;
cas casauto;
libname mycas cas caslib="CASUSER(your-username)";
/* Création de la table cible (clients) */
data mycas.clients;
length id 8 nom $10 age 8;
id = 1;
nom = 'Alice';
age = 30;
output;
run;
/* Création de la table source (nouveaux_clients) avec une colonne supplémentaire */
data mycas.nouveaux_clients;
length id 8 nom $10 age 8 pays $10;
id = 3;
nom = 'Charlie';
age = 25;
pays = 'France';
output;
run;
/* Ajout avec l'option 'casout' et 'promote' pour rendre la table visible et 'noAutoPromote=FALSE' */
proc cas;
table.append /
source={caslib='CASUSER(your-username)',name='nouveaux_clients'}
target={caslib='CASUSER(your-username)',name='clients', promote=TRUE, noAutoPromote=FALSE};
run;
/* Affichage de la table clients mise à jour */
proc print data=mycas.clients; run;
1
/* Préparation de l'environnement CAS */
2
options cashost="cloud.example.com" casport=5570;
3
cas casauto;
4
LIBNAME mycas cas caslib="CASUSER(your-username)";
5
6
/* Création de la table cible (clients) */
7
DATA mycas.clients;
8
LENGTH id 8 nom $10 age 8;
9
id = 1;
10
nom = 'Alice';
11
age = 30;
12
OUTPUT;
13
RUN;
14
15
/* Création de la table source (nouveaux_clients) avec une colonne supplémentaire */
16
DATA mycas.nouveaux_clients;
17
LENGTH id 8 nom $10 age 8 pays $10;
18
id = 3;
19
nom = 'Charlie';
20
age = 25;
21
pays = 'France';
22
OUTPUT;
23
RUN;
24
25
/* Ajout avec l'option 'casout' et 'promote' pour rendre la table visible et 'noAutoPromote=FALSE' */
Explanation : This advanced example demonstrates how to rename columns from the source table on the fly before the append operation. The `commandes_recentes` table has different column names than `commandes_historiques`. The `vars` option in the `source` parameter is used to specify the column mapping (`rename='new_name'`). This ensures that the source data is correctly aligned with the target structure.
Copied!
/* Préparation de l'environnement CAS */
options cashost="cloud.example.com" casport=5570;
cas casauto;
libname mycas cas caslib="CASUSER(your-username)";
/* Création de la table cible (commandes_historiques) */
data mycas.commandes_historiques;
length id_commande 8 article $15 quantite 8 date_achat $10;
id_commande = 101;
article = 'Livre A';
quantite = 2;
date_achat = '2023-01-15';
output;
run;
/* Création d'une table source (commandes_recentes) avec des colonnes à renommer/filtrer */
data mycas.commandes_recentes;
length order_id 8 product_name $15 qty 8 purchase_date $10;
order_id = 102;
product_name = 'Stylo B';
qty = 5;
purchase_date = '2023-02-20';
output;
order_id = 103;
product_name = 'Cahier C';
qty = 1;
purchase_date = '2023-03-01';
output;
run;
/* Ajout après renommage des colonnes dans la source pour correspondre à la cible */
proc cas;
table.append /
source={caslib='CASUSER(your-username)', name='commandes_recentes',
vars={{name='order_id', rename='id_commande'},
{name='product_name', rename='article'},
{name='qty', rename='quantite'},
{name='purchase_date', rename='date_achat'}}
}
target={caslib='CASUSER(your-username)', name='commandes_historiques'};
run;
/* Affichage de la table commandes_historiques mise à jour */
proc print data=mycas.commandes_historiques; run;
1
/* Préparation de l'environnement CAS */
2
options cashost="cloud.example.com" casport=5570;
3
cas casauto;
4
LIBNAME mycas cas caslib="CASUSER(your-username)";
5
6
/* Création de la table cible (commandes_historiques) */
/* Affichage de la table commandes_historiques mise à jour */
44
PROC PRINTDATA=mycas.commandes_historiques; RUN;
4 Code Block
DATA STEP / PROC CAS Data
Explanation : This example explores handling incompatible data types during append. Initially, the source table `nouveaux_produits` has the `prix` column as a character string, while the target table `produits` has `prix` as numeric. A direct attempt to append with `addCols=TRUE` (which by default handles missing columns) could fail or result in implicit type conversion with data loss if the types are too different. The solution shown is to create an intermediate `DATA` step (`nouveaux_produits_clean`) to explicitly convert the `prix` column from the source to numeric before proceeding with the append. This ensures type compatibility and successful operation.
Copied!
/* Préparation de l'environnement CAS */
options cashost="cloud.example.com" casport=5570;
cas casauto;
libname mycas cas caslib="CASUSER(your-username)";
/* Création de la table cible (produits) */
data mycas.produits;
length ref 8 nom_produit $15 prix 8;
ref = 1;
nom_produit = 'Pommes';
prix = 1.50;
output;
run;
/* Création d'une table source (nouveaux_produits) avec type de données incompatible pour 'prix' */
data mycas.nouveaux_produits;
length ref 8 nom_produit $15 prix $10;
ref = 2;
nom_produit = 'Poires';
prix = '2.25 EUR'; /* Prix comme chaîne de caractères */
output;
ref = 3;
nom_produit = 'Bananes';
prix = '1.80'; /* Prix comme chaîne de caractères */
output;
run;
/* Tentative d'ajout avec l'option 'addCols=FALSE' pour ignorer les colonnes incompatibles, ou 'addCols=TRUE' pour tenter d'ajouter les colonnes */
/* Dans cet exemple, nous allons d'abord illustrer un échec potentiel en omettant 'addCols' ou en l'utilisant avec 'promote=FALSE' sur source, puis une solution. */
/* Tentative échouée si les types sont trop différents et sans gestion spécifique */
proc cas;
table.append /
source={caslib='CASUSER(your-username)',name='nouveaux_produits'}
target={caslib='CASUSER(your-username)',name='produits'}
addCols=TRUE; /* Tente d'ajouter de nouvelles colonnes si elles n'existent pas */
/* Si les colonnes existent mais ont des types incompatibles, cela peut échouer ou entraîner une conversion de type */
run;
/* Pour gérer l'incompatibilité de type sur 'prix', on peut la renommer ou la supprimer/convertir dans la source */
/* Ici, nous allons convertir 'prix' en numérique avant l'ajout */
data nouveaux_produits_clean;
set mycas.nouveaux_produits;
prix_num = input(prix, BEST.); /* Convertit la chaîne en numérique */
drop prix;
rename prix_num=prix;
run;
/* Ajout avec la table source nettoyée */
proc cas;
table.append /
source={caslib='CASUSER(your-username)',name='nouveaux_produits_clean'}
target={caslib='CASUSER(your-username)',name='produits'};
run;
/* Affichage de la table produits mise à jour */
proc print data=mycas.produits; run;
1
/* Préparation de l'environnement CAS */
2
options cashost="cloud.example.com" casport=5570;
3
cas casauto;
4
LIBNAME mycas cas caslib="CASUSER(your-username)";
5
6
/* Création de la table cible (produits) */
7
DATA mycas.produits;
8
LENGTH ref 8 nom_produit $15 prix 8;
9
ref = 1;
10
nom_produit = 'Pommes';
11
prix = 1.50;
12
OUTPUT;
13
RUN;
14
15
/* Création d'une table source (nouveaux_produits) avec type de données incompatible pour 'prix' */
16
DATA mycas.nouveaux_produits;
17
LENGTH ref 8 nom_produit $15 prix $10;
18
ref = 2;
19
nom_produit = 'Poires';
20
prix = '2.25 EUR'; /* Prix comme chaîne de caractères */
21
OUTPUT;
22
ref = 3;
23
nom_produit = 'Bananes';
24
prix = '1.80'; /* Prix comme chaîne de caractères */
25
OUTPUT;
26
RUN;
27
28
/* Tentative d'ajout avec l'option 'addCols=FALSE' pour ignorer les colonnes incompatibles, ou 'addCols=TRUE' pour tenter d'ajouter les colonnes */
29
/* Dans cet exemple, nous allons d'abord illustrer un échec potentiel en omettant 'addCols' ou en l'utilisant avec 'promote=FALSE' sur source, puis une solution. */
30
/* Tentative échouée si les types sont trop différents et sans gestion spécifique */
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.