BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ndhaubhadel
Calcite | Level 5

I am trying to import a NDJSON file from SAS but I am unable to do so using proc json statement. Is there any other way to read in such file?

Link to NDJSON file I am trying to read in SAS is https://bcda.cms.gov/assets/data/Patient.ndjson

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Rhodochrosite | Level 12

I answered similar question last week.

Try converting the NDJSON into a JSON, then using SAS to import it

filename jsonl url "https://bcda.cms.gov/assets/data/Patient.ndjson";
filename json temp;
filename map temp;

/* Convert the NDJSON to JSON */
data _null_;
	infile jsonl end=eof;
	file json;
	put '[{"records":['; /* Required for JSON formatting */
	do until (eof);
		input;
		line = STRIP(_infile_);
		if (eof=0) then
			line=cats(line, ',');

		put line;
	end;
	put ']}]'; /* Required for JSON formatting */
	stop;
run;
libname json json automap=create map=map;
/* Check resulted Library and data sets */
proc contents data=json._all_; run;

 Hope this helps,

Ahmed

View solution in original post

5 REPLIES 5
AhmedAl_Attar
Rhodochrosite | Level 12

I answered similar question last week.

Try converting the NDJSON into a JSON, then using SAS to import it

filename jsonl url "https://bcda.cms.gov/assets/data/Patient.ndjson";
filename json temp;
filename map temp;

/* Convert the NDJSON to JSON */
data _null_;
	infile jsonl end=eof;
	file json;
	put '[{"records":['; /* Required for JSON formatting */
	do until (eof);
		input;
		line = STRIP(_infile_);
		if (eof=0) then
			line=cats(line, ',');

		put line;
	end;
	put ']}]'; /* Required for JSON formatting */
	stop;
run;
libname json json automap=create map=map;
/* Check resulted Library and data sets */
proc contents data=json._all_; run;

 Hope this helps,

Ahmed

ChrisNZ
Tourmaline | Level 20

@AhmedAl_Attar 

Nice code!

The strip() function does nothing as used though.

Also, if you want you can have a single line

	do until (EOF);
		input;
		LINE = cats( _infile_, ifc(EOF,,',') );
		put LINE;
	end;

 

AhmedAl_Attar
Rhodochrosite | Level 12
Thanks, Will keep it in mind 🙂
ndhaubhadel
Calcite | Level 5
Thank you Ahmed. It works!!!
ChrisNZ
Tourmaline | Level 20

Why proc json? Have you tried this?


filename RAW "c:\temp\raw.json";

proc http method = "get"
          url    = "https://.......json"
          out    = RAW;
run;

libname X json fileref=RAW ;

proc copy in=X out=WORK;
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
  • 5 replies
  • 1483 views
  • 7 likes
  • 3 in conversation