DATA Step, Macro, Functions and more

trailing blanks

Reply
Super Contributor
Posts: 647

trailing blanks

How to trim or delete trailing blanks in the macro whskt?
proc sql;
select table_name into:wksht from tabnaes;
quit;
%put &wksht.;
Frequent Contributor
Posts: 102

Re: trailing blanks

data tabnaes;
table_name = "Hello World ";
run;

proc sql;
select table_name into:wksht from tabnaes; quit;
%let wksht = %trim(&wksht);
%put |&wksht.|;
Super Contributor
Posts: 647

Re: trailing blanks

Thanks curtis.
I used this one:
(select * from [%sysfunc(compress(&wksht))]);
Frequent Contributor
Posts: 102

Re: trailing blanks

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
Super Contributor
Posts: 647

Re: trailing blanks

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.
Frequent Contributor
Posts: 102

Re: trailing blanks

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;
Super Contributor
Posts: 647

Re: trailing blanks

I will take that into consideration Curtis.
Ask a Question
Discussion stats
  • 6 replies
  • 154 views
  • 0 likes
  • 2 in conversation