- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you so much! This solution worked perfectly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I have the exact same situation. I tried to use your code but it will not go inside the "if exists(tables, 'TableInfo') then do;" condition.
If I use print tables; the result shows some tables. if I comment the if statement the code works like a charm.
Any suggestions why the if statement might be failing ? is there any documentation for the results format for table actions
Thanks
Yatin Rao
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yatin
I suggest to open a new discussion and provide your code sample with the log, this will help.
Thanks