Lovnlust
Lovnlust

Reputation: 1537

How to check if a string contains apostrophe

I don't remember how SAS deal with these special characters. Any built-in functions?

E.g

a = New Year's Day, should I use something like index(a, 'New Year's Day') > 0?

Upvotes: 1

Views: 8909

Answers (3)

mjsqu
mjsqu

Reputation: 5452

The key to this question is the masking of the apostrophe in quotes. If you wish to look for an occurrence of a single apostrophe, you can mask it with double apostrophes:

Looking for single apostrophes

data _NULL_;
  a="New Year's Day";
  b=index(a,"'");
  put b=;
run;

The single apostrophe is passed as a second argument to the index function, using double quotes.

Looking for double quotes

data _NULL_;
  a='They said, "Happy New Year!"';
  b=index(a,'"');
  put b=;
run;

This time around, the double quote is set inside single quotes when passed to the index function

Upvotes: 6

Joe
Joe

Reputation: 63424

mjsqu and NeoMental covered the basic case well, but in the special case where you do not have the option of using " (for example, you need to prevent macro variable resolution), you can double the apostrophe:

data _null_;
  a='MerryXmas&HappyNewYear''s'; *here need single quotes or a macro quoting function;
  b=find(a,"'"); *here do not need to mask ampersand resolution;
run;

Of course you could also use %nrstr to avoid resolution, but there are real life cases where this is occasionally needed. This works with "" similarly (two "" become one character ").

Upvotes: 3

in_user
in_user

Reputation: 1958

Use "find" command like below to find out what are you looking for is there in the string or not. If the returned value is greater than > 0 then apostrophe or whatever you are looking for is there, otherwise not.

  • Teststring - where you want to look

  • Next to Teststring is "'" - In quotes what are you looking for, in your case apostrophe


data _null_;
TestString="New year's day";
IsItThere=find(TestString,"'");
put IsItThere=;
run;

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002267763.htm

Upvotes: 1

Related Questions