The `table.index` action is used to create an index on specified columns of a SASHDAT file. Unlike in-memory indexing after loading, this method allows the SASHDAT file to be designated as input for the `index` action via the `table` parameter. This optimizes resource utilization because the action reads the file directly from disk rather than requiring the full original table to be loaded into memory. The result of the operation is a new in-memory CAS table with the indexed columns. This new table can then be saved to disk, potentially replacing the original SASHDAT file or creating a new one, to persist the indexing. The `table.tableDetails` action is then used to verify the presence and size of the index in the resulting table.
Data Analysis
Type : INTERNAL_CREATION
The examples use data generated via SAS DATA steps and CAS procedures to create temporary SASHDAT files for demonstration purposes, ensuring the autonomy of each code block.
1 Code Block
DATA STEP / table.index Data
Explanation : This example creates a simple SASHDAT file `monfichier_unindexed.sashdat` in a temporary caslib. It then uses the `table.index` action to create an index on the 'ID' column by specifying the SASHDAT file directly. The indexed table `monfichier_indexed` is then saved to persist the indexing in the original SASHDAT file. The `table.tableDetails` action is used to confirm that the index has been created.
Copied!
/* Assurez-vous qu'une session CAS est active */
options cashost="localhost" casport=5570;
cas casauto;
/* Crée une caslib temporaire si elle n'existe pas */
proc cas;
caslib _all_ assign;
builtins.addCaslib / name='mycaslib', path='/tmp/mycaslib', subdirs=TRUE, dataSource={srcType='PATH'};
quit;
/* 1. Création d'un fichier SASHDAT simple sur disque */
data casuser.monfichier_unindexed;
input ID $ Nom $ Valeur;
datalines;
A Jean 100
B Marie 150
C Pierre 200
D Anne 120
;
run;
proc casutil;
save casuser.monfichier_unindexed / caslib='mycaslib' replace;
quit;
/* 2. Indexation d'une seule colonne du fichier SASHDAT */
proc cas;
table.index /
table={name='monfichier_unindexed.sashdat', caslib='mycaslib'},
casout={name='monfichier_indexed', replace=TRUE, indexVars={'ID'}};
run;
/* 3. Sauvegarde de la table indexée, remplaçant l'originale pour persister l'index */
table.save /
table='monfichier_indexed',
name='monfichier_unindexed.sashdat',
caslib='mycaslib',
replace=TRUE;
run;
/* 4. Vérification de l'indexation (optionnel) */
table.tableDetails / table={name='monfichier_indexed', caslib='mycaslib'};
run;
quit;
1
/* Assurez-vous qu'une session CAS est active */
2
options cashost="localhost" casport=5570;
3
cas casauto;
4
5
/* Crée une caslib temporaire si elle n'existe pas */
Explanation : This example illustrates the indexing of multiple columns ('Produit', 'Region') of a SASHDAT file. After creating and saving the `ventes_unindexed.sashdat` file, the `table.index` action creates a composite index. `table.tableDetails` is then used to display detailed information for the in-memory table `ventes_indexed`, confirming the presence and configuration of the new index, including its size.
Copied!
/* Assurez-vous qu'une session CAS est active */
options cashost="localhost" casport=5570;
cas casauto;
/* Crée une caslib temporaire si elle n'existe pas */
proc cas;
caslib _all_ assign;
builtins.addCaslib / name='mycaslib', path='/tmp/mycaslib', subdirs=TRUE, dataSource={srcType='PATH'};
quit;
/* 1. Création d'un autre fichier SASHDAT sur disque */
data casuser.ventes_unindexed;
input Produit $ Region $ Montant;
datalines;
A Est 1000
B Ouest 1500
A Nord 800
C Sud 2200
B Est 1100
;
run;
proc casutil;
save casuser.ventes_unindexed / caslib='mycaslib' replace;
quit;
/* 2. Indexation de plusieurs colonnes */
proc cas;
table.index /
table={name='ventes_unindexed.sashdat', caslib='mycaslib'},
casout={name='ventes_indexed', replace=TRUE, indexVars={'Produit', 'Region'}};
run;
/* 3. Sauvegarde de la table indexée */
table.save /
table='ventes_indexed',
name='ventes_unindexed.sashdat',
caslib='mycaslib',
replace=TRUE;
run;
/* 4. Affichage des détails de la table pour confirmer l'index */
table.tableDetails / table={name='ventes_indexed', caslib='mycaslib'};
run;
quit;
1
/* Assurez-vous qu'une session CAS est active */
2
options cashost="localhost" casport=5570;
3
cas casauto;
4
5
/* Crée une caslib temporaire si elle n'existe pas */
Explanation : This advanced example demonstrates how to create an index on a large SASHDAT file and reload it into memory with the index intact. After creating a large dataset `grandedonnees_unindexed.sashdat` and indexing it on 'ID' and 'Groupe', the indexed table is saved. The in-memory table is then dropped to simulate a fresh start. Finally, the table is reloaded into memory via `table.loadTable`, and `table.tableDetails` confirms that the indexing was preserved during persistence and reloading, demonstrating the efficiency of this approach for large volumes.
Copied!
/* Assurez-vous qu'une session CAS est active */
options cashost="localhost" casport=5570;
cas casauto;
/* Crée une caslib temporaire si elle n'existe pas */
proc cas;
caslib _all_ assign;
builtins.addCaslib / name='mycaslib', path='/tmp/mycaslib', subdirs=TRUE, dataSource={srcType='PATH'};
quit;
/* 1. Création d'un fichier SASHDAT de plus grande taille */
data casuser.grandedonnees_unindexed;
do i = 1 to 100000;
ID = i;
Groupe = ceil(i / 1000);
Valeur = ranuni(0) * 1000;
output;
end;
run;
proc casutil;
save casuser.grandedonnees_unindexed / caslib='mycaslib' replace;
quit;
/* 2. Indexation de colonnes clés directement depuis le fichier SASHDAT sur disque */
proc cas;
table.index /
table={name='grandedonnees_unindexed.sashdat', caslib='mycaslib'},
casout={name='grandedonnees_indexed', replace=TRUE, indexVars={'ID', 'Groupe'}};
run;
/* 3. Sauvegarde de la table indexée, pour persister l'index sur disque */
table.save /
table='grandedonnees_indexed',
name='grandedonnees_unindexed.sashdat',
caslib='mycaslib',
replace=TRUE;
run;
/* 4. Nettoyage de la table en mémoire pour simuler un rechargement */
table.dropTable / name='grandedonnees_indexed';
run;
/* 5. Chargement de la table SASHDAT qui inclut maintenant l'index */
table.loadTable /
caslib='mycaslib',
path='grandedonnees_unindexed.sashdat',
casout={name='grandedonnees_reloaded', replace=TRUE};
run;
/* 6. Vérification que la table rechargée possède bien l'index */
table.tableDetails / table={name='grandedonnees_reloaded', caslib='mycaslib'};
run;
quit;
1
/* Assurez-vous qu'une session CAS est active */
2
options cashost="localhost" casport=5570;
3
cas casauto;
4
5
/* Crée une caslib temporaire si elle n'existe pas */
Explanation : This example demonstrates how to work with SASHDAT files from the file system to CAS. A SASHDAT file is first created locally, then loaded into an in-memory CAS table. The `table.index` action is then applied to this in-memory CAS table to create a new CAS table with the desired indexes. Finally, this indexed CAS table is saved to a new SASHDAT file on disk, incorporating the indexing. `table.tableDetails` is used to confirm the index on the in-memory CAS table.
Copied!
/* Assurez-vous qu'une session CAS est active */
options cashost="localhost" casport=5570;
cas casauto;
/* Crée une caslib temporaire si elle n'existe pas */
proc cas;
caslib _all_ assign;
builtins.addCaslib / name='mycaslib', path='/tmp/mycaslib', subdirs=TRUE, dataSource={srcType='PATH'};
quit;
/* 1. Création d'un fichier SASHDAT local */
data _null_;
file '/tmp/mycaslib/data_locale_unindexed.sashdat';
do i = 1 to 5000;
ID = i;
Categorie = ceil(i / 1000);
Value = rannor(0);
output;
end;
run;
/* 2. Chargement du fichier SASHDAT dans CAS en tant que table en mémoire */
proc casutil;
load data='/tmp/mycaslib/data_locale_unindexed.sashdat' outcaslib='mycaslib' casout='data_locale_cas' replace;
quit;
/* 3. Création d'une nouvelle table CAS indexée à partir de la table en mémoire */
proc cas;
table.index /
table={name='data_locale_cas', caslib='mycaslib'},
casout={name='data_locale_cas_indexed', replace=TRUE, indexVars={'ID', 'Categorie'}};
run;
/* 4. Vérification de l'indexation de la nouvelle table CAS */
table.tableDetails / table={name='data_locale_cas_indexed', caslib='mycaslib'};
run;
/* 5. Sauvegarde de la table CAS indexée vers un fichier SASHDAT */
table.save /
table='data_locale_cas_indexed',
name='data_locale_indexed.sashdat',
caslib='mycaslib',
replace=TRUE;
run;
quit;
1
/* Assurez-vous qu'une session CAS est active */
2
options cashost="localhost" casport=5570;
3
cas casauto;
4
5
/* Crée une caslib temporaire si elle n'existe pas */
/* 5. Sauvegarde de la table CAS indexée vers un fichier SASHDAT */
39
TABLE.save /
40
TABLE='data_locale_cas_indexed',
41
name='data_locale_indexed.sashdat',
42
caslib='mycaslib',
43
replace=TRUE;
44
RUN;
45
QUIT;
46
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.