BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Georg_UPB
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

8 REPLIES 8
Patrick
Opal | Level 21

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;

Georg_UPB
Fluorite | Level 6

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

data_null__
Jade | Level 19

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;

Tom
Super User Tom
Super User

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;

Georg_UPB
Fluorite | Level 6

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.

Tom
Super User Tom
Super User

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

Georg_UPB
Fluorite | Level 6

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

Anielwa
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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