BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

 Would like to know what does 'if not(closed))' means in here? 

%MACRO CLOSED;
    CLOSED=0;
    IF AC_ACCOUNT_STATUS IN ("C","W","BW") OR (AC_CLOSED_DATE NOT IN (-21914,.)) OR BK_FLAG=1 THEN Closed = 1;
%mend

%macro OPEN_TRD;
OPEN_TRD=0;
IF (NOT(CLOSED)) THEN OPEN_TRD=1;
%MEND;
4 REPLIES 4
LinusH
Tourmaline | Level 20

This looks like normal data step statments, that happen to be encapsulated into macros.

This is a boolean logic, so if closed is equal to 0, it's evaluated as true, otherwise false.

Data never sleeps
Kurt_Bremser
Super User

Your macros create code that creates Boolean values, so you can use simpler logic:

%macro closed;
closed = (ac_account_status in ("C","W","BW") or ac_closed_date not in ('01jan1900'd,.) or bk_flag = 1);
%mend;

%macro open_trd;
open_trd = not closed;
%mend;

For better readability, I replaced the numerical date value with a human-readable literal.

 

FK1
Lapis Lazuli | Level 10 FK1
Lapis Lazuli | Level 10

Hi @HeatherNewton ,

 

the following things come to my mind.

Both Macros seem to be developed in the way that they can be called within a data step, since this line for example 

IF AC_ACCOUNT_STATUS IN ("C","W","BW") OR (AC_CLOSED_DATE NOT IN (-21914,.)) OR BK_FLAG=1 THEN Closed = 1;

is not macro langage condition that is expressed, since "IF" and "THEN" are not preceded by a "%" sign.
Therefore I concluded, that it must be a data step condition. As a consequence, these two lines:

OPEN_TRD=0;
IF (NOT(CLOSED)) THEN OPEN_TRD=1;

mean:

if closed eq zero, then OPEN_TRD equals one.

 

Here is an example:

 


data test;
set sashelp.class;

closed = age gt 13;

if (not(closed)) then a = 1;
if closed then a = 0;
run;
ballardw
Super User

Style comment:

I inherited a bunch of code for one project that had about 150 macros that looked like this. All they did was obfuscate what the data step (yes, singular. Only used in one place) they were placed in were doing to the data. Not really reused anywhere which makes the creation of a macro a pretty problematic thing to do.

 

Note: your first macro %mend statement is missing a ; so may not behave as expected if that is the actual code.

 

SAS only has two variable types in the basic data set: numeric and character. As such, SAS uses numeric values in the role of Boolean (logical) true/false values.

This may give some ideas:

data example;
   input x;
   if x then put 'True ' x=;
   else put 'False ' x= ;
datalines;
1
0
234
.
0.0005
-1234567
.N
1.4E12
;

Note that every value above except 0, missing and the special missing are treated as True when used in an IF.

The SAS comparisons will return 1/ 0 for true:   Y = (x > 3); for example would return 0 for Y when X is less than or equal to 3 or missing (missing is always less basically so cannot be greater than 3) and 1 otherwise.

This allows writing a statement like

y = (x=1)*4.5 + (x=2)*7.3 + (x=3)* 11;

which is equivalent to

if x=1 then y=4.5;
else if x=2 then y=7.3;
else if x=3 then y=11;
else y=0;

When you ask  "why" the reason becomes pretty obvious when you have a large data set (and maybe a few more X= values to use) as the IF / Then /Else code can take much longer to evaluate then the numeric computation.

So if you save 0.0000001 seconds on the calculation for 1,000,000,000,000,000 records it adds up.

 

Plus we get to write code that confuses new programmers.😈

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1163 views
  • 0 likes
  • 5 in conversation