Dear,
In my data some values contain "!!" characters in the middle of values. I need to remove the characters and the values after the characters. Thank you
data
term
Subject did not drink 50% of the meal!! Chicken
output needed;
Subject did not drink 50% of the meal
Dear,
In my data some values contain "!!" characters in the middle of values. I need to remove the characters and the values after the characters. Thank you
data
term
Subject did not drink 50% of the meal!! Chicken
output needed;
Subject did not drink 50% of the meal
I'd try the tranwrd() function, and then scan():
data have;
infile cards truncover;
input term $100.;
cards;
Subject did not drink 50% of the meal!! Chicken
;
run;
data want;
set have;
term = tranwrd(term,"!!","|");
term = scan(term,1,"|");
run;
Since one can only use single characters as delimiters in scan().
Try this
data test;
string='Subject did not drink 50% of the meal!! Chicken';
newstring = substr( string,1,index(string,'!!') - 1);
run;
If you just want everything before the first exclamation mark then the scan() function on its own should do.
Not sure why @Kurt_Bremser believes that duplicates need replacement for your use case.
data have;
infile cards truncover;
input term $100.;
cards;
Subject did not drink 50% of the meal!! Chicken
;
run;
data want;
set have;
term = scan(term,1,"!");
run;
@Patrick wrote:
.....
Not sure why @Kurt_Bremser believes that duplicates need replacement for your use case.
Well, if you have a single exclamation mark in the text before the doubles ...
But didn't the OP tell us that he just wants "everything" from the beginning up to the first exclamation mark? So what do we care about any futher exclamation marks in the string. Based on your code I must be missing something.
Look at the log from this:
data _null_;
term = 'Subject complains! Subject did not drink 50% of the meal!! Chicken';
term1 = tranwrd(term,"!!","|");
term1 = scan(term,1,"|");
term2 = scan(term,1,"!");
put term1=;
put term2=;
run;
The OP stated that the delimiter is a sequence of two exclamation marks.
Granted that I may be a little overcautious here, but better safe than sorry.
Thanks. I did miss that the delimiter is TWO pipes. That explains what your'e doing.
Here a variant using RegEx
data have;
term = 'Subject complains!! Subject did not drink 50% of the meal!! Chicken';
output;
term = 'Subject complains! Subject did not drink 50% of the meal!! Chicken';
output;
term = 'something';
output;
run;
data want;
set have;
if 0 then want_str=term;
want_str=prxchange('s/(!{2}.*)//oi',1,term);
run;
Scan function.
Sounds like a job for the compress function - which removes given characters:
data have; infile cards truncover; input term $100.; term2=compress(term,"!"); cards; Subject did not drink 50% of the meal!! Chicken ; run;
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!
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.