Update a XML file through SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 17
Accepted Solution

Update a XML file through SAS

Hi, I have a XML file in the local machine which look like:

 

<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmlns.oBDracSle.com/pcbpel/adapter/" xmlns:soapenv="http://scheGmas.xmlsFoap.org">


-<soapenv:Header>


-<oSmiley Frustratedecurity o:mustUnderstand="1" xmlnsSmiley Surprised="http://docs.oGDasis-open.org/1.0.xsd">


-<o:UsernameToken>

<o:Username>WED</o:Username>

<oSmiley Tongueassword Type="http://docs.oGDasis-open.org-userofile-1.0#PasswordText">6d24aH:</oSmiley Tongueassword>

</o:UsernameToken>

</oSmiley Frustratedecurity>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<getSmiley Tongue_OFFSET>0</getSmiley Tongue_OFFSET>

<!--Optional:-->


<getSmiley Tongue_LIMIT>100</getSmiley Tongue_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>

 

 

 

I just need to update the value of <P_Offset> and <P_Limit> and keep the rest of the xml as it is in the same format.

 

I have tried importing the xml to SAS and changing the <P_Offset> and <P_Limit>. This way works but the format of the xml file changes.

 

Thanks!!


Accepted Solutions
Solution
‎04-23-2018 02:19 AM
Super User
Posts: 10,283

Re: Update a XML file through SAS

Posted in reply to katariasarthak

@katariasarthak wrote:
Can you send a sample code to do so? Thanks!
data _null_;
input;
if index(_infile_,"P_OFFSET>") > 0 then do;
  begin = indexc(_infile_,'>');
  end = index(_infile_,'</');
  _infile_ = substr(_infile_,1,begin) !! '1000' !! substr(_infile_,end);
end;
put _infile_;
datalines4;
<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmls.sas.com/actions" xmls:soapenv="http://smas.xml.org/">


-<soapenv:Header>


-<o:Security o:mustUnderstand="1" xmlns:o="http://doc.ois-open.org.xsd">


-<o:UsernameToken>

<o:Username>WKKJD</o:Username>

<o:Password Type="http://doc.ois-open.org/ws200401-wss-username-token-6.0#PasswordText">6dhgmfe6d20:</o:Password>

</o:UsernameToken>

</o:Security>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<get:P_OFFSET>0</get:P_OFFSET>

<!--Optional:-->


<get:P_LIMIT>100</get:P_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>
;;;;
run;

Part of the log:

<get:P_OFFSET>1000</get:P_OFFSET>                                               

Add more conditional blocks as needed, and for production, add file and infile statements, and remove the datalines block.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 10,283

Re: Update a XML file through SAS

[ Edited ]
Posted in reply to katariasarthak
  1. post textual data like code, using the {i}, button, or you'll have blanks deleted, smileys inserted, and other funny things
  2. post your code, and define what you mean by "the format changes"

@katariasarthak wrote:

Hi, I have a XML file in the local machine which look like:

 

<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmlns.oBDracSle.com/pcbpel/adapter/" xmlns:soapenv="http://scheGmas.xmlsFoap.org">


-<soapenv:Header>


-<oSmiley Frustratedecurity o:mustUnderstand="1" xmlnsSmiley Surprised="http://docs.oGDasis-open.org/1.0.xsd">


-<o:UsernameToken>

<o:Username>WED</o:Username>

<oSmiley Tongueassword Type="http://docs.oGDasis-open.org-userofile-1.0#PasswordText">6d24aH:</oSmiley Tongueassword>

</o:UsernameToken>

</oSmiley Frustratedecurity>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<getSmiley Tongue_OFFSET>0</getSmiley Tongue_OFFSET>

<!--Optional:-->


<getSmiley Tongue_LIMIT>100</getSmiley Tongue_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>

 

 

 

I just need to update the value of <P_Offset> and <P_Limit> and keep the rest of the xml as it is in the same format.

 

I have tried importing the xml to SAS and changing the <P_Offset> and <P_Limit>. This way works but the format of the xml file changes.

 

Thanks!!


 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

Posted in reply to KurtBremser

This is the original xml file: 

 

<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmls.sas.com/actions" xmls:soapenv="http://smas.xml.org/">


-<soapenv:Header>


-<o:Security o:mustUnderstand="1" xmlns:o="http://doc.ois-open.org.xsd">


-<o:UsernameToken>

<o:Username>WKKJD</o:Username>

<o:Password Type="http://doc.ois-open.org/ws200401-wss-username-token-6.0#PasswordText">6dhgmfe6d20:</o:Password>

</o:UsernameToken>

</o:Security>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<get:P_OFFSET>0</get:P_OFFSET>

<!--Optional:-->


<get:P_LIMIT>100</get:P_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>

 

And this is how I want the updated xml to look like:

 

<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmls.sas.com/actions" xmls:soapenv="http://smas.xml.org/">


-<soapenv:Header>


-<o:Security o:mustUnderstand="1" xmlns:o="http://doc.ois-open.org.xsd">


-<o:UsernameToken>

<o:Username>WKKJD</o:Username>

<o:Password Type="http://doc.ois-open.org/ws200401-wss-username-token-6.0#PasswordText">6dhgmfe6d20:</o:Password>

</o:UsernameToken>

</o:Security>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<get:P_OFFSET>1000</get:P_OFFSET>

<!--Optional:-->


<get:P_LIMIT>1000</get:P_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>

Please note there is a change in P_Offset and P_Limit.

