How to write a quote inside a character string?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 131
Accepted Solution

How to write a quote inside a character string?

Hi All:

What's the syntax for an if-then statement where my 'if' variable is character and the value has quotes in it. For example:

The client name is originally Joe "Average" Smith but I want to change it to Joe A Smith...

if last_name="Joe"Average"Smith" then last_name="Joe A Smith";

won't work

because 'the meaning of an identifier after a quoted string might change in a future SAS release.  Inserting white spaced between a quoted string and the succeeding identifier is recommended.

My quotes are balanced...


Accepted Solutions
Solution
‎03-25-2014 10:06 AM
Super Contributor
Posts: 644

Re: How to write a quote inside a character string?

Posted in reply to Maisha_Huq

Either double up the quotes in the centre or alternate single and double

if last_name="Joe""Average""Smith" then last_name="Joe A Smith";

if last_name='Joe"Average"Smith' then last_name="Joe A Smith";


Richard

View solution in original post


All Replies
Solution
‎03-25-2014 10:06 AM
Super Contributor
Posts: 644

Re: How to write a quote inside a character string?

Posted in reply to Maisha_Huq

Either double up the quotes in the centre or alternate single and double

if last_name="Joe""Average""Smith" then last_name="Joe A Smith";

if last_name='Joe"Average"Smith' then last_name="Joe A Smith";


Richard

PROC Star
Posts: 7,485

Re: How to write a quote inside a character string?

Posted in reply to Maisha_Huq

The following approach might be more generalizable:

data have (drop=x y);

  informat last_name x y $50.;

  infile cards dlm='"' truncover;

  input last_name x y;

  if length(strip(x)) gt 1 then last_name=cat(strip(last_name),

    ' ',substr(x,1,1),'. ',strip(y));

  cards;

Joe"Average"Smith

Mary Jones

Tom"Tall"Jones

;

Frequent Contributor
Posts: 131

Re: How to write a quote inside a character string?

Posted in reply to Maisha_Huq

Thanks Richard and Arthur!!

Super Contributor
Posts: 644

Re: How to write a quote inside a character string?

Posted in reply to Maisha_Huq

I am hoping that the examples you gave are made up for the purpose of understanding quoting, and not representative of real data in one of your databases.  If the data is real I believe it would be wrong to create a middle initial based on what appears to be a nickname, and I would hesitate to remove the information inside the quotes entirely.  The best solution would be to create a nickname column and move the data in quotes to the new column.  I can suggest code if required.

Richard

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 237 views
  • 3 likes
  • 3 in conversation