If you have code that's under construction and should never be run while you work on other parts of your program, you can now "IF 0" out the entire block. As a longtime C and C++ programmer, this reminds me of the "#if 0 / #endif" preprocessor directives as an alternative for commenting out blocks of code. Glad to see this in SAS!

Check a system environment variable before running code

For batch jobs especially, system environment variables can be a rich source of information about the conditions under which your code is running. You can glean user ID information, path settings, network settings, and so much more. If your SAS program needs to pick up cues from the running environment, this is a useful method to accomplish that.

Limitations of %IF/%THEN in open code

As awesome as this feature is, there are a few rules that apply to the use of the construct in open code. These are different from what's allowed within a %MACRO wrapper.

First rule: your %IF/%THEN must be followed by a %DO/%END block for the statements that you want to conditionally execute. The same is true for any statements that follow the optional %ELSE branch of the condition.

And second: no nesting of multiple %IF/%THEN constructs in open code. If you need that flexibility, you can do that within a %MACRO wrapper instead.

And remember, this works only in SAS 9.4 Maintenance 5 and later. That includes the most recent release of SAS University Edition, so if you don't have the latest SAS release in your workplace, this gives you a way to kick the tires on this feature if you can't wait to try it.

About Author

+Chris Hemedinger is the manager of SAS Online Communities. 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.
He also hosts the SAS Tech Talk webcasts each year from SAS Global Forum, connecting viewers with smart people from SAS R&D and the impressive work that they do.

Also, the MLOGIC Option does NOT show any execution trace information in the SAS log for the %IF %THEN statement when used in Open Code. The trace Information is only displayed when the statement is contained within a Macro definition.

But am I the only one who feels like this is around 20 years late? Are we so excited about this simply because the SAS base language has lacked modern programming constructs for so long?

What I think would be even cooler is something like (it's just an illustration, to convey a concept - don't take it literally as the final syntax):

function foo(a,b);
return a**b;
endfunc; * or whatever... {} would also work, but would not be "sas-onic" ("pythonic") ;
data foo;
do x=1 to 10;
do y=1 to 5;
z=foo(x,y); /* not proc fcmp...a "proper" support of open code function definitions */
end
end;
run;
if foo.nobs = 0 then do;
print "foo is empty";
end;
do name="class","stocks","shoes";
data.output=catx(".","work",name);
set.input=catx(".","sashelp",name);
run;
end;

Instead, we still have a "C preprocessor" (https://en.wikipedia.org/wiki/C_preprocessor) (ok, a C preprocessor on steroids) to control what gets sent to the SAS compiler, instead of more modern programming constructs like ***open code*** functions, if statements, do & while loops, properties and methods, etc.

The SAS data step language is great for data processing - I know it and use it every day. But it feels "tired" to me, compared to when I use Python, Java, C#, Powershell, and other modern programming languages.

I know SAS is married to the idea that 40 year old code still runs, but I wish there was something like:

The SAS macro language has been the "control" language of SAS for decades, and those proficient in SAS macro have accomplished amazing feats of reuse and control. Of course it predates many of the other languages you mention, so the syntax has a learning curve. Also, projects like PROC LUA and the saskernel (Jupyter) and SASPy are designed to allow you to mix your "other favorite" languages with SAS.

As far as new innovations and "new SAS", you'll find a ton of new syntax in SAS Viya/CAS. In addition to some modern constructs (action sets), the language is optimized for parallel processing and in-memory computation. See this paper for an introduction.

This is really a good news. Most recently I used macro routine with %IF-%THEN %ELSE to check if a macro variable exists or not. If exists I need to run one program and if not then other program. This new feature will simplify my code and also what I noticed in SAS EG is the color for the syntax will be maintained in open code, where as in between %MACRO - %MEND will be plain.

Yes, that works as long as &MYCONDITION is defined as a numeric value. 0 is "false", any other number is "true". If &MYCONDITION is undefined as a macro symbol, or is assigned as any other type (character string), I think you'll see an error in the log. And if you try something tricky like:

...you'll still see an error if &MYCONDITION is not defined. Looks like SAS will try (and fail) to resolve the macro value even if you attempt to short-circuit the check with a more complex boolean. So if you absolutely need this check, you would need to guarantee a defined value for the macro var.

I 100% agree with you, Scott. The fact that the SAS macro has accomplished amazing feats is not a justification for requiring everyone to use it. Being able to do if/then in open code seems like a requirement twenty years ago and not a great, new feature. Aside from the fact that there's no telling when my organization will get this, not being able to use nested if statements means this is only a very small improvement. As you suggested, why not have an option that reads open code and macros as text (i.e. - as SAS currently is) or allows you interact with SAS like a true programming language? Syntax, user interface, and functionality are all reasons why SAS has been such a frustrating part of my job.

Mark, I can appreciate your view on this. My view: SAS isn't (just) a programming language -- it's a set of capabilities that reach deeper and broader than anything else I've seen. Having built systems that use a combination of SAS and something else (.NET, Java, HTML), I know that there are other languages that provide a better feel for certain types of applications. Still, when it comes to anything related to data, analytics, and reporting -- I would not want to be without SAS.

And I think your comment gets to the root of my frustration. SAS is great to have for some of the specific tasks it's built to handle, but I don't see it as a programming language, at all. In cases like mine where all you have is SAS, trying to incorporate very basic programming concepts like if/then loops is very cumbersome which means hard-to-read code and lots of mistakes.

I think it's also worth pointing out that SAS trails other options for the biggest data task of all - cleaning and understanding new data sets. Since so much of SAS is based on either point/click or printing out long reports, it takes far longer than it should to even get column names, convert between data types, etc. Here, the issue is a combination of both the 'language' and the IDE options you have for SAS. I can't print to a console, and I can't modify my workspace the way you can with lots of other IDEs.