BookmarkSubscribeRSS Feed
Obsidian | Level 7



    We are required to produce .docx files with SAS. I searched on web and did not find a good solution. Does anyone help me to figure it out? 



Diamond | Level 26 RW9
Diamond | Level 26

Why the docx requirement?  We always output to RTF which is plain text with markup which Word can read just fine, and is portable and readable by anything, much better.  As for creating M$ Proprietary DOCX file format, I don't know of a direct way from SAS, Word is pretty awful to use, even worse than Excel.  Use RTFand if necessary SaveAs from Word when it has opened the RTF.

Tourmaline | Level 20

The "best" in terms of possibility of personalization would be using Add.-in for MS Office.

Using RTF ODS destination is another alternative (there are probably more).

Data never sleeps
Obsidian | Level 7

AC need to compile the finall files, so they asked for using docx format, instead of doc and rtf. I guess for AC docx is easy for compiling with new version of word.

Diamond | Level 26 RW9
Diamond | Level 26

AC?   I would check the requirement, RTF should be compilable with other file no problem, as I said, RTF is well accepted by Word.  As for final compilation, from my understanding PDF is preferred by almost everyone, at least in my industry, and it should matter what file format, you just print to PDF.

Obsidian | Level 7

AC: Administrative Coordinator

I find to a way by using VBA to convert doc to docx. However I like to use SAS to produce docx files directly if possible.

Does someone know how to use sas to produce docx files directlly?

Super User

@Niugg2010 wrote:

AC: Administrative Coordinator

I find to a way by using VBA to convert doc to docx. However I like to use SAS to produce docx files directly if possible.

Does someone know how to use sas to produce docx files directlly?

If you can use VBA to convernt doc to docx then you should be able to convert RTF to docx just as easily.

Obsidian | Level 7

Yes. You are correct. 

Super User

At present, SAS doesn't offer a way to create DOCX files directly. You can create an RTF file using ODS RTF and then using a VB script to convert the RTF file into a DOCX. If you system allows X command, SAS can call the VB Script as well. 


Or you could combine them into PDF which is what I suspect the end product may be. 


If you have the Add In for Microsoft you can likely build the report in word and create the DOCX file, but I suspect that's a lot more difficult than ODS RTF. 





  A .DOCX file is really a zip archive that holds XML files that describe your document. So a .DOCX file is not one file, it is a collection of files that conform to the new Microsoft XML standard for describing Word docs.


  SAS creates RTF -- not Microsoft proprietary XML as .DOCX. If you need to convert an RTF file to .DOCX, then write the RTF file to a folder and convert it to .DOCX using VBScript or some other process.


  You can prove to yourself that a DOCX file is a zip archive by doing this:

1) make a Word document and save it as .DOCX

2) close Word and find the file in Windows Explorer and rename the file from .DOCX

3) Rename the file to .ZIP -- click yes when you get the warning about changing the file extension

4) Open the renamed file with WinZip or a utility that will allow you to explore the folder trees

5) Explore the structure of the zip archive folders -- you will see a lot of XML files

6) You will find the main text of your document in the document.xml file inside the archive


  Here are screenshots that illustrate the process --split into 2 images --






  Most companies generate RTF files with SAS and then either resave them to .DOCX before editing or combine multiple RTF files in Word and then save as .DOCX after editing.


  Note that there was NO SAS programming involved in the above process. The only applications used were Word, Windows Explorer, WinZip and Notepad.



Obsidian | Level 7

Great thanks. Understand.

Quartz | Level 8

This is actually dated now.  As of SAS 9.4, there is a preproduction version of ods word which will directly created docx files.  For example, the following code produces the attached MS Word document.



ods word file="C:\temp\myoutput.docx";
proc print data=sashelp.class;
ods word close;
Diamond | Level 26 RW9
Diamond | Level 26

Interesting that after all this time they have made DOCX a destinatation.  I would still however stand by my original posts.  RTF being a far better format for outputs.  The reasons, RTF is markup, you can insert markup via report, or in data, its open, plain text file format makes it machine and programmatically readable, can be opened and parsed just like a word document.  So has all the functionality, without the drawbacks of docx (which is a zip file with XML parts).  If you need PDF from it, you can directly convert to PDF from RTF.  Only if there was no other option would I be generating docx off the bat.


Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


Register now!

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
  • 12 replies
  • 7 in conversation