The SAS Output Delivery System and reporting techniques

Update html page using SAS

Reply
Contributor
Posts: 41

Update html page using SAS


I hope I am posting this in correct group...

I have a SAS program that creates several html pages for a report. The reports have an index.html page and currently I have to manually edit the index page to create hyperlinks to the newly created html reports. Is there a way to update the index.html page using SAS code? For example, I want to create and insert html code (see insert.txt) each time I run the report based on the current date. I want to insert the code (insert.txt) at line 26 of (index.html), keeping the existing html code. Thanks in advance.

Attachment
SAS Super FREQ
Posts: 8,743

Re: Update html page using SAS

Hi:

  You must have write access to INDEX.HTML in order to do what you want to do. And, how will you ensure that no one else is accessing INDEX.HTML if you want to write to it? And, if index.html is on a corporate web site, have you checked with your Webmaster to ensure that you will be allowed to do what you want to do.

  Other than that, reading and writing ASCII text files is one of the things that SAS does best. And an HTML file is nothing more than an ASCII text file.

  Look for examples of using the FILE statement and the PUT statement within a DATA _NULL_ step in the documentation. Reading and writing your HTML file might be a bit trickier -- but not much. It sort of depends on whether you have your web server directory mapped to your SAS machine or whether you have to access the HTML file via FTP or the URL engine.

(http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000223242.htm)

  This is not really a reporting question, per se. More of a data and file access and file manipulation question using the DATA step.

cynthia

Contributor
Posts: 41

Re: Update html page using SAS

Hi Cynthia,

Thanks for the info. There is no web server to worry about. This is a local area network folder and I control permissions on the html file. I take a look through the link you sent. Thanks again.

SAS Employee
Posts: 25

Re: Update html page using SAS

what I have done in the past when faced with a similar task is include a comment line within the HTML markup where the text is to be inserted.  In the data step, simply read and output each line until you hit the comment (test for a key string using the SCAN or INDEX functions), then insert the new link, maybe the comment again (to mark the place for the next insertion), then go back to reading and outputting the remaining lines in the file.

Tim

Contributor
Posts: 41

Re: Update html page using SAS

Thanks Tim,

Do you happend to have some example code? If I can see the example I can usually figure it out.

SAS Super FREQ
Posts: 8,743

Re: Update html page using SAS

Hi:

  I don't have an HTML specific example, but I do have an example of inserting text from one file into another file. It's a silly example, but it does the job. Since HTML is just an ASCII text file, this may give you a place to start. This example was written for beginners, so instead of making the program more complicated, I showed creating an ASCII text file (you would not need this step) and then reading in the text to insert in a second step and then, in a third step, reading the first file and putting the  "new lines" into a new copy of the first file.

  For beginners, I don't generally recommending reading from and writing to the same file in the same program unless you are absolutely, positively sure that your program is error free, so this is a program that you can break down and study each step. If you're a more experienced programmer, then you know there are many other ways I could have written this program. Oh, there's also a bit of how to use a macro variable to stop a DO loop -- in this example, the number of lines to insert is always 4, but the person wanted to know how they could use a macro variable if they didn't know the number of array elements. So there's a CALL SYMPUT in one program and then the macro variable is used in a different step.

  Tim may have a more specific HTML example, but I think this will get you started.

cynthia

** Make an ASCII text file with the 1st and 3rd stanza;
** from Jabberwocky by Lewis Carroll;
data _null_;
  length line $100;
  infile datalines missover;
  input @;
  lg = length(_infile_);
  input @1 line $varying. lg;
        
  file 'c:\temp\fileone.txt';
  put _infile_ $varying. lg;
return;
datalines;
Twas brillig, and the slithy toves
  Did gyre and gimble in the wabe:
All mimsy were the borogoves,
  And the mome raths outgrabe.
He took his vorpal sword in hand:
  Long time the manxome foe he sought --
So rested he by the Tumtum tree,
  And stood awhile in thought.
;
run;

                             
** need to insert these lines after line 4 above;
** this file contains the 2nd stanza of Jabberwocky;
** I know that there are only 4 lines to insert;
** so I hardcoded the 4 in the array statement;
data insert(keep= il1-il4 numlines);
  length iline $50;
  retain il1-il4;
  infile datalines;
  input @1 iline $char.;
         
  array il $50 il1-il4;
  il(_n_) = iline;
  numlines = 4;
  call symput('numlines',left(put(numlines,2.0)));
  if _n_ = 4 then output;
return;
datalines;
"Beware the Jabberwock, my son!
  The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
  The frumious Bandersnatch!"
;
run;
       
ods listing;
proc print data=insert;
var numlines il:;
run;
       
data _null_;
  retain il1-il4;
  array il $50 il1-il4;
       
  ** now read in the 1st and 3rd stanza from the file;
  ** that was created above;
  infile 'c:\temp\fileone.txt' length=lg end=lastone;
  input @1 oline $varying. lg;
    
  ** make the "new" file with all 3 stanzas;
  file 'c:\temp\newfile.txt';
   
  if _n_ = 1 then do;
     set insert;
  end;
  else if _n_ = 4 then do;
     ** want the new lines to go AFTER what has just been read;
     ** so issue a PUT for the _INFILE_;
     put @1 _infile_ $varying. lg;
     put @1 '!!-- begin insert --!!';
     do i = 1 to &numlines;
       ilen = length(il(i));
       put @1 il(i) $varying. ilen;
     end;
     put @1 '!!-- end insert --!!';
  end;
       
  ** since line 4 was written above;
  ** write all the other _INFILE_ lines here;
  if _n_ ne 4 then put _infile_ $varying. lg; 
run;

Contributor
Posts: 41

Re: Update html page using SAS

Thanks Cynthia. I'll take a look at this and let you know how it turns out. It may be a few days before I get back as this is one of several projects I am working on.

Ask a Question
Discussion stats
  • 6 replies
  • 388 views
  • 4 likes
  • 3 in conversation