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

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
GeorgeSAS
Lapis Lazuli | Level 10

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!

View solution in original post

13 REPLIES 13
ballardw
Super User

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

Reeza
Super User

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.

 

GeorgeSAS
Lapis Lazuli | Level 10

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!

Reeza
Super User

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.

 

GeorgeSAS
Lapis Lazuli | Level 10

sample.doc file

Reeza
Super User

@GeorgeSAS You attachments aren't working. Save it as xml and change the extension to txt to upload it. 

 

GeorgeSAS
Lapis Lazuli | Level 10

Thanks!

 

 

data need1;
infile "c:\temp\sample.xml" truncover pad ;
input source $10000.;
run;

 

Reeza
Super User

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. 

GeorgeSAS
Lapis Lazuli | Level 10
not just simply change .doc to .xml.
opend .doc file then save as .xml file


Thank!
Reeza
Super User

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!

 

GeorgeSAS
Lapis Lazuli | Level 10

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!

GeorgeSAS
Lapis Lazuli | Level 10

Thanks!

 

I will try both way!

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 13 replies
  • 2494 views
  • 1 like
  • 4 in conversation