DATA Step, Macro, Functions and more

%Sysfunc

Reply
Contributor
Posts: 27

%Sysfunc

I am trying to create a macro variable that will contain the prior year and same month based on a current date. For example if the current date is 30MAY2014 I wiould like to get 1305.  However I keep getting an error with the following

:

%let medate=30MAY2014;

%Let Prior_Year=%sysfunc(cats(%substr(putc(%Sysfunc(year("&medate"d)),4.),3,2)-1,putc(%sysfunc(month("&medate"d)),z2.)));

The error I am getting is the following:

ERROR: Required operator not found in expression: putc(5,z2.)

I would really appreciate any suggestions on how I can fix this.

PROC Star
Posts: 1,231

Re: %Sysfunc

39   %let medate=30MAY2014;
40   %put %sysfunc(intnx(month,"&medate"d,-12),yymmn4);
1305
Contributor
Posts: 27

Re: %Sysfunc

Thank you - this is helpful!  However I need for this value to be in a character format.  Any ideas?

Regular Contributor
Posts: 180

Re: %Sysfunc

%let medate=30MAY2014;

%let want=%sysfunc(intnx(month,"&medate"d,-12),yymmn4);

The value of macrovariable &want is already text.

data test;

   char="&want";

   num=&want;

run;

Super User
Super User
Posts: 7,401

Re: %Sysfunc

Well:

%let medate=30MAY2014;
data _null_;
  call symput('prior_year',put(intnx('month',"&medate"d,-12),yymmn4.));
run;

%put &prior_year;

But my question would be, why not just use the calculation where you need it, no need to go creating new variables for it?

Super User
Posts: 6,938

Re: %Sysfunc

Assigning the value to a macro variable causes the intnx (or whatever method one uses) to be executed only once. Putting it into the later data step causes it to be executed with every iteration. The old-hand programmer in me cringes at that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 5 replies
  • 305 views
  • 3 likes
  • 5 in conversation