Writing macro variable to Xml (or txt file)

Accepted Solution Solved
Reply
Super Contributor
Posts: 418
Accepted Solution

Writing macro variable to Xml (or txt file)

Hello everyone. I need to create a XML file with a given output, using variables from a dataset (a txt file is fine too since I can make it into an xml easily).  I'm running into a problem because the only way I know how to create a xml file (or txt file) is to use a data _null_ step with datalines if I want it to have the EXACT structure that I need.

Does anyone know how to create an xml file with given headers and footers (found below) and use variables to populate it?

Anyways I have a dataset which has multiple varaibles named "outputs","outputs1","outputs2", etc.. of which there is 1 row. Please see the dataset as defined below.


data person;
   infile datalines delimiter=',';
   informat outputs1 $100.;
   informat outputs2 $100.;
   informat outputs3 $100.;

   input outputs1 $ outputs2 $ outputs3 $;
   datalines;                     
<loanId>23552</loanid>,<balance> 2352.153 </balance>,<Comment> This is a comment</comment>
;

I am trying to create an XML file (or txt) with the following output, using the above dataset to populate the values between the <pm:start> and </pm:start> variables... Note this is a simplifed example, in the actual code there are hundreds of rows that I have to loop over to create hundreds of Xml's (or txt files) so I cannot simply hard-code it.

The xml output needs to look like this...


data _null_;
   file request;
   input;
   put _infile_;
   datalines;
<soapenv:Envelope xmlns:soapenv="http://madeupsoapenvelope" xmlnsSmiley Tonguem="http://madeupwebservices/pm">
   <soapenv:Header/>
   <soapenv:Body>
      <pm:start>
         <username>fakename</username>
         <password>madeupaccount</password>
         <loanId>23552</loanid>,
       <balance> 2352.153 </balance>

     <Comment> This is a comment</comment>
      </pm:start>
   </soapenv:Body>
</soapenv:Envelope>
;;;;

run

The three variables need to be inputs from the first dataset... I tried defining 3 macro variables (or even 1 variable separated by strings, etc..). Anyways apparently you cannot use a macro variable insde of a "datalines" step when writing to an output file... Does anyone know how to do this? Sorry if it is an overly easy question, I am self taught and have never run into this before!  my code is below.

data _null_;

set person;

if _n_=1 then call symputx('macro1',outputs1);

if _n_=1 then call symputx('macro1',outputs2);

if _n_=1 then call symputx('macro1',outputs3);

run;

data _null_;

   file request;

   input;

   put _infile_;

   datalines;

<soapenv:Envelope xmlns:soapenv="http://madeupsoapenvelope" xmlnsSmiley Tonguem="http://madeupwebservices/pm">

   <soapenv:Header/>

   <soapenv:Body>

      <pm:start>

         <username>fakename</username>

         <password>madeupaccount</password>

       &macro1.

       &macro2.

       &macro3.
      </pm:start>
   </soapenv:Body>
</soapenv:Envelope>
;;;;

run

I know another way to do this would be to use a data _null_ step and simply "put" the variables thatI want into thefile (like below).. However in this methodology I actually don't know how to seperate the varaibles by Line feeds (to make them show up on new lines) as apparently the program i'm passing this too needs each variable to have it's own line in the xml (which seems strange to me, but hey I didn't write it).  So if anyone can tell me how to modify the following code to add line feeds between the variables that would help me a BUNCH as well!

data _null_;
set person;
file 'C:\Users\BrandonME\Desktop\testabc.xml';
put '<soapenv:Envelope xmlns:soapenv="http://madeupsoapenvelope" xmlnsSmiley Tonguem="http://madeupwebservices/pm">';
put '<soapenv:Header>';
put '<soapenv:Body>'
put '<pm:start>';

put outputs1--outputs3;
put '</pm:start>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
run;

Any help would be appreciated!

Thank you as always.


Accepted Solutions
Solution
‎04-24-2014 03:04 PM
Super User
Posts: 19,877

Re: Writing macro variable to Xml (or txt file)

Posted in reply to Anotherdream

You don't need macro variables, you can do it from a datastep, but put the variable instead of the macro variable.

You can probably even use the filename or filevar option to create a new file for each line, see the reference at the end of the file.

If you only need the header then use if _n_=1 then do;  to selectively output your header.

Same concept for footer.

data _null_;

set have;

file "temp.xml";

put ...header info...;

put '<soapenv:Envelope xmlns:soapenv="http://madeupsoapenvelope" xmlnsSmiley Tonguem="http://madeupwebservices/pm">';

put '<soapenv:Header/>'

put  '<soapenv:Body>'

put  var1value;

put var2value;

run;


See example 5 here to dynamically change the output file.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

View solution in original post


All Replies
Solution
‎04-24-2014 03:04 PM
Super User
Posts: 19,877

Re: Writing macro variable to Xml (or txt file)

Posted in reply to Anotherdream

You don't need macro variables, you can do it from a datastep, but put the variable instead of the macro variable.

You can probably even use the filename or filevar option to create a new file for each line, see the reference at the end of the file.

If you only need the header then use if _n_=1 then do;  to selectively output your header.

Same concept for footer.

data _null_;

set have;

file "temp.xml";

put ...header info...;

put '<soapenv:Envelope xmlns:soapenv="http://madeupsoapenvelope" xmlnsSmiley Tonguem="http://madeupwebservices/pm">';

put '<soapenv:Header/>'

put  '<soapenv:Body>'

put  var1value;

put var2value;

run;


See example 5 here to dynamically change the output file.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Super Contributor
Posts: 418

Re: Writing macro variable to Xml (or txt file)

Thanks for the information Reeza (as always).  Does the put statement automatically start a new line in the output file by chance?  Aka if I write a macro variable to automate the put var1value; put var2value; etc... Will the put clause start a new line?

If so I can wrap my put statement on my macro varaibles into a macro string and this is EXACTLY what I need!

Super User
Posts: 19,877

Re: Writing macro variable to Xml (or txt file)

Posted in reply to Anotherdream

You don't need macros.

Try it.

Super Contributor
Posts: 418

Re: Writing macro variable to Xml (or txt file)

Oh sorry, I was saying I need macros because I don't know the names of hte variables being passed in and out of each dataset (therefore I'd like it to be automated for the names). My general question was does "put Var1; put var2" automatically start a new line (which it apparently does).

Related to your statemnet to see example 5, I am not following what you are implying here. i'll do some more digging on how to create files in using the filevar variable... I tried to do some testing, but honestly this is the first time i've ever written to an external file other than excel, so I simply might not get it. Will keep you posted on how I do

🔒 This topic is solved and locked.

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

Discussion stats
  • 4 replies
  • 1593 views
  • 0 likes
  • 2 in conversation