SAS Employee

## Juletip #8 - Making periods from time spans

``````/* Sometimes it is necessary to reorganize data in periods instead of having the data 	            */
/* in one row per time span. This example combines the INTNX and INTCK functions to do that			*/

data christmas_periods;
length christmas_food1 christmas_food2 \$ 22;
input christmas_food1 \$ christmas_food2 \$ start_date: date9. end_date :date9. quantum;
format start_date end_date ddmmyyp10.;
cards;
Trouth VanillaPudding 01dec2021 23dec2021 20
Ribs PepperCake 24dec2021 27dec2021 5
ChristmasPudding Beer 28dec2021 10jan2022 10
;
run;``````

``````/* Use the INTNX function to make periods from date spans	*/
data period_day(drop=i);
set christmas_periods;
number_of_days=end_date-start_date;
do i=0 to number_of_days;
date=intnx('day',start_date,i);
quantity=quantum/number_of_days;
output;
end;
format date ddmmyy10.;
run;
``````

``````/* Combine the INTCK and the INTNX functions to make periods other than days	*/
data period_week;
set christmas_periods;
number_of_weeks=intck('weekv',start_date,end_date);
do i=0 to number_of_weeks;
week=intnx('weekv',start_date,i);
quantity=quantum/number_of_weeks;
output;
end;
format week yyweekv8.;
run;
``````

One of the advantages of this technique is that it is possible to distribute measures on time periods other than those on the basic data. A disadvantage could be that it generates a lot of rows in the result data set, but SAS is capable to handle that!

Discussion stats
• 0 replies
• 281 views
• 1 like
• 1 in conversation