BookmarkSubscribeRSS Feed
sashamal
Fluorite | Level 6

I got error when running output to json using "proc json".

The program and error message you can see bellow.

 

1. I have to use "fmtnumeric" options for my needs. This is not an option for me.

2. When I use "format percentvar percent10.2" instead of percent7.2 it is working, but  maximum length of numeric variable is unknown. This is also not an option for me.

 

Program:

 

data json_sample1;
   percentvar=0.543;
   format percentvar percent7.2;
run;

 

proc json out="/tmp/r1.json" nosastags pretty;

  write open object;

    write values data;
       write open array;

           export json_sample1 / fmtnumeric;

       write close;

   write close;

run;

 

ERROR: Due to the previous error, the JSON output file is incomplete and invalid, or in some cases,
unable to be created. If created, the JSON output file is retained so that you can review it
to help determine the cause of the error.

7 REPLIES 7
AMSAS
SAS Super FREQ

PERCENTw.d Format (see Syntax Description section)

I tested this and it looks like you need to specify the width of the output field to account for the percent sign % and parentheses () for numbers, whether the number is negative or positive.

Your example number 0.543, is would be formatted "(54.30%)" and needs a minimum of 8 characters. 

 

You could convert the number (percentvar) into a string and output that:

strPct=putn(percentvar,"percent7.2") ;

Then your JSON file would look like this:

 

{
  "data": [
    {
      "strPct": "(54.3%)"
    }
  ]
}

 

 

 

sashamal
Fluorite | Level 6

Hello AMSAS. Thank your reply, but as was mentioned I can't use other formats or covert numeric data to character. I use JSON output in automated server to server communication and I have no possibility for any manual changes. The same bug in Proc JSON appears when using commad.w formats and possible other numeric SAS formats. May be SAS can fix this in next release? In such case, no need to stop generation of json and send message of fatal error, just output data in regular SAS form, like in all others procedures or dataset steps.

Tom
Super User Tom
Super User

The problem is triggered by the fact that the width on the format is not long enough to include 2 decimal places for that value.  Not sure why it triggers an in PROC JSON in not other places.

 

 See the difference in output between percent7.2 and percent8.2:

657   data json_sample1;
658      percentvar=0.543;
659      format percentvar percent8.2;
660      put '----5----0'/ percentvar percent7.2 / percentvar percent8.2 ;
661   run;

----5----0
 54.3%
 54.30%

 

Perhaps you can add some pre output step to confirm that the formats attached to the variables being written do not also have the same issue?

 

Perhaps you can use a user defined format to print the data?

proc format ;
picture xx  (round ) 
  other = '009.99%' (mult=100)
;
run;

data json_sample1;
   percentvar=0.543;
   format percentvar xx.;
run;
sashamal
Fluorite | Level 6

"Perhaps you can add some pre output step to confirm that the formats attached to the variables being written do not also have the same issue?"

 

I can add pre-check, but I have no ideas how can I validate such formats.

ballardw
Super User

Since the single error message you post includes: "ERROR: Due to the previous error," I wonder if there actually was a previous error message and if so how it appears.

sashamal
Fluorite | Level 6

No any previous errors here.

 

You can execute bellow code to see this error:

 

data json_sample1;
   percentvar=0.543;
   format percentvar percent7.2;
run;

 

proc json out="/tmp/r1.json" nosastags pretty;

  write open object;

    write values data;
       write open array;

           export json_sample1 / fmtnumeric;

       write close;

   write close;

run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1080 views
  • 1 like
  • 4 in conversation