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...
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
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
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
;
Thanks Richard and Arthur!!
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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.