How do you assign the result of a macro function to a macro variable in SAS?
Question
I have a macro that creates a timestamp (to append to output file names). However, rather than have to remember what macro variable the macro assigns the value to, I would prefer to assign a macro variable to the result of the macro (if that isn't circular enough).
%let tms= %tms();
This is the current macro....
%macro tms ;
%* Create a Timestamp ;
%let tms_date= %sysfunc(date(),yymmdd10.) ; %* Todays date ;
%let tms_time= %sysfunc(time(),time.) ; %* Current Time ;
%* Format mmddyyhhmmss ;
%let tms=_%scan(&tms_date,1)%scan(&tms_date,2)%scan(&tms_date,3)%scan(&tms_time,1,:)%scan(&tms_time,2,:)%scan(&tms_time,3,:) ;
%mend ;
How do you quote this to make it work? Also, would I just remove the "%let tms=" from the macro?
Also, would the same quoting work for the following ODS assignment statement?
ods Tagsets.excelxp file="&outname.&tms..xml" style= Styles.XLsansPrinter ;
Thanks for taking the time....
Solution
A macro with a return value is usually called a macro function. Before the mend statment, if you put an expression/value without a semicolon, it will return the value to the caller. If the return value is in an if-else block, the ; would be necessary.
Basically, you can do as you suggest and remove the %let tms= from inside the macro. And yes, I do believe it would work with the ods assignment statment.
%macro tms ;
%* Create a Timestamp ;
%let tms_date= %sysfunc(date(),yymmdd10.) ; %* Todays date ;
%let tms_time= %sysfunc(time(),time.) ; %* Current Time ;
%* Format mmddyyhhmmss ;
_%scan(&tms_date,1)%scan(&tms_date,2)%scan(&tms_date,3)%scan(&tms_time,1,:)%scan(&tms_time,2,:)%scan(&tms_time,3,:)
%mend;
%let tms=%tms;
%put **&tms**;
As another suggestion, you can simplify the code somewhat by using the compress function insead of the %scan functions like this
%sysfunc(compress(_&tms_date.&tms_time,"-:"));