Hi,
I have tons of SAS program code with trailing tab characters at the end of each line which I want to get rid off. Is there any function similar to TRIM that is suitable for this task?
I modified code (see below) taken from the following discussion that was suggested by Tom in order to deal with trailing blanks: https://communities.sas.com/message/104870#104870.
1) I'm not sure if the modified code (see below) always leads to the desired result (removing only trailing tab characters).
2) Isn't there a simpler way/a function similar to TRIM to remove trailing tab characters?
Data _null_;
infile "Code_w_trailingTabs.sas" lrecl=1000;
file "Code_wo_trailingTabs.sas" lrecl=1000;
input;
b=anyalnum(_infile_,-1000);
c=anypunct(_infile_,-1000);
abc=substr(_infile_,1,max(b,c));
put abc;
run;
Modifying the code you've posted something like below should work (not tested):
Data _null_;
infile "Code_w_trailingTabs.sas" lrecl=1000;
file "Code_wo_trailingTabs.sas" lrecl=1000;
input;
length abc $255;
abc=PRXCHANGE('s/\t+$//oi', 1, _infile_);
put abc;
run;
Modifying the code you've posted something like below should work (not tested):
Data _null_;
infile "Code_w_trailingTabs.sas" lrecl=1000;
file "Code_wo_trailingTabs.sas" lrecl=1000;
input;
length abc $255;
abc=PRXCHANGE('s/\t+$//oi', 1, _infile_);
put abc;
run;
@Patrick
Thank you very much! I'll have to get more familiar with PRX. Your code works perfectly.
@data_null_ d
Your code would also work nicely. However, I want to keep the tabs which I use to indent my code.
Do you have and want to preserve tabs that are in the code elsewhere.
It they are only at the end you could just translate to blank and the put with the lines with $varying.
input;
_infile_ = tranlate(_infile_,' ','09'x);
l = length(_infile_);
put _infile_ $varying1024. l;
Why do you want to remove the tabs?
What about if there are multiple tabs at the end of the lines? Do you want to remove all of the trailing tabs?
What about the tabs in the middle of the lines?
Do you want SAS to expand the tabs in the middle of the lines to spaces?
One way is to have SAS convert ALL of the tabs to spaces and then remove the trailing spaces.
data _null_;
infile 'old.sas' lrecl=1000 expandtabs;
file 'new.sas' lrecl=1000;
input;
len= length(_infile_);
put _infile_ $varying1000. len ;
run;
Hi @Tom, I just want to remove all of the trailing tabs. They are the ones which were unintentionally added when code was copied/pasted.
I'm using tabs at the beginning and in the middle of the lines to indent my code, so I want to keep those.
Whether to embed tabs in program codes is a bit of a programming Holy War topic.
http://c2.com/cgi/wiki?TabsVersusSpaces
If you are using the SAS Enhanced Editor then you can have the best of both worlds by telling it to expand the tabs to spaces. That way you can use tabs while entering text but not get the side effects they can cause. Just check the boxes for expanding the tabs, plus you can change the tab stop to something smaller than the screen real estate stealing 8 character default.
@Tom
Thank you very much! I'm using SAS 9.3 with the enhanced editor, and it seems that I have 4 characters as tab size by default.
Probably a little late but I was looking for the same thing and knew there is a shortcut for it in the SAS editor.
I found the answer on the support site: it's CTRL+SHIFT+W in your Editor Window.
See also: http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/viewer.htm#eeshortcuts.htm
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.