BookmarkSubscribeRSS Feed
helannivas88
Obsidian | Level 7

Hi, 

 

I have a requirement to  truncate all the tables in DB2 environment from SAS. I have written a below query in a loop and I'm facing few issues.

 

proc sql;
connect to DB2(datasrc=XXXXX authdomain="XXXXX");

select * into :tablename from connection to db2 (
select tabname
from
SYSCAT.TABLES
where TABSCHEMA = %str(%')%upcase(TEST)%str(%') and
TYPE = %str(%')%upcase(T)%str(%')

);
disconnect from DB2;
quit;

%let tablename1 = &tablename.;


%macro truncate_table;
%do i = 1 %to &tablename1.;
proc sql;
connect to DB2(datasrc=XXXXX authdomain="XXXXX");
exec ( truncate table TEST.&tablename1. immediate );
disconnect from DB2;
quit;
%end;
%mend;

%truncate_table;

 

Below is my error message

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: &tablename1.
ERROR: The %TO value of the %DO I loop is invalid.
ERROR: The macro TRUNCATE_TABLE will stop executing.

 

Please let me know where I went wrong. Thanks

1 REPLY 1
Shmuel
Garnet | Level 18

Few remarks to your code:

1) Try replace %str(%')%upcase(TEST)%str(%')  by %quote(%upacse(TEST))

2) I suppose that tabname is a string entered into the macro variable tablename

    and then assigned to tabelname1  - then what does next line mean?

%do i = 1 %to &tablename1.;

   I guess that tablename contains a list of tables' names and you want to execute 

   %truncate_table with each of the names. If so then you need to:

select * into :tablename separated by " "
    from connection to db2( ... etc ...

  and  count the number  of names:

do i=1 %to %sysfunc(countw(&tablename));
     %let tablename1 = %scan(&tablename,&i);
     ... etc. ...

I hope the other code relating to DB2 is OK.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1 reply
  • 389 views
  • 1 like
  • 2 in conversation