When working with SAS Viya you will eventually run into situations where the dataset you've worked with has dropped from CAS memory to a file in the CASLIB- library. In order to load the file or files back to memory, you typically need to list the names of the files with CASUTIL- procedure to find out the actual names of these files. And then you need ro re-run the same CASUTIL- procedure in order to load the files to memory using their full names including the correct suffixes. If you are like me, you end up copy-pasting all the names needed from Log- window to program editor one at the time. And, if you are like me, you never remember to promote the tables and you do this yet one more time.
One feature I've been missing in CASUTIL- procedure is that it would be really nice if I could load all files with some specific text in the file name with one go. For example, I would like to load all my ABT- tables from files to memory.
I've now done a SAS Macro called LoadToCAS which loads all files contaiting a specific text from files to CAS memory. The default CASlib is PUBLIC, but it can be over-ridden.
Examples:
%LoadToCAS(TableNamesWith="HMEQ_"); - macro call would load all files in PUBLIC -caslib containing text "HMEQ_" to memory.
%LoadToCAS(CASLIB=CASUSER, TableNamesWith="ABT_"); - macro call would load all files in CASUSER -caslib containing text "ABT_" to memory.
%macro LoadToCAS(CASLIB=PUBLIC, TableNamesWith="");
/*
This SAS Macro loads the wanted files from files to CAS. The default CASLIB- is PUBLIC, but
it can be overridde by the macro's CASLIB- parameter.
This Macro loads all files containing the example text in TableNamesWith- parameter.
Note! TableNamesWith- parameter should be provided within the douple quotes.
Ex1. %LoadToCAS(TableNamesWith="HMEQ_");
- macro call would load all files in PUBLIC -caslib containing text "HMEQ_" to memory.
Ex2. %LoadToCAS(CASLIB=CASUSER, TableNamesWith="ABT_");
- macro call would load all files in CASUSER -caslib containing text "ABT_" to memory.
*/
/* Let's capture the file listing of CASUTIL- procedure. */
ods output FileInfo=FileInfo;
proc casutil incaslib= &CASLIB.;
list files;
run;
/* Create a dataset containing all rows with the table names we want. */
/* Also, count the number of rows. */
data WORK._FILES;
set WORK.FILEINFO;
where Name contains &TableNamesWith.;
call symput("_CASDATA_" || left(put(_N_, 4.)), compress(Name));
call symput("_CASOUT_" || left(put(_N_, 4.)), compress(scan(Name, 1, ".")));
call symput("_N_", _N_);
run;
/* Loop the rows and load the wanted files to CAS. */
proc casutil incaslib= &CASLIB. outcaslib= &CASLIB.;
%do i = 1 %to &_N_.;
load casdata="&&_CASDATA_&i.." CASOUT="&&_CASOUT_&i.." promote;
%end;
run;
%mend;