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

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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