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:
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:
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.)
23 Comments
Will this be built into the Enhanced Editor in Base SAS?
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...
Ugly program, beautiful blog!
At this point, I'd say no, don't expect these features to appear in SAS display manager.
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.
I will use the HTML for SAS code feature. Helps me format my SAS programs on my blog
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.
Trevor, yes, that is one of the options: keep THEN/ELSE/DO on one line or (by default) break them into different lines.
Chris,
Will it also ignore formatting of character strings which might have funny formatting, like:
data test;
a="Z
EBRA";
run;
*- Ralph Winters;
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!
Pingback: Job security through code obscurity - The SAS Dummy
Pingback: Take SAS program editor abbreviations to the next level - The SAS Dummy
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!
I'm glad you found it and glad it worked for you!
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;."
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.
Pingback: 5 keyboard shortcuts in SAS that will change your life - The SAS Dummy
Pingback: SAS Talks: Favorite SAS Enterprise Guide Tricks - The SAS Dummy
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?
Check out my short list of "key" keyboard shortcuts here!
Pingback: The SAS code formatter: more than a pretty face - The SAS Dummy
Pingback: Is SAS Enterprise Guide making you stupid? - The SAS Dummy
Pingback: 5 keyboard shortcuts in SAS that will change your life - The SAS Dummy