DATA Step, Macro, Functions and more

explanation

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 87
Accepted Solution

explanation


Hi, I came across a wierd situation. Can someone explain me this?

One of our prod jobs is using %let statement as below and it works

%let rundt = today();

however, when i run the same &rundt resolves to today() and not actual date of today.

I've to use %let rundt = %sysfunc(today());

why is it that it works without %sysfunc in other jobs?


Accepted Solutions
Solution
‎10-21-2013 01:36 PM
Super User
Posts: 5,516

Re: explanation

The statements do the same thing, whether it is in your job or in a production job.  Whenever you see this statement:

%let rundt = today();

the value of &RUNDT is not a number, but is the 7 characters "today()".

What you are not observing is how the production program uses &RUNDT.  For example, it may use this statement in a DATA step:

date = &RUNDT;

In that case, the statement resolves into:

date = today();

When that statement appears in a DATA step, it executes and DATE receives the proper numeric value.

View solution in original post


All Replies
Solution
‎10-21-2013 01:36 PM
Super User
Posts: 5,516

Re: explanation

The statements do the same thing, whether it is in your job or in a production job.  Whenever you see this statement:

%let rundt = today();

the value of &RUNDT is not a number, but is the 7 characters "today()".

What you are not observing is how the production program uses &RUNDT.  For example, it may use this statement in a DATA step:

date = &RUNDT;

In that case, the statement resolves into:

date = today();

When that statement appears in a DATA step, it executes and DATE receives the proper numeric value.

Frequent Contributor
Posts: 87

Re: explanation

Posted in reply to Astounding

Thank you!

One more question

Why am i not able to use below statement?

%let month1x = %sysfunc(month(today()));

where as, I'm able to use

data _null_ ;

call symput('month1x',month(today()));

run;

Super User
Posts: 5,516

Re: explanation

You'll need to use %SYSFUNC for each function ... once for MONTH and once for TODAY.  I hope I get the number of parentheses correct:

%let month1x = %sysfunc(month(%sysfunc(today())));

Frequent Contributor
Posts: 87

Re: explanation

Posted in reply to Astounding

Thank you!

I was able to get same results using both ways below. However, I had to use  ' ' quotes around month in intnx function for call symput and not for %sysfunc.

Can you tell me the reason why?

  %let month2 = %sysfunc(month(%sysfunc(intnx(month,%sysfunc(today()),-1))));
  %put &month2;

   data _null_;
  call symput ('month1',month(intnx('month',today(),-1)));
  run;
  %put &month1;

Super User
Posts: 5,516

Re: explanation

Except in rare cases where a number is required, macro language assumes all strings are text.  Easy examples:

%let name=Fred;

%let name="Fred";

The first statement assigns &NAME a 4-character value.  The second assigns &NAME a 6-character value.  The quotes are not needed to identify a string, and so become part of the value assigned to &NAME.

You're looking at the same principle, but in a more complex setting.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 278 views
  • 8 likes
  • 2 in conversation