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.