Hello,
I am trying to write a program on Viya to save all in-memory tables in a particular set of caslibs. I have written code that can identify the caslibs and iterate through them to do something (for now, just print the names of the tables).
proc cas;
table.caslibinfo result=caslibs / active=false srcType="PATH" showHidden=true;
caslibNames = caslibs.caslibinfo.where(Name LIKE 'text%')[,'Name'];
do name over caslibNames;
print name; /* show current caslib name */
table.tableInfo result=tables / caslib=name;
if tables[1] ne . then do; /* if statement skips caslibs that have no tables */
tableNames = tables.tableInfo[,'Table Name'];
do name over tableNames;
print name; /* show table name, replace with code to save table */
end;
end;
end;
run;
I've been looking at the table.save action, but I can't figure out how to programatically specify the caslib and table names based on where the program currently is in the do loops.
Any advice would be welcomed! Thank you!
Hi
I made some changes to your code to return full information for cas libraries and cas tables. I then added the table.save action.
The table.save action will save a table (in memory) to the physical location of the same cas library. For testing purposes I haved added "2" at the end of the filename.
Here is the code:
cas sugus sessopts=(caslib="casuser");
/*
* create some test data in casuser
*/
libname casuser cas caslib="casuser";
data casuser.mycars(replace=yes) casuser.mycars_a(replace=yes);
set sashelp.cars;
run;
data casuser.myclass(replace=yes);
set sashelp.class;
run;
proc cas;
table.caslibinfo result=caslibs / srcType="PATH" ;
caslibNames = caslibs.caslibinfo.where(Name LIKE 'CASUSER%');
do currentCaslib over caslibNames;
print "NOTE: currentCaslib=" currentCaslib.name; /* show current caslib name */
table.tableInfo result=tables / caslib=currentCaslib.name;
if exists(tables, 'TableInfo') then do; /* if statement skips caslibs that have no tables */
tableNames = tables.tableInfo;
do currentCastable over tableNames;
print "NOTE: currentCastable=" currentCastable.name; /* show table name, replace with code to save table */
action table.save /
/* source caslib and table name */
table={
caslib=currentCaslib.name
name=currentCastable.name
}
/* target caslib and filename info */
caslib=currentCaslib.name
name=cats(currentCastable.name, "2")
replace=true
;
end;
/*
* check for files being written
*/
action table.fileinfo / caslib=currentCaslib.name allFiles=TRUE ;
end;
end;
run;
Hi
I made some changes to your code to return full information for cas libraries and cas tables. I then added the table.save action.
The table.save action will save a table (in memory) to the physical location of the same cas library. For testing purposes I haved added "2" at the end of the filename.
Here is the code:
cas sugus sessopts=(caslib="casuser");
/*
* create some test data in casuser
*/
libname casuser cas caslib="casuser";
data casuser.mycars(replace=yes) casuser.mycars_a(replace=yes);
set sashelp.cars;
run;
data casuser.myclass(replace=yes);
set sashelp.class;
run;
proc cas;
table.caslibinfo result=caslibs / srcType="PATH" ;
caslibNames = caslibs.caslibinfo.where(Name LIKE 'CASUSER%');
do currentCaslib over caslibNames;
print "NOTE: currentCaslib=" currentCaslib.name; /* show current caslib name */
table.tableInfo result=tables / caslib=currentCaslib.name;
if exists(tables, 'TableInfo') then do; /* if statement skips caslibs that have no tables */
tableNames = tables.tableInfo;
do currentCastable over tableNames;
print "NOTE: currentCastable=" currentCastable.name; /* show table name, replace with code to save table */
action table.save /
/* source caslib and table name */
table={
caslib=currentCaslib.name
name=currentCastable.name
}
/* target caslib and filename info */
caslib=currentCaslib.name
name=cats(currentCastable.name, "2")
replace=true
;
end;
/*
* check for files being written
*/
action table.fileinfo / caslib=currentCaslib.name allFiles=TRUE ;
end;
end;
run;
Thank you so much! This solution worked perfectly.
Yatin
I suggest to open a new discussion and provide your code sample with the log, this will help.
Thanks
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.