@elwayfan446 wrote:
@PaigeMiller no quotes was giving me the error. Double quotes worked as noted in an answer above.
No quotes and no formatting of the macro variables works fine.
Formatting macro variables is not necessary for logic or arithmetic. SAS does all internal logic (such as comparing dates or date/times) on the unformatted values anway. So, to take a simple example, you can create a macro variable &datevar that is formatted as 01JAN2020, and then today()>"&datevar"d is the exact same thing as if the macro variable is unformatted and has value 21915 and then you use today()>&datevar. So there's extra programming work formatting the variable and then you have to un-format it by putting quotes around it and appending a d (for date values) or dt (for date/time values). I recommend you just avoid that extra work and use unformatted macro variables.
@elwayfan446 wrote:
@PaigeMiller no quotes was giving me the error. Double quotes worked as noted in an answer above.
Instead of hitting me with the caps lock, can you explain why variables shouldn't be formatted so I (and others who find this thread) will know in the future?
@PaigeMiller hits you with capslock to get your attention, as you do not seem to actually run the codes we supply for proof.
Once again, see my Maxim 28 (look at my footnotes for the link) for a short explanation.
Keep in mind that resolving macro variables (and macros) only replaces text and does nothing else. Wherever a raw value in code would be sufficient, use it in the macro variable, so you don't have to constantly think about correctly de-formatting a value when you reference the macrovar.
%let mvar = %sysfunc(today());
%put &=mvar;
data _null_;
if &mvar = today()
then put "Yes!";
else put "No!";
run;
The only time I use formatted dates or times in macro variables is when I get them from user prompts. And if I use them repeatedly in the code, I de-format them once at the beginning.
@Kurt_Bremser wrote:
The only time I use formatted dates or times in macro variables is when I get them from user prompts. And if I use them repeatedly in the code, I de-format them once at the beginning.
I think we part ways a bit on this one. In the case of date, time, or datetime values for macrovars, I typically keep them in the form they would otherwise have as literals, i.e.
These expressions are easy for the user to read in any macro-generated code, yet they are also easy to use because they are ready-formatted for use in date, time, or datetime literals, as in:
data want;
set have;
where event_date>="&reference_date"d;
where evt_time>"&start_time"t;
where event_dt>="&begin_point"dt:
run;
To me this is one of the primary benefits of the macro processor being allowed to resolve macro expressions in double quotes, while being prohibited from the same when in single quotes.
I still hold it that the many cases here on the communities, where users get in trouble because of formatted macro variables, strengthen my argument.
I prefer unformatted macro variables as well. My goal is to make the code work with as little effort as possible.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.