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?

SAS Innovate 2025: Register Now

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!

SAS Enterprise Guide vs. SAS Studio

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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