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

After putting $hex48. in format statement I can see the line breaks 0A.. It means line breaks are in the dataset but when it prints it does not display with the line breaks..so it seems any option in proc print solve this problem?

 

proc print data=df noobs ;
var content / style = [fontsize=14pt];
format content $hex48.;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you want ODS to print a line break try changing the linefeeds into ODS newline commands.  https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/odsug/p11xia2ltavr8ln17srq8vn4rnqc.htm#p0xwm6...

string=tranwrd(string,'0a'x,'^{newline}');

Or you could split the strings into multiple observations and print those. 

data for_print;
  set have;
  do row=1 to max(1,countw(string,'0A'x);
     newstring=scan(string,row,'0A'x);
     output;
  end;
  drop string;
run;

 

You might want to include the ASIS option to preserve the leading spaces.

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

The text you shared does not appear to be a fully formed JSON file.

 

8427   libname json json ;
NOTE: JSON data is only read once.  To read the JSON again, reassign the JSON LIBNAME.
ERROR: Invalid JSON in input near line 1 column 4: Text must begin with an object or array open.
ERROR: Error in the LIBNAME statement.
8428
deepanshu88us0
Fluorite | Level 6
JSON
Tom
Super User Tom
Super User

It seems to complaining about the random line breaks you have inserted into the middle of that one value.

So you can remove them and the file will work:

Let's re-create your posted text as a file, let's use JSONRAW as the fileref

options parmcards=jsonraw;
filename jsonraw temp ;
parmcards4;
{
   "id": "c",
   "object": "ch",
   "created": 1678202926,
   "model": "g",
   "usage": {
     "prompt_tokens": 12,
     "completion_tokens": 192,
     "total_tokens": 204
   },
   "choices": [
     {
       "message": {
         "role": "assistant",
         "content": "\n\nHere's an example Python code to remove duplicates from a list:\n\n```python\nmy_list = [1, 2, 3, 1, 2, 4, 
 5, 5]\n\n# Use a set to remove duplicates\nunique_list = list(set(my_list))\n\nprint(unique_list)\n```\n\nOutput:\n\n```\n[1, 2, 3, 
 4, 5]\n```\n\nAlternatively, you can also use a for loop to iterate through the list and create a new list with only unique 
 elements:\n\n```python\nmy_list = [1, 2, 3, 1, 2, 4, 5, 5]\n\nunique_list = []\nfor element in my_list:\n    if element not in 
 unique_list:\n        unique_list.append(element)\n\nprint(unique_list)\n```\n\nOutput:\n\n```\n[1, 2, 3, 4, 5]\n```"
       },
       "finish_reason": null,
       "index": 0
     }
   ]
 }
;;;;

Now if you just copy it to file without the line breaks it will work:

filename json temp;
data _null_;
  infile jsonraw;
  file json recfm=n;
  input;
  put _infile_ @;
run;
libname json json ;

proc copy inlib=json outlib=work;
run;

If you want the line breaks to actually be part of the value you need to insert \n , like the ones it already has.

filename json temp;
data _null_;
  infile jsonraw;
  file json recfm=n;
  input;
  retain nq;
  nq=mod(sum(nq,countc(_infile_,'"')),2);
  put _infile_ @;
  if nq then put '\n' @;
run;
libname json json ;

 

Tom
Super User Tom
Super User

What does this question have to do with PROC PRINT?

deepanshu88us0
Fluorite | Level 6

I apologize I couldn't explain in clearer manner.. Let me explain what I am doing-

 

My goal : To print the output in SAS like in the image. This is what I am trying to do with PROC PRINT.  When I used hexcode in the format statement I can see line break in the dataset (see my original post - I updated the post).. Hope it makes sense.

 

Looks like it's nothing to do with JSON.. It's to do with PROC PRINT if an observation has text which contains line breaks so when user prints it.. it does not show in results window...But we can see line breaks in hex code.

Tom
Super User Tom
Super User

If you want ODS to print a line break try changing the linefeeds into ODS newline commands.  https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/odsug/p11xia2ltavr8ln17srq8vn4rnqc.htm#p0xwm6...

string=tranwrd(string,'0a'x,'^{newline}');

Or you could split the strings into multiple observations and print those. 

data for_print;
  set have;
  do row=1 to max(1,countw(string,'0A'x);
     newstring=scan(string,row,'0A'x);
     output;
  end;
  drop string;
run;

 

You might want to include the ASIS option to preserve the leading spaces.

deepanshu88us0
Fluorite | Level 6

Thanks a ton, works like a charm.. ASIS seems to be not available in PROC PRINT.

 

proc print noobs;  
var newstring;
run;

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 993 views
  • 2 likes
  • 2 in conversation