BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

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;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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))
                 ,'}')
      ,"'");

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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))
                 ,'}')
      ,"'");
alepage
Barite | Level 11

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 ?

 

Tom
Super User Tom
Super User

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;
ballardw
Super User

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?

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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