BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Kurt_Bremser
Super User

@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;

 

 

elwayfan446
Barite | Level 11
Perfect. Thank you for the explanation. I had always done it in the macro with no issues but will move away from that going forward.


Kurt_Bremser
Super User

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.

mkeintz
PROC Star

@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.


@Kurt_Bremser :

 

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.

  • %let reference_date=06jun2019;
      or
  • %let start_time= 23:01:35.356;
      or
  • %let begin_point= &reference_date.:&start_time;
      or
    %let begin_point=06jun2019:23:01:35.356;

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Kurt_Bremser
Super User

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.

PaigeMiller
Diamond | Level 26

I prefer unformatted macro variables as well. My goal is to make the code work with as little effort as possible.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 21 replies
  • 1831 views
  • 2 likes
  • 5 in conversation