BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
CR-Horton
Fluorite | Level 6

Hello,

I am using a data _null_ statement to create a JSON output file that I then reference using proc http. 

I have ran into an issue with the JSON output containing line breaks for one of my put statements. This is due to the fact that the variable I am referencing in the specific put statement  has HTML code.

%macro update;
%do i = 2 %to 3;
data _null_;
file "XXXXX_&i..json";
  set _final_b&i end=eof;
  if _n_=1 then put '[' / ' {' @;
  else put ',{' @;
  put 
    '"id":' id
  /' ,"type":' type :$quote.
  /' ,"map_price":' map_price
  /' ,"fixed_cost_shipping_price":' fixed_cost_shipping_price
  /' ,"is_free_shipping":' is_free_shipping_alt
  /' ,"availability_description":' availability_description :$quote.
  /' ,"page_title":' page_title :$quote.
  /' ,"meta_description":' meta_description :$quote.
  /' ,"description":' description :$quote. (this field has the HTML that is getting output with multiple line breaks)
  /' ,"name":' name :$quote.
  /' ,"price":' price
  /' ,"weight":' weight
  /' }'
  ;
  ;
  if eof then put ']';

This issue is with respect to the description variable. Here is an example of what that variable looks like:

<h3>Description</h3><ul><li>Hello this is the product description</li><li>Here is some further information</li><li>

 

Is there a way I can suppress the line breaks when outputting a JSON file? Many of my descriptions have lengths>1000 and numerous HTML code creating the line breaks.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

If you have CR LF control characters in the data, you should be able to remove them with:

description= compress(description,'0D0A'x) ;

There are other options for the optional third argument to compress that will allow you to remove all control characters.  

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.

View solution in original post

8 REPLIES 8
Quentin
Super User

I'm confused, if the value of description is "<h3>Description</h3><ul><li>Hello this is the product description</li><li>Here is some further information</li><li>" then that value doesn't have any line breaks in it.  Some of the values are HTML tags that will create an unordered list.

 

What value would you want to write to the JSON file?  Would you want to remove the tags <ul><li></li> so you would have description="<h3>Description</h3>Hello this is the product descriptionHere is some further information" ?

 

You might want to look into PROC JSON for generating JSON files, rather than writing the file with PUT statements.

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
CR-Horton
Fluorite | Level 6

No I do not want to remove the tags I am wanting everything printed in one single like. This is what the output looks like currently:

[
{"id":31790
,"type":"physical"
,"map_price":0
,"fixed_cost_shipping_price":10
,"is_free_shipping":true
,"availability_description":""
,"page_title":"XXXX"
,"meta_description":"XXXX"
,"description":"<h3>Description</h3><ul><li>Hello this is the product description
</li><li>Here is some further information
</li><li>"
,"name":"XXX"
,"price":18.18
,"weight":0.18
}
]

 

Want: "description":"<h3>Description</h3><ul><li>Hello this is the product description</li><li>Here is some further information</li><li>"

Quentin
Super User

If you have CR LF control characters in the data, you should be able to remove them with:

description= compress(description,'0D0A'x) ;

There are other options for the optional third argument to compress that will allow you to remove all control characters.  

The Boston Area SAS Users Group is hosting free webinars!
Next webinar will be in January 2025. Until then, check out our archives: https://www.basug.org/videos. And be sure to subscribe to our our email list.
CR-Horton
Fluorite | Level 6

Thanks, but I am actually not wanting to remove those characters. Those are HTML characters that I need to remain in the string.

Using that compress function strips out all the content post the control characters and in my instance since I have strings that are 2k characters and it ends up removing 90% of the string.

Tom
Super User Tom
Super User

You have a string with 1,800 thousand linefeed or carriage return characters?

Something is either wrong with the source data or the program you used to to implement the COMPRESS() function call.

CR-Horton
Fluorite | Level 6

Yes, my mistake - sorry for the confusion. That compress statement worked for me.

Tom
Super User Tom
Super User

Your data step is NOT going to add any end of line characters to the output file UNLESS the logical recode length for the output file is shorter than the length of the string you are trying to write.

 

Are you sure that the data in the variable DESCRIPTION does not contain LF (or perhaps CR LF pairs)?

If so you should remove those first.

Perhaps with TRANSLATE() or TRANWRD() function calls.

description=translate(description,' ','0a'x);

or TRANWRD() function calls.

description=tranwrd(description,'0d0a'x,' ');
Ksharp
Super User
/*Try recfm= option.*/

data _null_;
file "XXXXX_&i..json" recfm=n ;

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 984 views
  • 0 likes
  • 4 in conversation