Published on :
ETL CREATION_INTERNE

Save a SAS7BDAT file to a Caslib

This code is also available in: Deutsch Español Français
Awaiting validation
The 'table.save' action in SAS© Viya allows saving in-memory CAS tables to various file formats, including SAS©7BDAT. The 'exportOptions' parameter is crucial for specifying the output file type ('basesas' for SAS©7BDAT) and type-specific options, such as encryption. This example illustrates saving a table with AES2 encryption and a password. Each example is self-contained and includes the necessary data creation using a DATA step with DATALINES.
Data Analysis

Type : CREATION_INTERNE


The examples use generated data (datalines) to ensure their autonomy. The 'cholesterol' table is created and loaded into CAS memory for the save demonstrations.

1 Code Block
DATA STEP / PROC CAS Data
Explanation :
This example creates a simple SAS table using a DATA step with DATALINES, loads it into CAS memory, and then saves it as a SAS7BDAT file in the 'casuser' caslib without any encryption options. The 'fileinfo' command verifies that the file was created successfully.
Copied!
1DATA work.cholesterol;
2 INPUT Name $ Age Sex $ Cholesterol_Level;
3 DATALINES;
4John_Doe 45 M 200
5Jane_Smith 30 F 180
6Peter_Jones 55 M 240
7Alice_Brown 25 F 160
8;
9RUN;
10 
11cas casauto sessopts=(caslib="casuser");
12 
13PROC CAS;
14 TABLE.upload /
15 caslib="casuser",
16 path="cholesterol",
17 casout={name="cholesterol", replace=true};
18 TABLE.save /
19 TABLE="cholesterol",
20 name="cholesterol_basic.sas7bdat",
21 exportOptions={fileType="basesas"},
22 replace=true;
23QUIT;
24 
25PROC CAS;
26 TABLE.fileinfo / caslib="casuser" pattern="cholesterol_basic.sas7bdat";
27QUIT;
2 Code Block
DATA STEP / PROC CAS Data
Explanation :
This case reproduces the example from the documentation. It creates the 'cholesterol' table, loads it into CAS memory, and then saves it as an AES2 encrypted SAS7BDAT file with a specified password. The 'fileinfo' command verifies that the file was created successfully.
Copied!
1DATA work.cholesterol;
2 INPUT Name $ Age Sex $ Cholesterol_Level;
3 DATALINES;
4John_Doe 45 M 200
5Jane_Smith 30 F 180
6Peter_Jones 55 M 240
7Alice_Brown 25 F 160
8;
9RUN;
10 
11cas casauto sessopts=(caslib="casuser");
12 
13PROC CAS;
14 TABLE.upload /
15 caslib="casuser",
16 path="cholesterol",
17 casout={name="cholesterol", replace=true};
18 TABLE.save /
19 TABLE="cholesterol",
20 name="cholesterol_encr.sas7bdat",
21 exportOptions={
22 fileType="basesas",
23 encrypt="AES2",
24 encryptionPassword="pasquotank"
25 },
26 replace=true;
27QUIT;
28 
29PROC CAS;
30 TABLE.fileinfo / caslib="casuser" pattern="cholesterol_encr.sas7bdat";
31QUIT;
3 Code Block
DATA STEP / PROC CAS Data
Explanation :
This example illustrates a more advanced usage by saving a subset of data (here, records where Cholesterol_Level is greater than 200) to a different format, namely CSV. The 'where' parameter is used for filtering and 'exportOptions={fileType="csv"}' for the output format. The 'fileinfo' command verifies that the file was created successfully.
Copied!
1DATA work.cholesterol;
2 INPUT Name $ Age Sex $ Cholesterol_Level;
3 DATALINES;
4John_Doe 45 M 200
5Jane_Smith 30 F 180
6Peter_Jones 55 M 240
7Alice_Brown 25 F 160
8;
9RUN;
10 
11cas casauto sessopts=(caslib="casuser");
12 
13PROC CAS;
14 TABLE.upload /
15 caslib="casuser",
16 path="cholesterol",
17 casout={name="cholesterol", replace=true};
18 
19 TABLE.save /
20 TABLE="cholesterol",
21 where="Cholesterol_Level > 200",
22 name="cholesterol_filtered.csv",
23 exportOptions={fileType="csv"},
24 replace=true;
25QUIT;
26 
27PROC CAS;
28 TABLE.fileinfo / caslib="casuser" pattern="cholesterol_filtered.csv";
29QUIT;
4 Code Block
DATA STEP / PROC CAS Data
Explanation :
This example shows how to create a temporary caslib ('my_temp_caslib') and save the 'cholesterol' table to it. This is useful for managing data between different CAS storage spaces or for preparing data for external use outside the original caslib. The temporary caslib is deleted at the end of execution for cleanup. The 'fileinfo' command verifies that the file was created successfully.
Copied!
1DATA work.cholesterol;
2 INPUT Name $ Age Sex $ Cholesterol_Level;
3 DATALINES;
4John_Doe 45 M 200
5Jane_Smith 30 F 180
6Peter_Jones 55 M 240
7Alice_Brown 25 F 160
8;
9RUN;
10 
11cas casauto sessopts=(caslib="casuser");
12 
13PROC CAS;
14 TABLE.upload /
15 caslib="casuser",
16 path="cholesterol",
17 casout={name="cholesterol", replace=true};
18 
19 caslib.add /
20 caslib="my_temp_caslib",
21 path="/tmp/my_temp_data",
22 subdirs=true,
23 global=false,
24 dataSource={srcType="path"};
25 
26 TABLE.save /
27 TABLE="cholesterol",
28 caslib="my_temp_caslib", /* Spécifie la caslib de destination */
29 name="cholesterol_remote.sas7bdat",
30 exportOptions={fileType="basesas"},
31 replace=true;
32QUIT;
33 
34PROC CAS;
35 TABLE.fileinfo / caslib="my_temp_caslib" pattern="cholesterol_remote.sas7bdat";
36 caslib.drop / caslib="my_temp_caslib" quiet=true;
37QUIT;
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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved


Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« If you are saving very large tables, be aware that .sas7bdat is a single-threaded format. For massive datasets, saving as a partitioned .sashdat or a .parquet file will offer significantly better performance for future CAS loads »