BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
arthurdpereira
Obsidian | Level 7

SAS can directly access XML files hosted at a remote URL when using the LIBNAME statement.

%let url = 'https://example.com/files/myfile.xml';
libname libxml xmlv2 &url xmlmap='https://example.com/files/myfile.map';

The problem arises when we try to use the URI because I will later need to upload this file in a JobExecution form, where it generates the file's URI.

%let uri = 'https://sas.example.com/files/files/cdb78937-1273-4d35-af89-5e5f00aac774/content'; /* File uploaded to SASDrive and URI copied */
libname libxml xmlv2 &uri xmlmap='https://example.com/files/myfile.map';

In the case above, an error occurs. I tried to debug it in my way, and it returned the following:

%let uri = 'https://sas.example.com/files/files/cdb78937-1273-4d35-af89-5e5f00aac774/content';

data _null_;
if fileexist("&uri") then
put "The file exists.";
else
put "The file was not found.";
run;

libname libxml xmlv2 &uri. xmlmap='https://example.com/files/myfile.map';

Result: The file was not found.

 

Could someone help me with this?

1 ACCEPTED SOLUTION

Accepted Solutions
arthurdpereira
Obsidian | Level 7

The flow is as follows. I have a client that receives several XML files with data. He wants to see this in a macro way, I have a panel at the VA. So I proposed that he send the XML through Job Execution, and the program inserts the data into a table in SAS. The problem is that I only know the way to get the file via JE with URI and, that way, it doesn't work. That's the problem.

 

But I found a solution: parenturi

 

 

filename upload filesrvc parenturi="&SYS_JES_JOB_URI" 
  name="&_WEBIN_FILENAME" 
  contenttype="&_WEBIN_CONTENT_TYPE";

libname xml xmlv2 xmlfileref=upload xmlmap='file.map';

With this code I managed to make it recognize it as an XML file

 

View solution in original post

7 REPLIES 7
SASJedi
SAS Super FREQ

I'd use PROC HTTP to download a (temporary) copy of the files. Something like this should work:

%let url = https://example.com/files/myfile.xml;
filename libxml temp;
proc http url="&url" out=libxml;
run;

%let url = https://example.com/files/myfile.xml;
filename xmlmp temp;
proc http url="&url" out=xmlmp;
run;

libname libxml xmlv2 xmlmap=xmlmp;
Check out my Jedi SAS Tricks for SAS Users
arthurdpereira
Obsidian | Level 7

Yes, it can be done through PROC HTTP, but the problem is downloading via the URI.

 

In the case of PROC HTTP, I would have to include other SAS parameters like token or webusername, but that’s not the case here.

 

I need to access the XML file through LIBNAME XMLV2 via the URI.

SASJedi
SAS Super FREQ

As stated before, in order to use the XMLV2 LIBNAME engine, you will need a local copy of the file to work with. If you can't use PROC HTTP to download the copy, you can use FILENAME URL and a DATA _NULL_ step to make it.  For example:

/* Set up FILENAME URL for remote file */
filename getme url "http://mydomain.net/my_xml_data.xml";
/* Set up FILENAME TEMP for local copy */
filename local temp;

/* Read remote file and write the result to the local copy */
data _null_;
	infile getme;
	file local;
	input;
	put _infile_;
run;

/* Release connection to remote file */
filename getme clear;

/* Use XMLV2 LIBNAME engine to read the local copy */
libname local xmlv2;

/* Extract the data from the XML file however you wish */
proc copy in=local out=work;
run;

/* Clear the XMLV2 libref */
libname local clear;
/* Clear the local XML fileref. This also deletes the local copy of the XML file.*/
filename local clear;

 

Check out my Jedi SAS Tricks for SAS Users
arthurdpereira
Obsidian | Level 7

Thanks for your answer! The problem is that I am using Job Execution in SAS Viya to send the XML file and in this context I cannot use the HTTP PROC directly or the suggestion you made since the system creates a URI. So since the process is executed inside the Job Execution I need to work with the URI of the XML file and that is where my difficulty lies.

ballardw
Super User

@arthurdpereira wrote:

Thanks for your answer! The problem is that I am using Job Execution in SAS Viya to send the XML file and in this context I cannot use the HTTP PROC directly or the suggestion you made since the system creates a URI. So since the process is executed inside the Job Execution I need to work with the URI of the XML file and that is where my difficulty lies.


This bit may mean that it is time to examine the bigger picture.

On the surface this sounds like: I have data in SAS. I am tranforming to XML file. So SAS can use it. But SAS isn't reading it the way I want.

 

Which looks kind of odd to me. Maybe I am missing something about the reason  XML is involved at all.

arthurdpereira
Obsidian | Level 7

The flow is as follows. I have a client that receives several XML files with data. He wants to see this in a macro way, I have a panel at the VA. So I proposed that he send the XML through Job Execution, and the program inserts the data into a table in SAS. The problem is that I only know the way to get the file via JE with URI and, that way, it doesn't work. That's the problem.

 

But I found a solution: parenturi

 

 

filename upload filesrvc parenturi="&SYS_JES_JOB_URI" 
  name="&_WEBIN_FILENAME" 
  contenttype="&_WEBIN_CONTENT_TYPE";

libname xml xmlv2 xmlfileref=upload xmlmap='file.map';

With this code I managed to make it recognize it as an XML file

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 1141 views
  • 4 likes
  • 3 in conversation