DATA Step, Macro, Functions and more

Create Marco variable when it's Monday skip back 3 day else skip 1 day.

Reply
N/A
Posts: 0

Create Marco variable when it's Monday skip back 3 day else skip 1 day.

I am not sure what is not working with the following code. I am trying to write a macro variable (yesterday) that jumps back to Friday (3 days) when it is Monday, otherwise the Macro variable should jump back one day. Take a look --


%LET today=%SYSFUNC(DATE());
%LET DAY=%SYSFUNC(PUTN(&today,WEEKDAY.));

%MACRO Yester;
DATA _NULL_;
%IF %EVAL(&day=2)
%THEN %LET yesterday=%EVAL ((&today*1)-3);
%ELSE %LET yesterday=%EVAL ((&today*1)-1);
run;
%MEND Yester;

%Yester;

%PUT Today: &today Yesterday: &yesterday Day: &Day; The %IF %EVAL(&day-2) is the way the program was written, just a bad typo in the discussion forum posting.


Message was edited by: dml
Super Contributor
Super Contributor
Posts: 3,174

Re: Create Marco variable when it's Monday skip back 3 day else skip 1 day.

The DATA step has no purpose for your code excerpt, and can be eliminated.

Also, this type of use of SAS macro variables involves variable "scope", that being either local or global.

Scott Barry
SBBWorks, Inc.

SAS 9.2 Macro Language: Reference - Scopes of Macro Variables
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047080.htm
N/A
Posts: 0

Re: Create Marco variable when it's Monday skip back 3 day else skip 1 day.

Thanks, I read the section you pointed to and was able to create a global variable yesterday. Thanks again!
SAS Super FREQ
Posts: 8,739

Re: Create Marco variable when it's Monday skip back 3 day else skip 1 day.

Hi:
The other issue I see with your code (beyond what Scott has noted with scope issues and the fact that you don't need a DATA _NULL_ step at all), is that &TODAY will hold today's date, but &DAY will hold the result of using the WEEKDAY format.

You can test the values of these 2 macro variables by doing this:
[pre]
%LET today=%SYSFUNC(DATE());
%LET DAY=%SYSFUNC(PUTN(&today,WEEKDAY.));

%put today= &today;
%put day= &day;
[/pre]

It seems to me that if you test &TODAY, you will be testing a date value and NOT the day of the week. So, scope issues aside, you might want to consider testing &DAY and not &TODAY. You can take care of some of the scope issues by using an explicit %GLOBAL statement.

Since the %IF must be used in a Macro program, you will still need the macro program definition for %YESTER. However you do NOT need the DATA _NULL_ and you must still resolve the scope issues if you plan/hope to use &YESTERDAY outside the scope of the local macro program environment.

cynthia
N/A
Posts: 0

Re: Create Marco variable when it's Monday skip back 3 day else skip 1 day.

Thanks for the info. Sorry I had a typo in the posting. The actual code referenced &day in the %IF condition.

It is a scope problem. I need/want to make &yesterday global.

Thanks again.
Ask a Question
Discussion stats
  • 4 replies
  • 141 views
  • 0 likes
  • 3 in conversation