How to remove trailing tab characters (from SAS Code)

Accepted Solution Solved
Reply
Contributor
Posts: 38
Accepted Solution

How to remove trailing tab characters (from SAS Code)

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;


Accepted Solutions
Solution
‎09-01-2013 11:50 AM
Respected Advisor
Posts: 3,899

Re: How to remove trailing tab characters (from SAS Code)

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;

View solution in original post


All Replies
Solution
‎09-01-2013 11:50 AM
Respected Advisor
Posts: 3,899

Re: How to remove trailing tab characters (from SAS Code)

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;

Contributor
Posts: 38

Re: How to remove trailing tab characters (from SAS Code)

@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.

Respected Advisor
Posts: 3,777

Re: How to remove trailing tab characters (from SAS 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;

Super User
Super User
Posts: 6,502

Re: How to remove trailing tab characters (from SAS Code)

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;

Contributor
Posts: 38

Re: How to remove trailing tab characters (from SAS Code)

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.

Super User
Super User
Posts: 6,502

Re: How to remove trailing tab characters (from SAS Code)

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.

http://support.sas.com/documentation/cdl/en/hostwin/63285/HTML/default/images/eegen.gif

Contributor
Posts: 38

Re: How to remove trailing tab characters (from SAS Code)

@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.

N/A
Posts: 1

Re: How to remove trailing tab characters (from SAS Code)

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 2521 views
  • 3 likes
  • 5 in conversation