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

Hello SAS Community,

 

I've been working on a project that queries data each quarter and I wanted to create a macro that will make a column with the current federal quarter and fiscal year column. Essentially this:

mreynaud_0-1692896560167.png

So I started by making macros. At the time of creating them, they were resolving and the columns were created in my dataset. However, I was puzzled to find that I received this warning/error from SAS when revisiting my program this morning: 

mreynaud_1-1692896674710.png

 

Here is the code I used to create the macros:

/* Create quarter and fiscal year macro variable.*/
%macro GetQuarterFromDate();;
	%let date = %sysfunc(today()); /* Get today's date */
    %let quarter = 0;
    %let month = %sysfunc(month(&date));
    
    %if &month <= 3 %then %let quarter = 3;
    %else %if &month <= 6 %then %let quarter = 4;
    %else %if &month <= 9 %then %let quarter = 1;
    %else %if &month <= 12 %then %let quarter = 2;

    %put Quarter &quarter;
%mend;

%GetQuarterFromDate(); 

%macro GetFiscalYearFromDate();
	%let date = %sysfunc(today()); /* Get today's date */

    %let startYear = %eval(%sysfunc(year(&date)) - (%sysfunc(month(&date)) <= 6));
    %let endYear = %eval(&startYear + 1);
    %let fiscalYear = FY &startYear.-&endYear.;
    
    %put &fiscalYear;
%mend;

%GetFiscalYearFromDate();

Here is the code I used when creating a column with the macro as its value in a dataset. I swapped out my original dataset to SASHELP.stocks, if you want to test:

data quar_fisc;
    set sashelp.stocks;

    %GetQuarterFromDate;
    QuarterColumn = catx(' ', "Quarter", &quarter); /* Concatenate text with quarter value */

    %GetFiscalYearFromDate;
    FiscalYearColumn = "&fiscalYear";
run;

data final;
set quar_fisc;
CombinedColumn = catx(', ', QuarterColumn, FiscalYearColumn);
 drop QuarterColumn FiscalYearColumn;
run;

Any help is much appreciated!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
Within the macro you're creating local macro variables. Once they macro is complete, these variables disappear. If you want them to persist after the fact, you can create them as global macro variables.

%global fiscalYear;

Add those types of lines into your code to be able to use the macro variables outside of the macro.

Google Macro Variable Scope for more details.

View solution in original post

3 REPLIES 3
Reeza
Super User
Within the macro you're creating local macro variables. Once they macro is complete, these variables disappear. If you want them to persist after the fact, you can create them as global macro variables.

%global fiscalYear;

Add those types of lines into your code to be able to use the macro variables outside of the macro.

Google Macro Variable Scope for more details.
Tom
Super User Tom
Super User

You seem still a little confused about how macro processing works.  It makes no sense to call those   %GetQuarterFromDate and  %GetFiscalYearFromDate macros in the middle a data step.  Since the only thing they generate is macro variable values they will completed processes before the data step start running any way. You should call them before you start writing the data step.  Now the order of the lines in the program file will more closely match the order that they will be executed.

 

And if you want the value of QUARTER and FISCALYEAR to be available after the macros have finished running you need to have created those macro variables before the macros are called.  Otherwise the default action of making unknown new macro variables as LOCAL will apply and they will disappear when the macro call ends.

 

%let quarter=;
%let fiscalyear=;
%GetQuarterFromDate;
%GetFiscalYearFromDate;
 
data quar_fisc;
    set sashelp.stocks;

   QuarterColumn = catx(' ', "Quarter", &quarter); /* Concatenate text with quarter value */

   FiscalYearColumn = "&fiscalYear";
run;
Quentin
Super User

Reeza answered your main question.  As an aside, to calculate the quarter instead of using a macro, you could use a single macro expression, like:

%put The quarter for today is: %sysfunc(intnx(quarter,%sysfunc(today()),2),qtr) ;

That is basically "calculate the quarter of the quarter that is two quarters ahead of today."

 

In your DATA step, you could have the assignment statement be:

QuarterColumn = "Quarter %sysfunc(intnx(quarter,%sysfunc(today()),2),qtr)" ;
The Boston Area SAS Users Group (BASUG) is hosting our in person SAS Blowout on Oct 18!
This full-day event in Cambridge, Mass features four presenters from SAS, presenting on a range of SAS 9 programming topics. Pre-registration by Oct 15 is required.
Full details and registration info at https://www.basug.org/events.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 573 views
  • 1 like
  • 4 in conversation