BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mpg
Fluorite | Level 6 mpg
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

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;

View solution in original post

4 REPLIES 4
BrunoMueller
SAS Super FREQ

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;
mpg
Fluorite | Level 6 mpg
Fluorite | Level 6

Thank you so much! This solution worked perfectly.

yatinrao
Obsidian | Level 7
Bruno_SAS ,

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

BrunoMueller
SAS Super FREQ

Yatin

 

I suggest to open a new discussion and provide your code sample with the log, this will help.

 

Thanks

sas-innovate-2024.png

 

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1799 views
  • 2 likes
  • 3 in conversation