BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

If I execute the following code, I could see the result as '2018'

 

data test;
yr=put(year(date()),4.);
run;

However If I trying to turn it into macro as below, it is not producing the desired result.

 

%let yr=put(year(date()),4.);
%put &yr;

I need the macro to derive current year so that I can call that macro to derive the file name and data set name .

8 REPLIES 8
RW9
Diamond | Level 26 RW9
Diamond | Level 26

And why do you need to waste resource putting something already known into a macro variable?

data _null_;
  file "want_%sysfunc(year(date(),4.).txt";
...
data want_%sysfunc(year(date(),4.);
...

I would also suggest that putting data - in this case year - into names is really not a good idea, nor is splitting up same data.

Babloo
Rhodochrosite | Level 12

I'm receiving the following error after executing the code below. Am I missing something?

 

%let curr_year=%sysfunc(year(date()));
%put &curr_year.;

Log:

 


ERROR: Required operator not found in expression: date() 
ERROR: Argument 1 to function YEAR referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC 
       or %QSYSFUNC function reference is terminated.
24         %let curr_year=%sysfunc(year(date()));
25         %put &curr_year.;
.

 

 

 

 

 

Kurt_Bremser
Super User

Two data step functions, two sysfunc's:

%let curr_year=%sysfunc(year(%sysfunc(date())));
%put &curr_year.;

You need to develop a little more creatitvity and audacity in playing around with code along logical lines of thinking. Also see Maxim 4.

 

ballardw
Super User

@Babloo wrote:

I'm receiving the following error after executing the code below. Am I missing something?

 

%let curr_year=%sysfunc(year(date()));
%put &curr_year.;

Log:

 


ERROR: Required operator not found in expression: date() 
ERROR: Argument 1 to function YEAR referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list.  Execution of %SYSCALL statement or %SYSFUNC 
       or %QSYSFUNC function reference is terminated.
24         %let curr_year=%sysfunc(year(date()));
25         %put &curr_year.;
.

 

 

 

 

 


EVERY instance of a data step function requires the use of the %sysfunc function when working in the macro language. Which is why you will find a largish number of folks do complex manipulations in a data step and then create a macro variable if needed. If you search this forum on macro and date you will find some lines of code showing 5 and more %sysfunc() calls. Between nesting complexities, a large number of parentheses and just plain long lines of code these can get hard to read and modify.

s_lassen
Meteorite | Level 14

You can just do it with a single function call, as %sysfunc accepts a format (in this case year4.):

%let yr=%sysfunc(date(),year4.);
PaigeMiller
Diamond | Level 26

@Babloo wrote:

If I execute the following code, I could see the result as '2018'

 

data test;
yr=put(year(date()),4.);
run;

However If I trying to turn it into macro as below, it is not producing the desired result.

 


First, you are trying to turn this into a MACRO VARIABLE, not a MACRO. A macro and a macro variable are not the same thing, so please don't use these two terms interchangeably. But, since you already have your data step code working, then you can create a macro variable right there in the data step:

 

data _null_;
yr=put(year(date()),4.);
call symputx('yr',yr);
run;

But it is completely unnecessary to format this inside a PUT statement.

 

data _null_;
call symputx('yr',year(date()));
run;

 

--
Paige Miller
Tom
Super User Tom
Super User

Do you want a macro? 

%macro year;
%sysfunc(date(),year4)
%mend year;

Or just a macro variable (or symbol)?

%let yr=%sysfunc(date(),year4);

Do you need the current year?  Or just the year that the SAS session started running?  Are you planning to run this on New Year's eve?

%let yr=%substr(&sysdate9,6);

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 8613 views
  • 1 like
  • 7 in conversation