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

How can I remove only the 2nd space in a string?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data _null_;
x='xxx yyy zzz';
call scan(x,3,p,l,' ');
want=substr(x,1,p-2)||substr(x,p);
put x= want=;
run;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

There may be easier ways.

 

It may be that this is not what you really want.  Remember, you are only shifting text over since the length of a character variable is fixed.

 

You may be better served by the COMPBL function.

 

WIth all that in mind, here is a way to approach what you asked for:

 

data want;

set have;

blanks=0;

if string > ' ' then do i=1 to length(string) until (blanks=2);

  if substr(string, i, 1)=' ' then blanks + 1;

end;

if blanks=2 then string = substr(string, 1, i-1) || substr(string, i+1);

run;

 

I also tried looking at CALL SCAN, but didn't find an approach that seemed like it would work.

PGStats
Opal | Level 21

Given the string

 

AsBsCssD

 

where s is a space, do you want

 

AsBCssD  (second space of the string removed)

 

or

 

AsBsCsD  (double space replaced by single space) ?

PG
Patrick
Opal | Level 21

What does "2nd space" mean? The 2nd occurence of a space or just don't have consecutive spaces?

 

For "not consecutive spaces" I'd use function "compbl()"

 

 

Ksharp
Super User
data _null_;
x='xxx yyy zzz';
call scan(x,3,p,l,' ');
want=substr(x,1,p-2)||substr(x,p);
put x= want=;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 3876 views
  • 1 like
  • 5 in conversation