Joe
Joe

Reputation: 1

Including SAS macro variable when creating XML file from SAS

I am creating an xml file in SAS and want to replace EveningTelePhoneNum in the put statement with a macro variable but it doesn't seem to like it. See sample code:

put '  <Contact>';
if (pi ne "") then put '    <PersonalId>' pi +(-1) '</PersonalId>';
if (pi ne "") then put '    <EveningTelephoneNum>' '. ' +(-1) '</EveningTelephoneNum>';
put '  </Contact>';

Upvotes: 0

Views: 111

Answers (2)

Richard
Richard

Reputation: 27508

Use the XML library engine for robust and easy exporting of a flat data structure.

For example:

libname myxml XML "C:\Temp\sandbox.xml";

%let moneyvar = invoice;

data myxml.acura;
  set sashelp.cars;
  where make = "Acura";
  keep model &moneyvar;
run;

I used a macro variable moneyvar (although totally not necessary) to emulate the EveningTelePhoneNum idea in your question.

Upvotes: 1

Quentin
Quentin

Reputation: 6378

If you are trying to do:

%let fieldname=EveningTelephoneNum ;
put '  <Contact>';
if (pi ne "")         then put '    <PersonalId>' pi   +(-1) '</PersonalId>';
if (&fieldname ne "") then put '    <&fieldname>' '. ' +(-1) '</&fieldname>';
put '  </Contact>';

That won't work, because macro vars don't resolve inside single quotes. You could try (untested):

%let fieldname=EveningTelephoneNum ;
put '  <Contact>';
if (pi ne "")         then put '    <PersonalId>' pi   +(-1) '</PersonalId>';
if (&fieldname ne "") then put "    <&fieldname>" '. ' +(-1) "</&fieldname>";
put '  </Contact>';

But I may be misunderstanding your question. And there are probably easier ways to create an XML file than writing in manually, with PUT statements.

Upvotes: 2

Related Questions