« Maximum levels of 512
Presenting decision tree »


SAS/Macro. Data step

Posted by Oleg Solovyev on Mar 8, 2010

Recently I worked on a data step that creates additional variables in a table that was downloaded from a data warehouse. Those variables were created for data analysis purposes. For instance calculated account age at delinquency can be more important than account start date or date of delinquency.

Besides account age at delinquency there were many similar variables like the number of days:

  • since delinquency till the first call to the debtor,
  • since first call till the first promise to pay the debt,
  • since first promise to pay till the first payment,
  • etc.

I wrote the following code to calculate the first variable:

if not( missing(account_start_date) or missing(delinquency_start_date) )
then days_from_ASD_to_DSD = intck('day',account_start_date,delinquency_start_date );
else call missing(days_from_ASD_to_DSD);

Then I started copping the code to calculate other variables. After the third iteration I realized that I was doing something wrong. The thing is that SAS/Macro allows creating macros and calling them in a data step. For instance the code above can be rewritten as a macro:

%macro get_days_number(days_number, first_date, last_date);
  if not( missing(&first_date) or missing(&last_date) )
  then &days_number = intck('day', &first_date, &last_date);
  else call missing(&days_number);
%mend;

One has to run it before the data step to save it in a work library to make it available in a subsequent data step. Then you can run the macro in a data step the following way:

%get_days_number(days_from_ASD_to_DSD, account_start_date, delinquency_start_date);

This macro helped me to reduce the code by three times. Moreover if the algorithm changes I will have to fix the macro only.

Leave a Reply

Please type the word if you are not a spam-robot