BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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
4 REPLIES 4
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
deleted_user
Not applicable
Thanks, I read the section you pointed to and was able to create a global variable yesterday. Thanks again!
Cynthia_sas
SAS Super FREQ
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
deleted_user
Not applicable
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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 4 replies
  • 694 views
  • 0 likes
  • 3 in conversation