BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BonnaryW
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
Reeza
Super User
Do have non macro working code? If so, post that first and also please take the time to explain what the issue is with the code above.

If you don't that's usually where I recommend you start. In addition, it's clear that this is only part of your code.
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
BonnaryW
Obsidian | Level 7

It's Worked !! Thank you very much for simplify my codes and the explanations.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 935 views
  • 0 likes
  • 3 in conversation