BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I have the following scenario:

I have to find if the string "text" has more than 4 spaces then I want to replace the fourth "space" with the string "[ESC]". Otherwise, we don't insert any strings.  If replacing is complicated, I am okay with inserting a string after the fourth space. Thank you for your help.

Example: "I am happy to[ESC]be part of this group"

 data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;

 run;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data have1;
    set have;
    fourth_word=scan(text,4,' ');
    location_of_fourth_word=findw(text,cats(' ',fourth_word));
    length_of_fourth_word=length(fourth_word);
    if not missing(fourth_word) then 
        replace_text=cats(substr(text,1,location_of_fourth_word+length_of_fourth_word-1),'[ESC]',
        substr(text,location_of_fourth_word+length_of_Fourth_word));
    else replace_text=text;
run;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26
data have1;
    set have;
    fourth_word=scan(text,4,' ');
    location_of_fourth_word=findw(text,cats(' ',fourth_word));
    length_of_fourth_word=length(fourth_word);
    if not missing(fourth_word) then 
        replace_text=cats(substr(text,1,location_of_fourth_word+length_of_fourth_word-1),'[ESC]',
        substr(text,location_of_fourth_word+length_of_Fourth_word));
    else replace_text=text;
run;
--
Paige Miller
Quentin
Super User

Can you show the code you have tried? 

 

This blog from leonid Batkhan might help:

https://blogs.sas.com/content/sgf/2019/06/26/finding-n-th-instance-of-a-substring-within-a-string/

 

Another approach would be to use the CALL SCAN routine, which can return the position of the 5th word.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0ecxfx00bn8i4n1vhh8up24ha6x.h...

 

 

The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.
Ksharp
Super User
 data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;
 run;

 data want;
  set have;
  want=prxchange('s/(^\S+\s+\S+\s+\S+\s+\S+)\s+/\1[ESC]/',1,left(text));
run;
SASuserlot
Barite | Level 11

Thank you  for your time @Ksharp . That's awesome which can be done in one step 😮. what's the 'S+' and 's+' signifies in the prxchange ?

 

Thank you @PaigeMiller  @ErikLund_Jensen  @mkeintz @Ksharp . All of you guys are amazing.  unfortunately I have to chose one as Answer😑.

Ksharp
Super User
'S+' means one or more non-space characters .
and 's+' mean one or more space characters .
ErikLund_Jensen
Rhodochrosite | Level 12

Hi @SASuserlot 

 

This is using a prxchange similar to the one provided by @Ksharp , but with an added check on the number of words  in the string. This is to avoid insert of {ESC] at the end of the string, if there are exactly 4 words.

data have;
  text = "I am happy to be part of this group";
  output;
  text = "This SAS Group is      Amazing";
  output;
  text = "Thank you very much";
  output;
  text = "Thanks for everything";
  output;
  text = " Thank you";
  output;
run;

data want;
  length text $200;
  set have; 
  if countw(text,' ') > 4 then
    text = prxchange('s/(\S+\s+\S+\s+\S+\s+\S+)(\s*)(.*)/$1[ESC]$3/',1,catt(text));
run;
mkeintz
PROC Star
data have;
  text = " I am happy  to  be part of this group";
  output;
  text = "This SAS Group is Amazing";
  output;
  text = " Thank you";
  output;
 run;

data want (drop=_:);
  set have;
  text=left(compbl(text));
  length newtext $40;
  if countw(text) > 4 then do;
    _four_word_length=length(catx(' ',scan(text,1),scan(text,2),scan(text,3),scan(text,4)))+1;
    newtext=cats(substr(text,1,_four_word_length),'[ESC]',substr(text,_four_word_length+1));
  end;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 1572 views
  • 5 likes
  • 6 in conversation