BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

Hi,

 

How do I add 45days from from a year ago to today's date?

 

this is what i have so far in my program but it is only adding 45days for 08OCT2019 (period 2). 

I need to create period 3, period 4, period 5, etc. until present day. Thank you

 

data x;
    period1dt='08OCT2019'd;
    day45=intnx('day',period1dt,45);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

@HitmonTran,

 

Do you mean efficient or do you mean flexible?

 

If you want to make it so there is little hard coding and the program is very flexible, you could use macros like the below example.  Here, even the variable names in the output SAS dataset are dynamically determined based on the values of the macro variables set at the top of the program.

 

Results from the below program:

jimbarbour_0-1601593971592.png

 

Jim

 

%LET	Period_Start	=	'08OCT2019'd;
%LET	Period_End		=	%QSYSFUNC(INTNX(DAY,&Period_Start, 365));
%LET	Increment		=	45;

%MACRO	Calculate_Periods;
	%DO	i	=	1	%TO	999;
		%GLOBAL	Day45_&i;
		%GLOBAL	Nbr_Increments;
		%IF	%SYSFUNC(INTNX(DAY,&Period_Start, %EVAL(&Increment * &i)))	>	&Period_End	%THEN
			%DO;
				%LET	Nbr_Increments	=	%EVAL(&i - 1);
				%LET	i				=	999;
			%END;
		%ELSE
			%DO;
				%LET	Day&Increment._&i		=	%SYSFUNC(INTNX(DAY,&Period_Start, %EVAL(&Increment * &i)));
			%END;
	%END;
%MEND	Calculate_Periods;

%Calculate_Periods;
*%PUT	_USER_;

%MACRO	Generate_SAS_Code;
	%DO	i					=	1	%TO	&Nbr_Increments;
		Day&Increment._&i	=	&&Day&Increment._&i;
	%END;
%MEND	Generate_SAS_Code;

data x;
	FORMAT	Period_Start Day&Increment._1 - Day&Increment._&Nbr_Increments Period_End YYMMDDD10.;
	Period_Start	=	&Period_Start;
	Period_End		=	&Period_End;
	%Generate_SAS_Code;
run;

 

 

View solution in original post

7 REPLIES 7
jimbarbour
Meteorite | Level 14

If it's it's a set period you need the 45 day incrdments for, you could do something like:

 

data x;
    period1dt='08OCT2019'd;

day45_1=intnx('day',period1dt,45); day45_2=intnx('day',day45_1,45); day45_3=intnx('day',day45_2,45); day45_4=intnx('day',day45_3,45); day45_5=intnx('day',day45_4,45); day45_6=intnx('day',day45_5,45); day45_7=intnx('day',day45_6,45); day45_8=intnx('day',day45_7,45); run;

Jim

jimbarbour
Meteorite | Level 14

Results from the above (with formats added for clarity):

 

jimbarbour_0-1601591854634.png

 

Jim

CurtisMackWSIPP
Lapis Lazuli | Level 10

And if you want it as rows and a little less hardcoded.

 

data x;
    format period1dt date10.;
    period1dt='08OCT2019'd;    
    do until(period1dt > date());
      output;
      period1dt = intnx('day',period1dt,45);
    end;
run;
HitmonTran
Pyrite | Level 9
is there a way to make the program more efficient?
ballardw
Super User

@HitmonTran wrote:
is there a way to make the program more efficient?

Define efficient.

 

An array and loop counter for the number of times you want to add 45 days would be one way.

But since 45 does not go evenly into a year, 8.11111 (repeats) when the year is 365 days or 8.13333 (repeats) when 366 days, then your definition for "intervals for 1 year" needs some expansion.

 

 

CurtisMackWSIPP
Lapis Lazuli | Level 10

Perhaps you are looking for something like this:

 

  data have;
    startdt='08OCT2019'd; output;
    startdt='08DEC2019'd; output;
    startdt='08DEC2017'd; output;
  run;

  data x;
      set have;
      array period [1:100] ;
      i = 1;
      period1dt = startdt;
      do until(period1dt > date() or i > 100);
        period(i) = period1dt;
        period1dt = intnx('day',period1dt,45);
        i = i + 1;
      end;
  run;
jimbarbour
Meteorite | Level 14

@HitmonTran,

 

Do you mean efficient or do you mean flexible?

 

If you want to make it so there is little hard coding and the program is very flexible, you could use macros like the below example.  Here, even the variable names in the output SAS dataset are dynamically determined based on the values of the macro variables set at the top of the program.

 

Results from the below program:

jimbarbour_0-1601593971592.png

 

Jim

 

%LET	Period_Start	=	'08OCT2019'd;
%LET	Period_End		=	%QSYSFUNC(INTNX(DAY,&Period_Start, 365));
%LET	Increment		=	45;

%MACRO	Calculate_Periods;
	%DO	i	=	1	%TO	999;
		%GLOBAL	Day45_&i;
		%GLOBAL	Nbr_Increments;
		%IF	%SYSFUNC(INTNX(DAY,&Period_Start, %EVAL(&Increment * &i)))	>	&Period_End	%THEN
			%DO;
				%LET	Nbr_Increments	=	%EVAL(&i - 1);
				%LET	i				=	999;
			%END;
		%ELSE
			%DO;
				%LET	Day&Increment._&i		=	%SYSFUNC(INTNX(DAY,&Period_Start, %EVAL(&Increment * &i)));
			%END;
	%END;
%MEND	Calculate_Periods;

%Calculate_Periods;
*%PUT	_USER_;

%MACRO	Generate_SAS_Code;
	%DO	i					=	1	%TO	&Nbr_Increments;
		Day&Increment._&i	=	&&Day&Increment._&i;
	%END;
%MEND	Generate_SAS_Code;

data x;
	FORMAT	Period_Start Day&Increment._1 - Day&Increment._&Nbr_Increments Period_End YYMMDDD10.;
	Period_Start	=	&Period_Start;
	Period_End		=	&Period_End;
	%Generate_SAS_Code;
run;

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 902 views
  • 5 likes
  • 4 in conversation