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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

14 REPLIES 14
Astounding
PROC Star

Not directly, but indirectly is easy:

 

%let time2 = %putn(&time, datetime20);

%put Time2=&time2;

Junyong
Pyrite | Level 9
Thanks, but it seems it is not working inside the submit-endsubmit sentences.
Astounding
PROC Star

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.

Junyong
Pyrite | Level 9

171206.png

This does not work as well. Must I consider another way rather than the submit-endsubmit sentences?

Astounding
PROC Star

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;

Junyong
Pyrite | Level 9
Sorry but this is not successful too. This prints time=%putn(numbers,datetime20.) instead.
Kurt_Bremser
Super User

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
Junyong
Pyrite | Level 9

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.

Kurt_Bremser
Super User

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

Junyong
Pyrite | Level 9

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.

Kurt_Bremser
Super User

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.

Rick_SAS
SAS Super FREQ

 

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"

 

Rick_SAS
SAS Super FREQ

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 14 replies
  • 16502 views
  • 4 likes
  • 4 in conversation