BookmarkSubscribeRSS Feed
nortrh
SAS Employee

 

/* 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;

nortrh_3-1638947498209.png

/* 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;

 

nortrh_4-1638947556450.png

/* 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;

nortrh_5-1638947599754.png

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!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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