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;
@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
@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
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;
@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.
you may find this paper useful: https://support.sas.com/resources/papers/proceedings/proceedings/sugi31/027-31.pdf
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.