- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello everyone,
I want to import a MS word .doc file into SAS ,after do simple change then export it to a new .doc file. but I want keep same format as the old one.format including font size,color paragraphs everything.
can I do it in SAS? or any other method?
I attached an sample doc file(sample.doc) here. I want import it into SAS and change June 12, 2006 to June 16, 2016 then export to a new file named newfile.doc with same format.
Thank you!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
first method: SAS
I open the .doc file and save it as .html file then do the following code:
filename in "C:\temp\sample.htm"; data sashelp.testsample; infile in pad missover lrecl=300; input source $200.; run; data need; set sashelp.testsample; year=year(date()); call symput('applyYear',strip(year-1)); run; data need; set need; if index(source,"June 12, 2006") then do; source=tranwrd(source,'2006',year); end; run; filename out "C:\temp\samplenew.doc"; data _null_; file out; set need; put source ; run;
The new .doc file is I need, change the view from web view to print view when open it in MS word.
Second method: VBA:
see attachment sampleVBA
the two arrows will change the year between 2006 and 2016.
the VBA is very simple .need add more feature to make the function better.
for example if I want change the year to current year ......
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GeorgeSAS wrote:
Hello everyone,
I want to import a MS word .doc file into SAS ,after do simple change then export it to a new .doc file. but I want keep same format as the old one.format including font size,color paragraphs everything.
can I do it in SAS? or any other method?
I attached an sample doc file(sample.doc) here. I want import it into SAS and change June 12, 2006 to June 16, 2016 then export to a new file named newfile.doc with same format.
Thank you!
Basically ain't gonna happen with SAS. DOC or DOCX file formats are not intended for data interchange SAS is intended to read data. You would have to write an entire Word document parser to do what it sounds like you want to accomplish.
You might be better of with some sort of Microsoft Visual Basic or VBA code or macro.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Doc or DocX?
If you're changing a single value in a docx files there are ways...a doc file however is pretty locked down.
I second the suggestion of using a VBS or VBA script instead. You can call it from SAS or even write it from SAS if so desired.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I will change the sample.doc to sample.xml. then I will import it into SAS. after modification then export it to newfile.doc.
now the thing is how to import sample.xml into SAS, I am working on it now.
here is the sample.xml in attachement, please help the code of importing it to SAS.
(
- The file sample.xml does not have a valid extension for an attachment and has been removed. sas,txt,csv,zip,pdf,ics,sx,sxs,doc,docx,xls,xlsx,egp,sav,sas7bdat,ctm,ctk,rtf,py are the valid extensions.)
it can't be attached, please just simply rename .doc to .xml.
data need1;
infile "c:\temp\sample.xml" truncover pad ;
input source $10000.;
run;
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
That works for DOCX files, after you unzip it, but not for DOC files. Which do you have?
GeorgeSAS wrote:
I will change the sample.doc to sample.xml. then I will import it into SAS. after modification then export it to newfile.doc.
now the thing is how to import xml into SAS, I am working on it now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
sample.doc file
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@GeorgeSAS You attachments aren't working. Save it as xml and change the extension to txt to upload it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
data need1; infile "c:\temp\sample.xml" truncover pad ; input source $10000.; run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
So that text file has content, including the word Georgia in some places. If I change the txt to XML, there's no more word Georgia in the file, so as far as I can see, that approach will not work.
You still haven't said if this is a doc or docx file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
opend .doc file then save as .xml file
Thank!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
If I do that I'll get a different file. I'm saying YOU should do that, create YOUR XML file, change the extension to txt and upload it.
How are you planning to automate that step by the way? And if you have to use VBA why bother with SAS at all...?
@GeorgeSAS wrote:
not just simply change .doc to .xml.
opend .doc file then save as .xml file
Thank!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
first method: SAS
I open the .doc file and save it as .html file then do the following code:
filename in "C:\temp\sample.htm"; data sashelp.testsample; infile in pad missover lrecl=300; input source $200.; run; data need; set sashelp.testsample; year=year(date()); call symput('applyYear',strip(year-1)); run; data need; set need; if index(source,"June 12, 2006") then do; source=tranwrd(source,'2006',year); end; run; filename out "C:\temp\samplenew.doc"; data _null_; file out; set need; put source ; run;
The new .doc file is I need, change the view from web view to print view when open it in MS word.
Second method: VBA:
see attachment sampleVBA
the two arrows will change the year between 2006 and 2016.
the VBA is very simple .need add more feature to make the function better.
for example if I want change the year to current year ......
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The proper tool for manipulating Word documents is MS Word. Use a VBA script with Word.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
I will try both way!