Hello,
I am trying to extract current/previous year and use it in my file name. I want to extract current year (2018) if not January and extract previous year (2017) if it's January.
data _NULL_;
data _null_;
%let testdate=01JANE2018;
%let cmons=%sysfunc(month("&testdate"d));
if &cmons = 1 then;
%let syear= %sysfunc(year(%sysfunc(intnx(year,%SYSFUNC("&testdate"d)),-1))));
else;
%let syear= %sysfunc(year("&testdate"d));
run;
%put &cmons &syear;
ods _all_ close;
ods tagsets.excelxp
file="c:\Reports\me_Q%sysfunc(intnx(qtr,%sysfunc(date()),-1), qtr.)_&syear..xls"
style=sasweb;
ods tagsets.excelxp options(sheet_name="me_Q%sysfunc(intnx(qtr,%sysfunc(date()),-1), qtr.)_&syear");
Thank you for your help.
Imagine fiscal years that start in Feb and end in Jan. Then you would probably label that year the same as the calendar year containing february. SAS has such yearly intervals. Specificaly "year.2" as in
yearname=year(intnx("year.2"),datadate,0,"begin");
The interval "year.2" is a year beginning FEB and ending JAN. Adding 0 such years to your date, and then aligning the result to the beginning of the interval produces a date value of 01FEBxxxx. The year function extracts the xxxx.
Embedding this in a series of %sysfunc:
%let testdate=01jan2018;
%let syear=%sysfunc(year(%sysfunc(intnx(year.2,%sysfunc(inputn(&testdate,date9.)),0,begin))));
%put &=syear;
Imagine fiscal years that start in Feb and end in Jan. Then you would probably label that year the same as the calendar year containing february. SAS has such yearly intervals. Specificaly "year.2" as in
yearname=year(intnx("year.2"),datadate,0,"begin");
The interval "year.2" is a year beginning FEB and ending JAN. Adding 0 such years to your date, and then aligning the result to the beginning of the interval produces a date value of 01FEBxxxx. The year function extracts the xxxx.
Embedding this in a series of %sysfunc:
%let testdate=01jan2018;
%let syear=%sysfunc(year(%sysfunc(intnx(year.2,%sysfunc(inputn(&testdate,date9.)),0,begin))));
%put &=syear;
It's Worked !! Thank you very much for simplify my codes and the explanations.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.