Vesnič
Vesnič

Reputation: 375

SAS: find quotation mark inside string

I have quotation marks like " inside strings in various unknown places. I want to find/remove them.

I'm using tranwrd() function for other chars that I want to remove (like +,-& etc.).

Does anyone know how to do that?

Upvotes: 0

Views: 2498

Answers (2)

Joe
Joe

Reputation: 63424

If you're asking how you put a quote character inside a literal string, you have two options.

First, SAS allows both ' and " to be used. You can put one in the other.

data want;
  x = '"Dear me", she said.';
  y = compress(x,'"');
  put x= y=;
run;

Second, more useful if you either have macro variables or both kind of quotes, doubling the quote character escapes it.

data want;
  x = '"Dear me, please don''t do that", she said.';
  y = compress(x,'"''');
  put x= y=;
run;

Note in both places in the second example I put two ' characters (the x assignment and the y compress).

Compress and tranwrd would both work the same way here (if you have a reason that tranwrd is superior for your use), as would any other SAS function or language element.

Second - for several functions, including compress but not tranwrd or translate - there is an optional parameter that lets you add all characters of a certain type. Compress for some reason doesn't support q like some do, but it does support p for punctuation. This may be desired, or may not.

data want;
  x = '"Dear me, please don''t do that", she said.';
  y = compress(x,,'p');
  put x= y=;
run;

Or, if you have a list of characters you are okay with, k lets you keep them:

data want;
  x = '"Dear me, please don''t do that", she said.';
  y = compress(x,'.,','kas');
  put x= y=;
run;

That keeps period, comma, a-z characters, and space characters, deletes all others.

Another interesting point: dequote() will remove pairs of quotes, if you have them - ie, if you're just trying to remove quotes that are surrounding your character variables or something like that.

Upvotes: 1

Robert Penridge
Robert Penridge

Reputation: 8513

Use the compress() function. First parameter is the string to modify, second parameter is the list of characters to remove. I've put that list of characters in single quotes so that the % and & characters would not be picked up by the macro processor.

data want;
  tmp = '+blah -&blah'' "blah';
  want = compress(tmp,'+-&''"%');
  put tmp= want=;
run;

Alternatively the compress() function provides some nice ways to keep just certain characters. In the below example I've told it to keep characters using the k modifier in the third argument. The characters it will keep are the space character (which I've listed in the second argument) and digits, the underscore character, and English letters (specified by the n modifier in the third argument).

data want;
  tmp = '+blah -&blah'' "blah';
  want = compress(tmp,' ','nk');
  put tmp= want=;
run;

Upvotes: 2

Related Questions