Cahn Ex
Cahn Ex

Reputation: 61

Using intnx to get the same date last year in SAS

I want to pull the same date one year before my variable 'day_3_ago'

My code:

%macro date; 
data _null_;
call symput ('day_3_ago', put(intnx('day',today(),-3, 'b'),yymmddn8.));
run;
%mend;

%date;

%let last_year_3dayago = [1 year before day_3_ago]

For example, if I ran this code today, day_3_ago would be 20170624. I would want last_year_3dayago to be 20160624

My current solution is:

%let last_year_3daygo = %sysfunc(intnx(day,%sysfunc(inputn(&day_3)ago.,yymmdd8)),-365),yymmddn8);

but that doesn't account for leap years.

Upvotes: 1

Views: 3104

Answers (2)

zuluk
zuluk

Reputation: 1577

I modified Longfish's code to solve your requirement with the variable last_year_3dayago:

%let last_year_3dayago = 20170624;
data _null_;
  x = intnx('year',input("&last_year_3dayago.",b8601da.),-1,'s');
  putlog x :date9.;
run;

Upvotes: 0

Longfish
Longfish

Reputation: 7602

Instead of using intnx with day, use year and end the function with s (or same) instead of b.

data _null_;
x = intnx('year',today()-3,-1,'s');
putlog x :date9.;
run;

Upvotes: 2

Related Questions