Monday, July 12, 2010

Generating Descriptive Please Wait Messages for Long Running Stored Processes

Well, its been a while since I posted to my blog. And to acknowledge this, I choose a topic that is all about being patient ;-).

Most everyone who has built a web application that takes more than a split second to run knows that users want immeditate feedback. That is why many sites generating spinning logos (and other amusing things) to alert users that progress is being made.

Such functionality is fairly straigtforward to implement in SAS Stored Processes. So here goes.

First check out an example, running on my server. This example interleaves a DATA step containing a SLEEP function call with a call to my pleaseWait macro.

So how'd I do that? It was actually not that hard. I took advantage of some HTML style/display attributes that can be controlled thru JavaScript combined with the SAS ODS HTML TEXT= statement. Here is a little code snippet from my pleaseWait macro that illustrates how it is done:

ods html text = "<span id=""pleaseWait&pleaseWaitCounter"">";
proc report data = pleaseWait nowd;
title "&message";
columns msg;
define msg / " ";
run;
ods html text = '</span>';

Some details:

  1. The pleaseWait SAS data set is a single observation SAS data set where the informative message is the value of the macro variable &message.

  2. The value of the data step variable msg is the logo I want to display.

  3. The macro variable reference &pleaseWaitCounter is used to allow for multiple status messages to be generated with each message replacing the prior one.

  4. I tried to do this all with the ODS HTML TEXT (i.e. without a PROC step), but it seems that ODS output in not streamed back to browser until there is a DATA and/or PROC step.

Check out the article on sasCommunity.org for the source code for the macro and the sample call. Feel free to comment here or on the sasCommunity discussion page if you have questions or want more details.

4 comments:

  1. Hello there (I couldn't find your e-mail so I am writing this here)

    I'd like to invite you to add your blog to the new:
    http://sas-x.com/
    Website.

    It's a news website, all dedicated to SAS, based on bloggers content.

    I am it's founder, and I created it after creating a similar (and somewhat successful) such website around the topic of R programming. That website can be seen at:
    http://www.r-bloggers.com/

    If you are interested in joining, you can simply add your site at:
    http://sas-x.com/add-your-blog/

    Best wishes,
    Tal

    ReplyDelete
  2. Hi Don,

    I had to do a similar thing and also wrestled with ods text= not flushing output. I ended up using the following technique of closing/reopening _webout with no_top_matter and no_bottom_matter to force the flush of a document fragment (I actually had it in a %logMessage macro). There is a bit of extra work to make sure a valid HTML document is generated by preventing %stpbegin from closing the document (no_top_matter) on first flush and making sure %stpend closes the document at the end.

    Here is the general outline:

    %* call stpbegin in a way that it wont close off the html document when we flush output;
    %let _ODSOPTIONS=body=_webout(no_bottom_matter);
    %stpbegin

    %* example message loop;
    %do i = 1 %to 10;
    ods html text="Iteration &I";
    %* close/reopen _webout to flush ods output without generating HTML header/footer;
    ods html body=_webout(no_bottom_matter no_top_matter);
    %end;

    %* other processing ...;

    %* flush output ready for %stpend to close off the html document;
    ods html body=_webout(no_top_matter);

    %stpend

    Cheers
    Paul

    ReplyDelete
  3. Don,
    Thanks for providing this tip. Unfortunately, I've not been able to see the example running on your server... seems that it is not working right now. I'd like to implement something like this. Could you check your server please. btw, I have your SAS book and love it. Thanks!
    -David

    ReplyDelete
  4. Sorry for not replying sooner. I've been away from the blog and somehow the setting to notify me of comments got turned off. It should now be fixed.

    It sounds like you resolved your issue Paul.

    And David, I moved the site recently and it still has a bit more tweaking to get it working. I have some time now and so I hope to get it operational soon. I will post again, once I've updated the link to a working URL.

    ReplyDelete