Hope for ugly programs

23

Have you ever inherited a SAS program from a "gifted" SAS programmer? By "gifted", I mean a person who regards line feeds and white space as a waste of precious bytes, who knows that his program is worth the tremendous effort it might take to read and understand it, as if he's the William Faulkner of SAS programming?

Or maybe his theory is that an ugly program just works better, taking a page from Jimmy Soul on how to be happy for the rest of your life.

In SAS Enterprise Guide 4.3 (due out later this summer), we've added a "code formatter" feature that can take your densely formatted SAS program and tidy it up so that it's much easier to read.

We showed this off at SAS Global Forum in April, and one customer said, "Hey, I've got a really ugly program. I wonder what the formatter would do with it?" Note: In his defense, I don't think he wrote the program. I think he just lives with it, as many of us do. Here's a snippet from the program as it is:

Yep, it's ugly
I opened the program in SAS Enterprise Guide 4.3, pressed Ctrl+I (the shortcut key for "indent this!"), and in less than a second the entire scene was beautified. Here's the same snippet, reformatted:

Ah, much better
If you want to see the entire program, before and after, follow these links:

By the way, I created the above-linked pages using another new feature: Export a SAS program as HTML. It's a great way to export your program and preserve the coloring and spacing for display within a web page. I don't know if anyone but me will use it, but that's worth it to me.

Here are common questions that people ask when we demonstrate the formatter feature:

  • Can you control the style of formatting? Yes, to a limited extent. There aren't enough options to satisfy every preferred style, but as one programmer said, "if you format it for me, maybe I'll just change my style to match."
  • What if I don't like the formatted version? Undo is just a click (or Ctrl+Z keystroke) away.
  • Will this change the way my program works? Not at all. The only changes are made to code lines (not content such as DATALINES) and only indenting and line breaks for statements and logic constructs.
  • What about macro programs? Yep, it will make those pretty too.
  • Can I reformat just a portion of my program? Yes, simply select the desired lines, press Ctrl+I, and BOOM! -- just those lines are reformatted.

Do you have a SAS program that you consider, well, sort of homely looking? Are you curious what the formatter might do with it? Feel free to send it to me at Chris.Hemedinger [at]sas.com and I'll give it a whirl. (Don't submit it as a blog comment, since the original formatting would be lost. And please, no programs that contain company secrets, passwords, or proprietary algorithms you don't want to share.)

Share

About Author

Chris Hemedinger

Director, SAS User Engagement

+Chris Hemedinger is the Director of SAS User Engagement, which includes our SAS Communities and SAS User Groups. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies

23 Comments

  1. Very interesting feature. I'm also looking forward to using the auto-completion feature with EG 4.3. Eventually SAS is coming closer to Java, C++ and C# editors...

  2. Chris Hemedinger on

    At this point, I'd say no, don't expect these features to appear in SAS display manager.

  3. This is awesome. Of course, you just ruined one of my points for my paper on moving from novice to intermediate programmer - write code that looks more like a poem than a secret message.

    Oh, well, I'll get over it.

  4. Trevor Kearney on

    Chris,
    is there a way to customise the formatting: change the indentation spaces, ... etc.? (as like cc-mode in emacs?)
    What if I prefer:
    if condition then do: /* do on same line as then */
    body; /* 3 spaces in */
    end; /* end vertically under if */

    I hope customisations are allowed, but if not, if for now, your formatter is still a really good feature.
    Trevor K.

  5. Trevor, yes, that is one of the options: keep THEN/ELSE/DO on one line or (by default) break them into different lines.

  6. Ralph Winters on

    Chris,
    Will it also ignore formatting of character strings which might have funny formatting, like:

    data test;
    a="Z
    EBRA";
    run;

    *- Ralph Winters;

  7. Alas, it does not. When applied to your example, the stripes of your "ZEBRA" get a bit fatter thanks to some extra white space.

    Thanks for the test case!

  8. Pingback: Job security through code obscurity - The SAS Dummy

  9. Pingback: Take SAS program editor abbreviations to the next level - The SAS Dummy

  10. I just found this posting. I don't typically (read practically never) use EG. But I am so impressed by this facility. I was given a terrible incomprehensible ugly macro. I saved its generated code using the MPRINT and MFILE options, pasted it into the EG program window, selected it all, right clicked and chose "format text", and, voila, it's readable.

    Thank you, thank you, thank you!

  11. This feature works great!

    How can we report a small bug...?

    If 'start' and 'end' are numeric variables in this code:

    if (condition) then do i=start to end;
    do_stuff;
    end;

    Then the SAS indenter (not thankfully the compiler) will think that the "end;" in the first line is linked with the "do". Looks like the indenter needs to check for a ";" after the "do" before finding it's matching "end;."

    • Chris Hemedinger
      Chris Hemedinger on

      Sam, thanks for the comment.

      Normally you could report this via SAS Technical Support. But I'll go ahead and send this on the the development team.

  12. Pingback: 5 keyboard shortcuts in SAS that will change your life - The SAS Dummy

  13. Pingback: SAS Talks: Favorite SAS Enterprise Guide Tricks - The SAS Dummy

  14. Thanks for the useful shortcut info.I have used ctrl+I and now sas program looks good.Is there any option to change lower case to upper case of sas keywords?

  15. Pingback: The SAS code formatter: more than a pretty face - The SAS Dummy

  16. Pingback: Is SAS Enterprise Guide making you stupid? - The SAS Dummy

  17. Pingback: 5 keyboard shortcuts in SAS that will change your life - The SAS Dummy

Back to Top