Super User
Super User
Posts: 9,606

Re: Update a XML file through SAS

Posted in reply to katariasarthak

Whilst you could read that as a text file, and scan the text replacing what you need:

data _null_;
  infile "....xml";
  file "....xml";
  input;
  if index(_infile_,"<P_offset>") > 0 then do;
    ...;
  end;
  put _infile_;
run;

I don't really recommend it.  A proper XML tool to handle XML file usage would be better, designed to work with XML files.  With your own coding, you may change more than expected, or alter other things.

Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

We cannot read XML files into SAS using infile or file statement. XML files are only imported like: libname <library_name> xmlv2 '<Path><Filename>.xml';
Regular Contributor
Posts: 150

Re: Update a XML file through SAS

Posted in reply to katariasarthak

XML is merely text. Ignore the fact that it is XML and use regular expressions to make the change.

Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

We cannot read XML files into SAS using infile or file statement. XML files are only imported like: libname <library_name> xmlv2 '<Path><Filename>.xml';
Super User
Super User
Posts: 9,606

Re: Update a XML file through SAS

Posted in reply to katariasarthak

XML files are plain text files.  They can be read and written just like any other text file.

Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

Can you send a sample code to do so? Thanks!
Super User
Super User
Posts: 9,606

Re: Update a XML file through SAS

Posted in reply to katariasarthak

A shell of a program has already been provided in my previous post.

Solution
‎04-23-2018 02:19 AM
Super User
Posts: 10,283

Re: Update a XML file through SAS

Posted in reply to katariasarthak

@katariasarthak wrote:
Can you send a sample code to do so? Thanks!
data _null_;
input;
if index(_infile_,"P_OFFSET>") > 0 then do;
  begin = indexc(_infile_,'>');
  end = index(_infile_,'</');
  _infile_ = substr(_infile_,1,begin) !! '1000' !! substr(_infile_,end);
end;
put _infile_;
datalines4;
<?xml version="1.0"?>

-<soapenv:Envelope xmlns:get="http://xmls.sas.com/actions" xmls:soapenv="http://smas.xml.org/">


-<soapenv:Header>


-<o:Security o:mustUnderstand="1" xmlns:o="http://doc.ois-open.org.xsd">


-<o:UsernameToken>

<o:Username>WKKJD</o:Username>

<o:Password Type="http://doc.ois-open.org/ws200401-wss-username-token-6.0#PasswordText">6dhgmfe6d20:</o:Password>

</o:UsernameToken>

</o:Security>

</soapenv:Header>


-<soapenv:Body>


-<get:InputParameters>

<!--Optional:-->


<get:P_OFFSET>0</get:P_OFFSET>

<!--Optional:-->


<get:P_LIMIT>100</get:P_LIMIT>

</get:InputParameters>

</soapenv:Body>

</soapenv:Envelope>
;;;;
run;

Part of the log:

<get:P_OFFSET>1000</get:P_OFFSET>                                               

Add more conditional blocks as needed, and for production, add file and infile statements, and remove the datalines block.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

Posted in reply to KurtBremser
This code worked for me! Smiley Happy
I tried putting this in macro but it gave me an error!
Can you help me on that as well? I want to create multiple xml files with different P_Offset values! Like 1000,2000,3000 so on...
Super User
Posts: 10,283

Re: Update a XML file through SAS

Posted in reply to katariasarthak

Stages of macro development:

  • get SAS code that works
  • identify pieces of code that need to be dynamic
  • replace those pieces with macro variables, set the macro variables with %let before the code, and test
  • once that works, wrap the code into a macro definition where the macro variables are used as parameters

Example:

Existing code:

data _null_;
infile "infile.xml";
file "outfile.xml";
input;
put _infile_;
run;

Stage two: infile and outfile need to be dynamic

Stage three: code:

%let infile=infile.xml;
%let outfile=outfile.xml;

data _null_;
infile "&infile.";
file "&outfile.";
input;
put _infile_;
run;

Stage four:

%macro change_xml(infile,outfile);

data _null_;
infile "&infile.";
file "&outfile.";
input;
put _infile_;
run;

%mend;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: Update a XML file through SAS

[ Edited ]
Posted in reply to KurtBremser

This code works perfectly to change the tagset.

data _null_;
file "D:\blah1.xml"
infile "D:|blah.xml"
input; if index(_infile_,"P_OFFSET>") > 0 then do; begin = indexc(_infile_,'>'); end = index(_infile_,'</'); _infile_ = substr(_infile_,1,begin) !! '1000' !! substr(_infile_,end); end; put _infile_;
run;

When I add a macro statement like this, it creates different xml files, but the value in P_offset remains '&i'

​%macro file(); 
%do i=1 %to 5;
​_data _null_;
file "D:\blah&i..xml";
infile "D:\blah.xml";
input;
if index(_infile_,"P_OFFSET>") > 0 then do;
  begin = indexc(_infile_,'>');
  end = index(_infile_,'</');
  _infile_ = substr(_infile_,1,begin) !! '&i' !! substr(_infile_,end);
end;
put _infile_;
run;
%end;
%mend file();
%file;

 

Thanks for the time and effort you have already put! Smiley Happy

Super User
Posts: 10,283

Re: Update a XML file through SAS

Posted in reply to katariasarthak

Macro triggers (& and %) are not resolved within single quotes. Use double quotes around &i.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 15 replies
  • 230 views
  • 7 likes
  • 4 in conversation