SAS Programming

DATA Step, Macro, Functions and more
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
Ammonite | Level 13

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
Ammonite | Level 13

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
Ammonite | Level 13
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 5 replies
  • 2631 views
  • 7 likes
  • 3 in conversation