BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

Folks,

 

I need to edit a raw json file prior to reading it into SAS for analysis.

 

Below is raw data I'm using and code I'm using which I would like to extend.

 

Raw data

 

{"time_stamp":1545314383,"empId":"4215-4-1","unregistered":false}
{"time_stamp":154534383,"empId":"4215-4-1","unregistered":false}
{"time_stamp":15454383,"empId":"425-4-1","unregistered":false}
{"time_stamp":15454383,"emId":"4915-1","unregistered":false}

Code 

data _null_;
  infile json2 end=eof;
  file json3 ;
  input ;
  if _n_=1 then put '[' @; else put ',' @;
  put _infile_;
  if eof then put ']';
run;

How data is changed

[{"time_stamp":1545314383,"empId":"4215-4-1","unregistered":false}
,{"time_stamp":154534383,"empId":"4215-4-1","unregistered":false}
,{"time_stamp":15454383,"empId":"425-4-1","unregistered":false}
,{"time_stamp":15453383,"emId":"4915-1","unregistered":false}
]

 I would like to extend the code to turn the time_stamp variable to character as I'm running into issues when it's read into SAS.

 

So I would like the following

 

{"time_stamp":"1545314383","empId":"4215-4-1","unregistered":false}
{"time_stamp":"154534383","empId":"4215-4-1","unregistered":false}
{"time_stamp":"15454383","empId":"425-4-1","unregistered":false}
{"time_stamp":"154531383","emId":"4915-1","unregistered":false}

Any help is appreciated.

7 REPLIES 7
BillM_SAS
SAS Employee

You do not explain what problem you are having with the time_stamp field data when read into SAS. If you are using the JSON LIBNAME engine note that you can modify the resultant map file and assign a format to the time_stamp field. This may solve your problem.

Sean_OConnor
Fluorite | Level 6

SAS is reading it in in scientific notation 

 

So the following values

 

1545317114383

Comes out as such

 

1.5453171E12
ballardw
Super User

@Sean_OConnor wrote:

SAS is reading it in in scientific notation 

 

So the following values

 

1545317114383

Comes out as such

 

1.5453171E12

That is likely a byproduct of a generic import routine assigning a default format of BEST12. to the values.

Please see the below code:

data _null_;
   x=1545317114383;
   put "Best12. format" x= best12. "Best13. format" x= best13.;
run;

Note the different appearance of the value as shown in the log with different format.

You can change the format after the data is imported to a preferred appearance either manually or with Proc Datasets.

 

TomKari
Onyx | Level 15

Not exhaustively tested:

 

data _null_;
	length InString $32767;
	retain _RX1;
	infile json2 end=eof;
	file json3;
	input;
	InString = _infile_;

	if _n_=1 then do;
		_RX1 = prxparse('s~\{"time_stamp":([[:digit:]]*),"empId":"~{"time_stamp":"$1","empId":"~');
		put '[' @;
	end;
	else put ',' @;
	call prxchange(_RX1, -1, InString);
	put InString;

	if eof then
		put ']';
run;

Tom

Tom
Super User Tom
Super User

No need to change the JSON file. Just change the MAP file that says how to convert it.

If you let SAS generate the map then you will have a definition like this for time_stamp.

       {
          "NAME": "time_stamp",
          "TYPE": "NUMERIC",
          "PATH": "/root/time_stamp"
        },

Which you can just change to this to have it make it as a character string of length 20.

       {
          "NAME": "time_stamp",
          "TYPE": "CHARACTER", "LENGTH": 20,
          "PATH": "/root/time_stamp"
        },
Sean_OConnor
Fluorite | Level 6
Hi Tom,

I'm just wondering is it possible to add labels at this stage too? So I would like to have labels for my variables in my stamp too?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 1717 views
  • 0 likes
  • 5 in conversation