Saturday, November 8, 2008

DateTime_Manip : a small SAS utility Tool.

This code is used to generate date and datetime values between a given range with a constant gap.

Here the constant gap may be day,week,month,year etc.,
For more details, see the comments in code.

Here you need to provide DATASET name in which you want to store created dates.
DT_SLOT: type of interval ex: DAY,WEEK,TENDAY,SEMIMONTH etc.,
START_VALUE: sas date, time, or datetime value that identifies a starting point.
END_VALUE: sas date, time, or datetime value that identifies a end point.
GAP: This is the number of intervals to shift the value of START_VALUE.
FORMA: Format which is used to display the value, easy to read.

SAS Code:


/* options to print resolved macro code in log file */
options mlogic mprint symbolgen;

/* use TIME8. format for time, DATE9. format for date and NLDATM21. format for datetime values */
/* use "01:00:00"t like values for Time, "1jan2001"d like values for date and
"1jan2001:23:59:59"dt like values for Datetime */
/* You can increment SECOND,MINUTE,HOUR(as 1st argument) for both Time and Datetime values,
DAY,WEEK,TENDAY,SEMIMONTH,MONTH,QTR,SEMIYEAR,YEAR etc., for Date values.
Just add DT for first argument, if you want to operate Date related things on Datetime values */

/* Macro code to generate date and datetime values with in the specified range, with specific gap. */
%macro dt_generate(dataset=,dt_slot=,start_value=,end_value=,gap=,forma=);

data &dataset;
dt_var=&start_value;
do while (dt_var<&end_value);
dt_var=intnx("&dt_slot",dt_var,&gap);
format dt_var &forma.;
output;
end;
run;

%mend dt_generate;
/* end of the macro code */

/* Here you need to provide DATASET name in which you want to store created dates/times.
DT_SLOT: type of interval ex: DAY,WEEK,TENDAY,SEMIMONTH etc.,
START_VALUE: sas date, time, or datetime value that identifies a starting point.
END_VALUE: sas date, time, or datetime value that identifies a end point.
GAP: This is the number of intervals to shift the value of START_VALUE.
FORMA: Format which is used to display the value, easy to read.
*/

%dt_generate(dataset=xxx,dt_slot=dtmonth,start_value="1jan2001:00:00:00"dt,end_value="1jan2002:00:00:00"dt,gap=1,forma=NLDATM21.);

--------------- END -----------------------------------------------------------

No comments: