AFHood Group Blog The thoughtless yammerings of marketing junkies..

3Dec/090

SAS INTNX function ( add days, months, years, etc )

The INTNX function increments dates, times, or datetimes by specific or custom intervals.

Here is the basic syntax for INTNX:

INTNX(interval[multiple.shift-index], start, increment[, alignment])

The interval can be a set value like - DAY, WEEK, DTWEEK, YEAR, etc. You can also specify a custom interval. We will post more on that later.

The multiple is optional and used as such. If you want the interval to be a 2 year period or biennial, you could use YEAR2. It is the same with weeks. If you were calculating 2 week pay periods and you wanted to increment by such, you would declare the interval to be WEEK2.

The shift-index lets you define a starting point within the interval. An example would be to start the week on Monday.  The interval designation should be WEEK1.2 .

The start is your date, time, or datetime at which you want to begin the increment.

Increment is a positive or negative integer representing the number of interval shifts that should be made from the start.

Alignment is an excellent tool. It allows you to designate at which point SAS should return within an interval. You can specify:

B - beginning

M - middle

E - end

S - same

The default for alignment is beginning.

29Oct/090

SAS DIM function – Counting the elements in an array

The DIM function returns the number of literal elements in an array. It functions against multi-dimensional arrays as well as one-dimensional arrays.

1-dimensional array example

DIM(array_name)

Multi-dimensional array examples

DIM(m_array) -> returns the number of elements in the first dimension of the array

DIM5(m_array) -> returns the number of elements in the 5th dimension of the array

DIM(m_array, 5) -> returns the number of elements in the 5th dimension of the array

The classic use case for the DIM function is to return the number of elements in an array for the upper bound of a do loop process. Example:

array array_name(5) var1 var2 var3 var4 var5;

do i=1 to dim(array_name);

some SAS statements here

end;

22Oct/090

SAS DIF function – Comparing previous records

The SAS DIF function is another useful tool for operating on previous records.

Lets say you want to know the difference in the last year's revenue and this year's revenue. Here is the data:

Year          Revenue

2001           10,000

2002          30,000

2003          60,000

2004          100,000

data revenue1;

set revenue;

Rev_increase=dif1(revenue);

run;

Year          Revenue         Rev_increase

2001           10,000         .

2002          30,000         20,000

2003          60,000         30,000

2004          100,000       40,000

The dif functions compares the current record to the nth previous record.