Ad

SAS Remove The Last Characters If Special

- 1 answer

I have a sas dataset with the variable called response, which has the following records:

enter image description here

and so on.

These are all the same records, I need to remove the last character wherever special and return the records as

enter image description here

When I use a compress function, it removes the asterisk in between and returns: TrailerOffer which is not what I want. Can somebody please help me code this? I need to remove the last characters if these are special.

Ad

Answer

You can use regular expression character classes to specify the 'special' trailing characters. In this example replacement pattern, any characters that are not letters or numbers will be removed. \s* is needed before $ because SAS character variables will have trailing spaces when their values are passed to the regex engine.

Learn more about regular expression class groupings in the SAS documentation

data have;
length response $20.; input response; datalines;
Trailer*Offer
Trailer*Offer*
Trailer*Offer???
Trailer*Offer?...
Offer#1
Offer#1?
Offer#1*?
;
data want;
  set have;
  response = prxchange ('s/[^a-z0-9]+\s*$//i', 1, response);
run;

enter image description here

Ad
source: stackoverflow.com
Ad