<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I read many json files from Azure Data Lake in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826493#M326465</link>
    <description>&lt;P&gt;You gave me the answer I needed! I am getting the filtering correct (see after line 106 - one file name gets printed "name=process.json").&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I wasn't taking into account that FILENAME and LIBNAME are global. The code below is what I am using and it works. Thanks for the help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;filename d adls "/"
	applicationId=&amp;amp;appId
   accountname=&amp;amp;acctname
   filesystem=&amp;amp;filesys;  

data files;

	folder_id = dopen("d");
	put folder_id=;

	num_files = dnum(folder_id);
	put num_files=;

	do i = 1 to num_files;
		length name $256 ;
		name=dread(folder_id,i);

		if prxmatch('/(process)\.(json)/',name) then do; 
			output;

		end;
	end;

	closerc = dclose(folder_id);
	put closerc=;
run;

%macro readone(name);
filename myjson adls "&amp;amp;name"
  applicationId=&amp;amp;appId
  accountname=&amp;amp;acctName 
  filesystem=&amp;amp;filesys
;

libname myjson json ;
proc append base=all data=myjson.mydata force;
run;
%mend;

data _null_;
  set files;
  call execute(cats('%nrstr(%readone)(',name,')'))
  ;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 01 Aug 2022 18:50:51 GMT</pubDate>
    <dc:creator>stf5018</dc:creator>
    <dc:date>2022-08-01T18:50:51Z</dc:date>
    <item>
      <title>How do I read many json files from Azure Data Lake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826466#M326456</link>
      <description>&lt;P&gt;I am trying to loop over all the files in an Azure Data Lake and want to read specific .json files so that I may do something with the data in those files.&amp;nbsp;I have access to the data lake and proved I can see the files stored there from within SAS. I only want to read files with a specific name (in this example, “process.json”).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I &lt;U&gt;can&lt;/U&gt; access the ADLS location and see the files. I &lt;U&gt;can&lt;/U&gt; filter out the files I don’t want. I &lt;STRONG&gt;cannot&lt;/STRONG&gt; successfully use the “filename” statement to read the files. I am unsure how to get the actual name of the file into the filename statement within the loop. How do I accomplish this? I can't seem to get the filename statement to incorporate the actual value of "name". Code and log output is below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that I have to read json files. I cannot convert to any other file type prior to reading into SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;options azuretenantid = "###-###-###-";

%let appId="###-###-###-";
%let acctName="###";
%let filesys="###";


filename d adls "/"
   applicationId=&amp;amp;appId 
   accountname=&amp;amp;acctName 
   filesystem=&amp;amp;filesys;

data _null_;

	folder_id = dopen("d");
	put folder_id=;

	num_files = dnum(folder_id);
	put num_files=;

	do i = 1 to num_files;
		name=dread(folder_id,i);

		if prxmatch('/(process)\.(json)/',name) then do; 
			put name=; 

			filename myjson adls "&amp;amp;name"
				applicationId=&amp;amp;appId
				accountname=&amp;amp;acctName 
				filesystem=&amp;amp;filesys;
			
			libname temp json fileref=myjson;
			*do something with data in temp;

		end;
	end;

	closerc = dclose(folder_id);
	put closerc=;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Log output:&amp;nbsp;&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;80&lt;BR /&gt;81 folder_id = dopen("d");&lt;BR /&gt;82 put folder_id=;&lt;BR /&gt;83&lt;BR /&gt;84 num_files = dnum(folder_id);&lt;BR /&gt;85 put num_files=;&lt;BR /&gt;86&lt;BR /&gt;87 do i = 1 to num_files;&lt;BR /&gt;88 name=dread(folder_id,i);&lt;BR /&gt;89&lt;BR /&gt;90 if prxmatch('/(process)\.(json)/',name) then do;&lt;BR /&gt;91 put name=;&lt;BR /&gt;92&lt;BR /&gt;93 filename myjson adls "&amp;amp;name"&lt;BR /&gt;WARNING: Apparent symbolic reference NAME not resolved.&lt;BR /&gt;94 applicationId=&amp;amp;appId&lt;BR /&gt;95 accountname=&amp;amp;acctName&lt;BR /&gt;96 filesystem=&amp;amp;filesys;&lt;BR /&gt;97&lt;BR /&gt;98 libname temp json fileref=myjson;&lt;BR /&gt;NOTE: JSON data is only read once. To read the JSON again, reassign the JSON LIBNAME.&lt;BR /&gt;ERROR: The AZURE file &amp;amp;name was not found.&lt;BR /&gt;ERROR: The endpoint within the Credential Service, azureDeviceCode, could not be found&lt;BR /&gt;ERROR: The endpoint within the Credential Service, azureDeviceCode, could not be found&lt;BR /&gt;ERROR: The specified path does not exist.&lt;BR /&gt;ERROR: Error in the LIBNAME statement.&lt;BR /&gt;99 *do something with data in temp;&lt;BR /&gt;100&lt;BR /&gt;101 end;&lt;BR /&gt;102 end;&lt;BR /&gt;103&lt;BR /&gt;104 closerc = dclose(folder_id);&lt;BR /&gt;105 put closerc=;&lt;BR /&gt;106 run;&lt;BR /&gt;folder_id=1&lt;BR /&gt;num_files=6&lt;BR /&gt;name=process.json&lt;BR /&gt;closerc=0&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 1.78 seconds&lt;BR /&gt;cpu time 0.12 seconds&lt;BR /&gt;&lt;BR /&gt;107&lt;BR /&gt;108 %studio_hide_wrapper;&lt;BR /&gt;118&lt;BR /&gt;119&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 16:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826466#M326456</guid>
      <dc:creator>stf5018</dc:creator>
      <dc:date>2022-08-01T16:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I read many json files from Azure Data Lake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826483#M326461</link>
      <description>&lt;P&gt;You appear to have two issues.&lt;/P&gt;
