BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

All, 
     When I export data as XML in SAS, is it possible to export it in a non-pretty format ? Kindly consider the following example: 

 

%Let WorkDir = %trim(%sysfunc(pathname(work)));
%put &WorkDir;

libname XMLData xmlv2 "&WorkDir./TestData.xml";
	
data Work.TestDataSet;
	set SASHelp.airline;
run;
	
data XMLData.TestData;
	Set Work.TestDataSet;
run;
	
1 ACCEPTED SOLUTION

Accepted Solutions
Vince_SAS
Rhodochrosite | Level 12

One way to accomplish this is to read the generated XML file, strip excess blanks, and write the data back out in a single record.

 

Perhaps a better solution is to create an ODS tagset that omits the CRLF and space characters in the first place.  This code creates the new tagset and stores it in WORK.  In a production environment you would run the code once to create the tagset in a permanent location, and then reference it when needed (without recompiling it):

 

ods path(prepend) work.tmplmst(update);
 
proc template;
define tagset tagsets.uglyxml;
 parent=tagsets.Sasioxml;
  notes "Ugly SAS-XML generic XML-Data";
   

   define event XMLversion;
      put "<?xml version=""1.0""";
      putq " encoding=" ENCODING;
      put " ?>";
      break;
   end;


   define event SASTable;
      start:
         put "<TABLE>" ;
         break;

      finish:
         put "</TABLE>" ;
         break;
   end;

   define event SASRow;
      start:
         put "<";
         put UPCASE(NAME);
         put ">";
         break;

      finish:
         put "</";
         put UPCASE(NAME);
         put ">";
         break;
   end;

   define event SASColumn;
      start:
         put "<";
         put "COLUMN" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put " name=""" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put NAME;
         put """" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         break;

      finish:
         break /if exists( MISSING);
         put " />" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put NL /if cmp( XMLDATAFORM, "ATTRIBUTE");
         break /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put "</";
         put NAME;
         put ">";
         break;
   end;

   define event MLEVDAT;
      putq " missing=" MISSING;
      put " />" /if exists( MISSING);
      put NL /if exists( MISSING);
      break /if exists( MISSING);
      put " rawvalue=""" /if exists( RAWVALUE);
      put RAWVALUE /if exists( RAWVALUE);
      put """" /if exists( RAWVALUE);
      put " value=""" /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put VALUE /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put """" /if cmp( XMLDATAFORM, "ATTRIBUTE");
      break /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put ">";
      put VALUE;
      break;
   end;
   
   private;
end;
run; quit;

 

Then use the tagset in your SAS code:

 

%let WORKDIR = %sysfunc(pathname(work));

libname xmldata xmlv2 "&WORKDIR/testdata.xml" tagset=tagsets.uglyxml;

data xmldata.testdata;
set sashelp.class;
run;

 

Vince DelGobbo

SAS R&D

(with help from Chevell Parker)

View solution in original post

9 REPLIES 9
ballardw
Super User

@UdayGuntupalli wrote:

All, 
     When I export data as XML in SAS, is it possible to export it in a non-pretty format ? Kindly consider the following example: 

 

%Let WorkDir = %trim(%sysfunc(pathname(work)));
%put &WorkDir;

libname XMLData xmlv2 "&WorkDir./TestData.xml";
	
data Work.TestDataSet;
	set SASHelp.airline;
run;
	
data XMLData.TestData;
	Set Work.TestDataSet;
run;
	

So, what exactly am I supposed to consider when looking at the output?

What do you mean by "without pretty format"? Which variables? What appearance do you want for them?

UdayGuntupalli
Quartz | Level 8

@ballardw,
        If you run the test script that I have attached, you can generate the following XML which you can view by creating a folder shortcut in SAS Studio. In my case, an external application is consuming a similar XML response that my code generates. It is my understanding that the highlighted child nodes have been formatted into a "pretty" format to make it more readable. 
         This however is inefficient for the consumer of my data and I would like to avoid formatting it as "pretty" 

 

image.png

 

Tom
Super User Tom
Super User

Can you get them to clarify what they mean?  

The white space is trivial in size and irrelevant to processing so I am not sure it is worth doing anything about that.

 

If they don't like how SAS structured the XML then perhaps they can supply you with an XMLMAP file that SAS can use to produce that format they prefer.  Or just write your own data step to write the XML file in the format they want.

 

Tom
Super User Tom
Super User

I don't understand the question.  The only "pretty" thing that SAS is doing is putting in line breaks and indentation.

Why would you want to remove that?  To save space?  If you want to save space then output a CSV file.

UdayGuntupalli
Quartz | Level 8

@Tom
    "pretty" is not my term. "pretty" is a format that XML is formatted into. 
https://atom.io/packages/xml-formatter 

 

ballardw
Super User

@UdayGuntupalli wrote:

@Tom
    "pretty" is not my term. "pretty" is a format that XML is formatted into. 

 


Still doesn't say anything about what you want it to look like.

To you mean that you want each "row" of data to appear without any line feeds? Similar to:

   <TESTDATA><Name>Alfred</Name><Sex>M</Sex><Age>14</Age><Height>69</Height><Weight>112.5</Weight></TESTDATA>

Note: the sashelp.airline data is not in every SAS install. So I used sashelp.class.

 

Vince_SAS
Rhodochrosite | Level 12

One way to accomplish this is to read the generated XML file, strip excess blanks, and write the data back out in a single record.

 

Perhaps a better solution is to create an ODS tagset that omits the CRLF and space characters in the first place.  This code creates the new tagset and stores it in WORK.  In a production environment you would run the code once to create the tagset in a permanent location, and then reference it when needed (without recompiling it):

 

ods path(prepend) work.tmplmst(update);
 
proc template;
define tagset tagsets.uglyxml;
 parent=tagsets.Sasioxml;
  notes "Ugly SAS-XML generic XML-Data";
   

   define event XMLversion;
      put "<?xml version=""1.0""";
      putq " encoding=" ENCODING;
      put " ?>";
      break;
   end;


   define event SASTable;
      start:
         put "<TABLE>" ;
         break;

      finish:
         put "</TABLE>" ;
         break;
   end;

   define event SASRow;
      start:
         put "<";
         put UPCASE(NAME);
         put ">";
         break;

      finish:
         put "</";
         put UPCASE(NAME);
         put ">";
         break;
   end;

   define event SASColumn;
      start:
         put "<";
         put "COLUMN" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put " name=""" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put NAME;
         put """" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         break;

      finish:
         break /if exists( MISSING);
         put " />" /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put NL /if cmp( XMLDATAFORM, "ATTRIBUTE");
         break /if cmp( XMLDATAFORM, "ATTRIBUTE");
         put "</";
         put NAME;
         put ">";
         break;
   end;

   define event MLEVDAT;
      putq " missing=" MISSING;
      put " />" /if exists( MISSING);
      put NL /if exists( MISSING);
      break /if exists( MISSING);
      put " rawvalue=""" /if exists( RAWVALUE);
      put RAWVALUE /if exists( RAWVALUE);
      put """" /if exists( RAWVALUE);
      put " value=""" /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put VALUE /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put """" /if cmp( XMLDATAFORM, "ATTRIBUTE");
      break /if cmp( XMLDATAFORM, "ATTRIBUTE");
      put ">";
      put VALUE;
      break;
   end;
   
   private;
end;
run; quit;

 

Then use the tagset in your SAS code:

 

%let WORKDIR = %sysfunc(pathname(work));

libname xmldata xmlv2 "&WORKDIR/testdata.xml" tagset=tagsets.uglyxml;

data xmldata.testdata;
set sashelp.class;
run;

 

Vince DelGobbo

SAS R&D

(with help from Chevell Parker)

UdayGuntupalli
Quartz | Level 8

@Tom,
  That is correct. I will attempt to implement @Vince_SAS's solution unless someone has a better solution. 

 

 

Reeza
Super User
Minify is the term, you’re looking for I believe. Pretty - means legible, minified is to remove spaces, line breaks etc. The data in the file is the same but makes it smaller in size. The parsing shouldn’t be different so this would really only matter for big files.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1865 views
  • 11 likes
  • 5 in conversation