BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

Appreciate if someone of you help me understand why this code producing the CURR_DATE value as 28MAY1961 instead of 28MAY2021?

 

data test;
CASE_FILL_DATE='29MAY2022'd;
format CURR_DATE CASE_FILL_DATE date9.;
CURR_DATE=CASE_FILL_DATE - INTNX('YEAR',TODAY(),-1);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@David_Billa wrote:

Appreciate if someone of you help me understand why this code producing the CURR_DATE value as 28MAY1961 instead of 28MAY2021?

 

data test;
CASE_FILL_DATE='29MAY2022'd;
format CURR_DATE CASE_FILL_DATE date9.;
CURR_DATE=CASE_FILL_DATE - INTNX('YEAR',TODAY(),-1);
run;

You are calculating the date that is the beginning of last year, so the date 01JAN2021.  You then subtract that from a date that is 29MAY2022.  So the difference is about 1 year and 6 months.  Since SAS numbers days from 01JAn1960 it makes sense that the result is some time in the middle on 1961.

 

If you want to use INTNX() to move back one interval, but not to the beginning of the interval, then make sure to use 'same' as the value for the third argument.

7484  data want;
7485    last_year = intnx('year',today(),-1,'same');
7486    format last_year date9.;
7487    put last_year= ;
7488  run;

last_year=29MAY2021

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

@David_Billa wrote:

Appreciate if someone of you help me understand why this code producing the CURR_DATE value as 28MAY1961 instead of 28MAY2021?

 

data test;
CASE_FILL_DATE='29MAY2022'd;
format CURR_DATE CASE_FILL_DATE date9.;
CURR_DATE=CASE_FILL_DATE - INTNX('YEAR',TODAY(),-1);
run;

You are calculating the date that is the beginning of last year, so the date 01JAN2021.  You then subtract that from a date that is 29MAY2022.  So the difference is about 1 year and 6 months.  Since SAS numbers days from 01JAn1960 it makes sense that the result is some time in the middle on 1961.

 

If you want to use INTNX() to move back one interval, but not to the beginning of the interval, then make sure to use 'same' as the value for the third argument.

7484  data want;
7485    last_year = intnx('year',today(),-1,'same');
7486    format last_year date9.;
7487    put last_year= ;
7488  run;

last_year=29MAY2021
SASKiwi
PROC Star

If you want to move CASE_FILL_DATE back a year put it in INTNX and use the 'SAME' option.

 

data test;
CASE_FILL_DATE='29MAY2022'd;
format CURR_DATE CASE_FILL_DATE Back_one_year date9.;
CURR_DATE=CASE_FILL_DATE - INTNX('YEAR',TODAY(),-1);
Back_one_year=INTNX('YEAR',CASE_FILL_DATE,-1,'SAME');
run;

 

PaigeMiller
Diamond | Level 26

@David_Billa wrote:

Appreciate if someone of you help me understand why this code producing the CURR_DATE value as 28MAY1961 instead of 28MAY2021?

 

data test;
CASE_FILL_DATE='29MAY2022'd;
format CURR_DATE CASE_FILL_DATE date9.;
CURR_DATE=CASE_FILL_DATE - INTNX('YEAR',TODAY(),-1);
run;

You can do some easy debugging ... as follows:

 

data test;
CASE_FILL_DATE='29MAY2022'd;
z=INTNX('YEAR',TODAY(),-1);
CURR_DATE=CASE_FILL_DATE - z;
format z CURR_DATE CASE_FILL_DATE date9.;
run;

 

When you look at the value of Z, you see 01JAN2021, so you are subtracting 28MAY2022 – 01JAN2021 which is a little less than 1.5 years. That doesn't seem to be what you want.

 

If you want an answer of 28MAY2021, there's no subtraction needed, use

 

CURR_DATE = INTNX('YEAR',TODAY(),-1,'s');

 

In essence, the INTNX does the subtraction for you. If your code also does the subtraction, you are doing the subtraction twice, and that is not what you want.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 4903 views
  • 4 likes
  • 5 in conversation