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

Hi Folks,

 

I have a string variable called string and what I need to do is search for certain letters such as S or X and if I find it then I need to convert the string to XXXX.

 

Does anyone have some example code to do this?

All the best,

 

Sean 

data test;
infile datalines delimiter=',';
   input string $ newstring $;
   datalines;
F03  J47  S528 X590,F03  J47  XXXX XXXX
E872  R263 S720 X590,E872  XXXX XXXX XXXX
S720 X590,XXXX XXXX
S150 V789,XXXX V789
;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

If you don't care if the words in the source string are separated by one or multiple blanks and you don't need to maintain this then use Option 1.

If you need to maintain the number of separating blanks then consider Option 2.

data have;
infile datalines delimiter=',' dsd truncover;
   input string :$40. newstring :$40.;
   datalines;
F03  J47  S528 X590,F03  J47  XXXX XXXX
E872  R263 S720 X590,E872  XXXX XXXX XXXX
S720 X590,XXXX XXXX
S150 V789,XXXX V789
;
run;

data want(drop=_:);
  set have;
  /* option 1 */
  length newstring_2 $40 _term $10;
  do _i=1 to countw(string,' ');
    _term=scan(string,_i,' ');
    if findc(_term,'rsx','i') then _term='XXXX';
    newstring_2=catx(' ',newstring_2,_term);
  end;
  /* option 2 */
  length newstring_3 $40.;
  newstring_3=prxchange('s/\b[^ ]*[rsx][^ ]*\b/XXXX/i',-1,strip(newstring));
run;

 Based on your sample data I'm also converting words to XXXX that contain the letter R.

View solution in original post

1 REPLY 1
Patrick
Opal | Level 21

If you don't care if the words in the source string are separated by one or multiple blanks and you don't need to maintain this then use Option 1.

If you need to maintain the number of separating blanks then consider Option 2.

data have;
infile datalines delimiter=',' dsd truncover;
   input string :$40. newstring :$40.;
   datalines;
F03  J47  S528 X590,F03  J47  XXXX XXXX
E872  R263 S720 X590,E872  XXXX XXXX XXXX
S720 X590,XXXX XXXX
S150 V789,XXXX V789
;
run;

data want(drop=_:);
  set have;
  /* option 1 */
  length newstring_2 $40 _term $10;
  do _i=1 to countw(string,' ');
    _term=scan(string,_i,' ');
    if findc(_term,'rsx','i') then _term='XXXX';
    newstring_2=catx(' ',newstring_2,_term);
  end;
  /* option 2 */
  length newstring_3 $40.;
  newstring_3=prxchange('s/\b[^ ]*[rsx][^ ]*\b/XXXX/i',-1,strip(newstring));
run;

 Based on your sample data I'm also converting words to XXXX that contain the letter R.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1 reply
  • 295 views
  • 0 likes
  • 2 in conversation