Hello. I wonder if there is anyway I can format a macro variable in this case.
proc iml;
Time=datetime();
submit Time;
%put Time=&Time;
endsubmit;
print Time[format=datetime20.];
quit;
It seems the variable Time is formatted well with print but not with %put.
There is no %putn macro function, the data step function needs to be wrapped with %sysfunc:
%let time = %sysfunc(datetime());
%let time = %sysfunc(putn(&time, datetime20));
%put time=&time;
Log:
24 %let time = %sysfunc(datetime()); 25 %let time = %sysfunc(putn(&time, datetime20)); 26 %put time=&time; time=07DEC2017:09:31:56
Not directly, but indirectly is easy:
%let time2 = %putn(&time, datetime20);
%put Time2=&time2;
Ah, that's right. The macro statements are not part of the PROC. You could try it this way:
%let time= %sysfunc(datetime(), datetime20.);
%put time=&time;
I can't test it until tomorrow, but give it a shot and we'll see what happens.
This does not work as well. Must I consider another way rather than the submit-endsubmit sentences?
Entirely separate from IML, this should be workable. I'm splitting it out in steps, since I can't test it myself so this might help isolate where things break down (if they do break down):
%let time = %sysfunc(datetime());
%let time = %putn(&time, datetime20);
%put time=&time;
There is no %putn macro function, the data step function needs to be wrapped with %sysfunc:
%let time = %sysfunc(datetime());
%let time = %sysfunc(putn(&time, datetime20));
%put time=&time;
Log:
24 %let time = %sysfunc(datetime()); 25 %let time = %sysfunc(putn(&time, datetime20)); 26 %put time=&time; time=07DEC2017:09:31:56
Thanks. It should have been putn rather than %putn.
proc iml;
Time=datetime();
submit Time;
%put Time=%sysfunc(putn(&Time,datetime20.));
endsubmit;
quit;
This solves everything.
@Junyong wrote:
Hello. I wonder if there is anyway I can format a macro variable in this case.
proc iml; Time=datetime(); submit Time; %put Time=&Time; endsubmit; print Time[format=datetime20.]; quit;
It seems the variable Time is formatted well with print but not with %put.
Usual miscomprehension about the macro facility. The macro PREprocessor evaluates the %put Statement before proc iml runs (while the code is being fetched), and will therefore have no access to the IML variable. It will look for a previously defined macro variable time, which most probably isn't there at all.
But it seems MACRO can read IML variables with submit-endsubmit sentences.
proc iml;
do IMLVariable=1 to 30;
submit IMLVariable;
%put &IMLVariable;
endsubmit;
end;
quit;
No MACRO variable exists here.
You are right. I'm not well versed with proc iml yet. IML supplies its variables as macro variables to the SAS processor when it executes a submit block.
You seem to have solved the formatting problem, so that's good.
I would like to point out a conceptual problem in this thread. As discussed in the SAS/IML documentation, NO MACRO VARIABLE is involved in this use of the SUBMIT block. The doc says:
SAS® Help Center: Passing Parameters from SAS/IML Matrices
You can list the names of SAS/IML matrices in the SUBMIT statement and refer to the contents of those matrices inside the SUBMIT block. The syntax is reminiscent of the syntax for macro variables: an ampersand (&) preceding an expression means "substitute the value of the expression." However, the substitution takes place before the SUBMIT block is sent to the SAS System; no macro variables are actually created.
For more about parameter substitution and SUBMIT blocks, see
"Passing values from PROC IML into SAS procedures"
or watch the video
"Calling SAS procedures from the SAS/IML language"
PS. Another way to format a value is to use the PUT statement in a DATA step:
submit Time;
data _null_;
Time = &Time;
put Time datetime20.;
run;
endsubmit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.