BookmarkSubscribeRSS Feed
Niugg2010
Obsidian | Level 7

Hi,

 

    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? 

 

Thanks

12 REPLIES 12
RW9
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.

LinusH
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
Niugg2010
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.

RW9
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.

Niugg2010
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?

ballardw
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.

Niugg2010
Obsidian | Level 7

Yes. You are correct. 

Reeza
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. 

 

 

Cynthia_sas
SAS Super FREQ

Hi:

  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 --

_1_make_docx.png

 

Then:

docx_is_zip_archive.png

 

  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.

 

cynthia

Niugg2010
Obsidian | Level 7

Great thanks. Understand.

statistician13
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;
run;
ods word close;
RW9
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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 6479 views
  • 2 likes
  • 7 in conversation