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.
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 25. Read more here about why you should contribute and what is in it for you!
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.