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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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