DATA Step, Macro, Functions and more

macro variable dates

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

macro variable dates

Hello SAS users,

I am doing a condition check here, if the code run day is sunday i want to take the sunday dt and output in  'm/dd/yyyy' format. If it is not sunday I want to take the run date and do a day -1 and output in  'mm/dd/yyyy' as well.

I am getting the below error and not sure what I am doing wrong.

ERROR: Expected close parenthesis after macro function invocation not found.

ERROR: The function PUTN referenced by the %SYSFUNC or %QSYSFUNC macro function has too few

Would anyone have any idea?

%Macro c;
Data _null_;

%If %sysfunc(weekday(%sysfunc(today()))) = 1 %then %Let dt = %str(%')%sysfunc(putn(%sysfunc(today()),mmddyys10.))%str(%'); 
%else %Let dt = %str(%')%sysfunc(putn(%sysfunc(today(),-1),mmddyys10.))%str(%');
Run;

%Put dt= &dt.;
%Mend c;
%c;

arguments.


Accepted Solutions
Solution
‎03-31-2012 04:50 PM
Super User
Super User
Posts: 6,502

Re: macro variable dates

Don't need a macro either as there is no macro logic involved.

You can make the nested %SYSFUNC() calls easier by first getting the numeric version of today's date.

%let dt=%sysfunc(today());

%let dt=%str(%')%sysfunc(putn(&dt-(1=%sysfunc(weekday(&dt))),mmddyys10.))%str(%');

Note the %EVAL is not required in the arguments passed to the function that %SYSFUNC() is calling.  The function can evaluate expressions just as it does when called in a data step.

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: macro variable dates

If you are running a data step they why are you bothering with macro statements?

%Macro c;
Data _null_;

%If %sysfunc(weekday(%sysfunc(today()))) = 1 %then %Let dt = %str(%')%sysfunc(putn(%sysfunc(today()),mmddyys10.))%str(%'); 
%else %Let dt = %str(%')%sysfunc(putn(%sysfunc(today(),-1),mmddyys10.))%str(%');
Run;

%Put dt= &dt.;
%Mend c;
%c;

data _null_;

  call symputx('dt',"'"||put(today()-(1 = weekday(today())),mmddyys10.)||"'");

run;

Respected Advisor
Posts: 3,124

Re: macro variable dates

I second Tom. On the other hand, you don't need data step to make it happen either:

%Macro c;

/*Data _null_;*/

%If %sysfunc(weekday(%sysfunc(today()))) = 1 %then %Let dt = %str(%')%sysfunc(putn(%sysfunc(today()),mmddyy10.))%str(%');

%else %Let dt = %str(%')%sysfunc(putn(%eval(%sysfunc(today())-1),mmddyy10.))%str(%');

/*Run; */

%Put dt= &dt.;

%Mend c;

%c

The only thing was missing is %eval.

Haikuo

Solution
‎03-31-2012 04:50 PM
Super User
Super User
Posts: 6,502

Re: macro variable dates

Don't need a macro either as there is no macro logic involved.

You can make the nested %SYSFUNC() calls easier by first getting the numeric version of today's date.

%let dt=%sysfunc(today());

%let dt=%str(%')%sysfunc(putn(&dt-(1=%sysfunc(weekday(&dt))),mmddyys10.))%str(%');

Note the %EVAL is not required in the arguments passed to the function that %SYSFUNC() is calling.  The function can evaluate expressions just as it does when called in a data step.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1616 views
  • 5 likes
  • 3 in conversation