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

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-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 548 views
  • 0 likes
  • 2 in conversation