&lt;P&gt;First it does not look like your attempt to use the DREAD() function to get the list of names worked.&amp;nbsp; You have PUT statements in the code, but no names where written to the log.&amp;nbsp; I would recommend skipping the filtering (at least until you know if you can get the names) so you can see what the names retrieved look like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second you have logic issue in the code.&amp;nbsp; You have embedded the global FILENAME and LIBNAME statements into the middle of your DATA step.&amp;nbsp; That will not really work right as the global statements will run while the data step compiler is figuring out how to run the code and before it actually starts running the code that is going to try to retrieve the names.&amp;nbsp; Plus you are referencing a macro variable NAME that you never assigned any value to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I recommend solving the first problem first and see if you can actually get the list of files.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data files;
  folder_id = dopen("d");
  put folder_id=;
  num_files = dnum(folder_id);
  put num_files=;
  do i = 1 to num_files;
    length name $256 ;
    name=dread(folder_id,i);
    output;
  end;
  closerc = dclose(folder_id);
  put closerc=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then figure out whether what you retrieved can be used to generate valid FILENAME and LIBNAME statements.&amp;nbsp; Then figure out if you can actually retrieve data from the JSON file using the libref created by the LIBNAME statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have it working for one file convert it into code you call with different input file names.&amp;nbsp; For example as a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%readone(name);
filename myjson adls "&amp;amp;name"
  applicationId=&amp;amp;appId
  accountname=&amp;amp;acctName 
  filesystem=&amp;amp;filesys
;

libname myjson json ;
proc append base=all data=myjson.mydata force;
run;
%mend readone;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use the list of files to generate calls to the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set files;
  if lowcase(scan(name,-1,'.'))='json' then 
     call execute(cats('%nrstr(%mymacro)(',name,')'))
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Aug 2022 18:14:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826483#M326461</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-01T18:14:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I read many json files from Azure Data Lake</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826493#M326465</link>
      <description>&lt;P&gt;You gave me the answer I needed! I am getting the filtering correct (see after line 106 - one file name gets printed "name=process.json").&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I wasn't taking into account that FILENAME and LIBNAME are global. The code below is what I am using and it works. Thanks for the help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;filename d adls "/"
	applicationId=&amp;amp;appId
   accountname=&amp;amp;acctname
   filesystem=&amp;amp;filesys;  

data files;

	folder_id = dopen("d");
	put folder_id=;

	num_files = dnum(folder_id);
	put num_files=;

	do i = 1 to num_files;
		length name $256 ;
		name=dread(folder_id,i);

		if prxmatch('/(process)\.(json)/',name) then do; 
			output;

		end;
	end;

	closerc = dclose(folder_id);
	put closerc=;
run;

%macro readone(name);
filename myjson adls "&amp;amp;name"
  applicationId=&amp;amp;appId
  accountname=&amp;amp;acctName 
  filesystem=&amp;amp;filesys
;

libname myjson json ;
proc append base=all data=myjson.mydata force;
run;
%mend;

data _null_;
  set files;
  call execute(cats('%nrstr(%readone)(',name,')'))
  ;
run;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2022 18:50:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-read-many-json-files-from-Azure-Data-Lake/m-p/826493#M326465</guid>
      <dc:creator>stf5018</dc:creator>
      <dc:date>2022-08-01T18:50:51Z</dc:date>
    </item>
  </channel>
</rss>

