Hello,
I am using a cats function to generate one json record (see the code below). But the cats function failed when there is an apostrophe either in the first name or last Name.
How do we solve that issue ?
Ex last Name = D'agostina
Data ConsentInfo&j.;
length record $4000;
set info_1;
i=_n_;
record=cats("'", '{ "firstName" : ',' "',firstName,'", ',' "lastName" : ','"',lastName,'", ','"email" :',
'"',email,'"}',"'");
call symputx(cats('NewRec',i),record,'g');
run;
Don't try to add the quotes yourself. Use the QUOTE() function to properly add the quotes around a string. That will properly double up any quotes that are in the string.
Use the optional second argument when you want it to use single quotes instead of the default double quotes.
record=quote(cats('{ "firstName":',quote(trim(firstname))
,',"lastName":',quote(trim(lastName))
,',"email" :',quote(trim(email))
,'}')
,"'");
Don't try to add the quotes yourself. Use the QUOTE() function to properly add the quotes around a string. That will properly double up any quotes that are in the string.
Use the optional second argument when you want it to use single quotes instead of the default double quotes.
record=quote(cats('{ "firstName":',quote(trim(firstname))
,',"lastName":',quote(trim(lastName))
,',"email" :',quote(trim(email))
,'}')
,"'");
By doing that, for example instead of seeing D'agostina, we will have D ' ' Agostina and the cats function will take it. However at the end, when this lastName will be sent to an API Web Site, the lastName will look like as D ' ' Agostina instead of D'agostina.
Is there another work around solution ?
You will need to show the full code to see whether or not that is true.
In normal SAS code that is NOT what will happen. Try it.
data test;
name="D'Agostina";
string1=cats('{"name":',quote(trim(name)),'}');
qstring=quote(trim(string1),"'");
call symputx('mvar',qstring);
run;
data test2 ;
set test;
string2=&mvar;
put (_all_) (=/);
run;
If you are doing this to create JSON output why are you sticking values into a macro variable?
And did you try the Libname JSON engine to create JSON files?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.