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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.