SmallFry
SmallFry

Reputation: 153

Use of the '<' operator in SAS

I have to convert some SAS code. In other programming languages I am used to < being used in comparisons e.g. in pseudo-code: If x < y then z

In SAS, what is the < operator achieving here:

intck(month,startdate,enddate)-(day(enddate)<day(startdate))

I have been able to understand the functions using the reference documentation but I can't see anything relating to how '<' is being used here.

Upvotes: 1

Views: 92

Answers (2)

Joe
Joe

Reputation: 63424

Just to go into a little more detail about what the code you have there is doing, it's an old school method to determine the number of months from one date to the next (possibly to calculate a birthday, for example).

Originally, SAS functions intck and intnx only calculated the number of "firsts of the month" in between two dates (or similar for other intervals). So INTCK('month','31OCT2020'd, '01NOV2020'd) = 1, while INTCK('month','01OCT2020'd,'30NOV2020'd) = 1. Not ideal! So you'd add in this particular bit of code, -(day(enddate)<day(startdate)), which says "if it is not been a full month yet, subtract one". It's equivalent to this:

if day(enddate) < day(startdate) then diff = intck(month,startdate,enddate) - 1;
else diff = intck(month,startdate,enddate);

There's now a better way to do this (yay!). intck and 'intnx' are a bit different, but it's the same idea. For intck the argument is method, where c for "continuous" is what you want to compare same period in the month. For intnx it is the alignment option, where 's' means "same" (so, move to the same point in the month).

So your code now should be:

intck(month,startdate,enddate,'c')

Upvotes: 3

Tom
Tom

Reputation: 51611

The symbol < is an operator in that expression. It is not a function call , like INTNX() is in your expression.

SAS evaluates boolean expressions (like the less than test in your example) to 1 for TRUE and 0 for FALSE.

So your expression is subtracting 1 when the day of month of ENDDATE is smaller than the day of month of STARTDATE.

Note: You can also do the reverse, treat a number as a boolean expression. For example in a statement like:

if (BASELINE) then PERCENT_CHANGE = (VALUE-BASELINE) / BASELINE ;

A missing value or a value of zero in BASELINE will be treated as FALSE and so in those cases the assignment statement does not run.

Upvotes: 3

Related Questions