BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
How to trim or delete trailing blanks in the macro whskt?
proc sql;
select table_name into:wksht from tabnaes;
quit;
%put &wksht.;
6 REPLIES 6
CurtisMack
Fluorite | Level 6
data tabnaes;
table_name = "Hello World ";
run;

proc sql;
select table_name into:wksht from tabnaes; quit;
%let wksht = %trim(&wksht);
%put |&wksht.|;
SASPhile
Quartz | Level 8
Thanks curtis.
I used this one:
(select * from [%sysfunc(compress(&wksht))]);
CurtisMack
Fluorite | Level 6
That works well as well and I find sysfunc calls to functions more predicatable. I used %trim because it is more straight foward in this situation.

You probably know this, but be aware that compress is not the same as trim and it will remove all blanks, not just the trailing blanks that the "into" statement generates. Probably not an issue in this case, but since you are using worksheet names, it might be.
------------------------------
data tabnaes;
table_name = "Hello World ";
run;
proc sql;
select table_name into:wksht from tabnaes; quit;
%put |%sysfunc(compress(&wksht))|;
%put |%sysfunc(trim(&wksht))|;
%put |%trim(&wksht)|;
------------------- result --------------------
|HelloWorld|
|Hello World|
|Hello World|
-----------------------------------------------
Curtis
SASPhile
Quartz | Level 8
Looks like %trim is more appropriate than compress function in my case.Thanks for the advice anticipating the situations which will arise in the future.
CurtisMack
Fluorite | Level 6
Actually, if you want those multy word spreadsheet names to really work, you will need to enclose them in single quotes followed by the letter n. like this.

'tmp stuff$'n

Here is a way to get that quoting right:
------------------------------------------

data tabnaes;
table_name = "tmp stuff$ ";
run;

libname myxls excel "c:\temp\temp.xls";

proc sql;
select table_name into:wksht from tabnaes; quit;
%let wksht = %str(%')%qsysfunc(trim(&wksht))%str(%')n;
quit;

data junk;
set myxls.%unquote(&wksht);
run;
SASPhile
Quartz | Level 8
I will take that into consideration Curtis.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1767 views
  • 0 likes
  • 2 in conversation