Published on :
Machine Learning CREATION_INTERNE

Downloading an Astore Store to the Local File System

This code is also available in: Deutsch Español
Awaiting validation
This functionality of the ASTORE procedure facilitates model lifecycle management. After training a model on CAS (for example, with PROC NNET, PROC FOREST, etc.) and saving it as an analytical store, it may be necessary to move it from the CAS server to a local environment for various reasons: scoring on local data without a CAS connection, archiving, or integration into client applications. Downloading preserves the model's structure and weights for later use. The command requires identifying the model on the CAS server (caslib and model name) and specifying the local destination path. It is crucial to ensure that the local path is accessible and that permissions are sufficient to write the file.
Data Analysis

Type : CREATION_INTERNE


The examples simulate the existence of an ASTORE model on the CAS server and demonstrate download operations. The data required for the examples are generated internally or assumed to exist in SASHELP.

1 Code Block
PROC ASTORE Data
Explanation :
This example illustrates the simplest download of an ASTORE model. It first creates a dummy ASTORE model on the CAS server (in the 'mycaslib' caslib). Then, the PROC ASTORE procedure with the DOWNLOAD statement is used to transfer this model to the local file system, specified by 'casfile'. The './' path indicates the current working directory of the SAS session.
Copied!
1/* Étape 1: Préparation - S'assurer qu'un modèle ASTORE existe sur CAS */
2/* Cette étape est à des fins de démonstration. En pratique, un modèle serait déjà entraîné. */
3/* Créons une table CAS simple, puis un store bidon */
4PROC CAS;
5 SESSION casauto;
6 TABLE.addCaslib / caslib='mycaslib' path='/tmp' datasource={srctype='path'};
7 DATA _null_;
8 file caslib='mycaslib' '/tmp/my_simple_model.sashdat';
9 put 'MODEL_HEADER_INFO';
10 put 'MODEL_BINARY_CONTENT';
11 RUN;
12 SESSION.upload / caslib='mycaslib' path='/tmp/my_simple_model.sashdat' casout={name='mySimpleModel', replace=TRUE};
13QUIT;
14 
15/* Étape 2: Téléchargement du modèle ASTORE vers le système de fichiers local */
16/* Le fichier sera sauvegardé dans le répertoire de travail actuel de SAS. */
17PROC ASTORE;
18 download caslib="mycaslib" name="mySimpleModel" casfile="./mySimpleModel.astore";
19RUN;
2 Code Block
PROC ASTORE Data
Explanation :
This example demonstrates the use of the 'REPLACE' option in the DOWNLOAD statement, which allows replacing an existing file on the local file system without generating an error. It reuses the dummy model concept and specifies the local destination path with the replace option.
Copied!
1/* Étape 1: Préparation - Assurer l'existence d'un modèle ASTORE sur CAS */
2/* Réutilisons le modèle de l'exemple précédent ou créons-en un nouveau */
3PROC CAS;
4 SESSION casauto;
5 TABLE.addCaslib / caslib='mycaslib' path='/tmp' datasource={srctype='path'};
6 DATA _null_;
7 file caslib='mycaslib' '/tmp/another_model.sashdat';
8 put 'MODEL_HEADER_INFO_V2';
9 put 'MODEL_BINARY_CONTENT_V2';
10 RUN;
11 SESSION.upload / caslib='mycaslib' path='/tmp/another_model.sashdat' casout={name='anotherModel', replace=TRUE};
12QUIT;
13 
14/* Étape 2: Téléchargement du modèle ASTORE, en écrasant un fichier local existant */
15/* L'option REPLACE assure que le fichier local sera mis à jour. */
16PROC ASTORE;
17 download caslib="mycaslib" name="anotherModel" casfile="./anotherModel.astore" replace;
18RUN;
3 Code Block
PROC LOGISTIC / PROC ASTORE Data
Explanation :
This example shows a more realistic scenario where a model is first trained (here, a logistic regression with PROC LOGISTIC) and saved directly as an analytical store on the CAS server. Then, the PROC ASTORE DOWNLOAD statement is used to retrieve this trained model and save it locally. This is useful for inspecting the model or deploying it in an offline environment.
Copied!
1/* Étape 1: Connexion au serveur CAS et chargement de données */
2options casopts=(timeout=3600);
3cas casauto;
4caslib _all_ assign;
5 
6/* Chargement de données SASHELP dans CAS pour l'entraînement */
7DATA casuser.hmeq_data;
8 SET sashelp.hmeq;
9 where loan ne . and bad ne .;
10RUN;
11 
12/* Étape 2: Entraînement d'un modèle simple (régression logistique) et sauvegarde en ASTORE */
13PROC LOGISTIC DATA=casuser.hmeq_data;
14 model bad = loan mortdue yoq;
15 OUTPUT out=casuser.predicted_hmeq p=phat;
16 astore caslib='CASUSER' name='hmeq_logistic_model' label='HMEQ Logistic Model' replace;
17RUN;
18 
19/* Étape 3: Téléchargement du modèle ASTORE vers le système de fichiers local */
20PROC ASTORE;
21 download caslib="CASUSER" name="hmeq_logistic_model" casfile="./hmeq_logistic_model.astore" replace;
22RUN;
4 Code Block
PROC ASTORE Data
Explanation :
This example highlights the behavior of the DOWNLOAD statement when a local file with the same name already exists. The first attempt without the 'REPLACE' option illustrates the necessity of this option to overwrite existing files. The second attempt, with 'REPLACE', demonstrates how to force the download and update of the local file. This example is relevant in a Viya environment where local and CAS file management is common.
Copied!
1/* Étape 1: S'assurer de l'existence d'un modèle sur CAS pour le test */
2PROC CAS;
3 SESSION casauto;
4 TABLE.addCaslib / caslib='mycaslib' path='/tmp' datasource={srctype='path'};
5 DATA _null_;
6 file caslib='mycaslib' '/tmp/conditional_model.sashdat';
7 put 'MODEL_CONTENT_V_COND';
8 RUN;
9 SESSION.upload / caslib='mycaslib' path='/tmp/conditional_model.sashdat' casout={name='conditionalModel', replace=TRUE};
10QUIT;
11 
12/* Étape 2: Tentative de téléchargement - simule un fichier existant déjà */
13/* Crée un fichier bidon local pour simuler un fichier existant */
14DATA _null_;
15 file './conditionalModel.astore';
16 put 'OLD_LOCAL_MODEL_CONTENT';
17RUN;
18 
19/* Tente de télécharger sans l'option REPLACE (devrait échouer ou avertir) */
20PROC ASTORE;
21 download caslib="mycaslib" name="conditionalModel" casfile="./conditionalModel.astore";
22RUN;
23 
24/* Étape 3: Téléchargement forcé avec REPLACE */
25/* Cette étape réussira, écrasant le fichier local existant. */
26PROC ASTORE;
27 download caslib="mycaslib" name="conditionalModel" casfile="./conditionalModel.astore" replace;
28RUN;
29 
30/* Nettoyage : Suppression du fichier local et du modèle CAS */
31/* x 'rm ./conditionalModel.astore'; */ /* Décommenter si l'exécution en ligne de commande est autorisée */
32PROC CASUTIL;
33 delete casdata="conditionalModel" incaslib="mycaslib";
34 dropcaslib 'mycaslib';
35QUIT;
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.
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« Before downloading, use proc astore; describe caslib="CASUSER" name="yourModel"; run; to check the model size and input requirements. Large models (like deep random forests) can result in multi-gigabyte files that may impact local storage and network transfer times. »