Statistical programming, matrix languages, and more

Formatting a Macro Variable with Put?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Formatting a Macro Variable with Put?

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.


Accepted Solutions
Solution
‎12-07-2017 05:06 PM
Super User
Posts: 10,577

Re: Formatting a Macro Variable 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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 6,934

Re: Formatting a Macro Variable with Put?

Not directly, but indirectly is easy:

 

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

%put Time2=&time2;

Occasional Contributor
Posts: 19

Re: Formatting a Macro Variable with Put?

Posted in reply to Astounding
Thanks, but it seems it is not working inside the submit-endsubmit sentences.
Super User
Posts: 6,934

Re: Formatting a Macro Variable with Put?

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.

Occasional Contributor
Posts: 19

Re: Formatting a Macro Variable with Put?

Posted in reply to Astounding

171206.png

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

Super User
Posts: 6,934

Re: Formatting a Macro Variable with Put?

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;

Occasional Contributor
Posts: 19

Re: Formatting a Macro Variable with Put?

Posted in reply to Astounding
Sorry but this is not successful too. This prints time=%putn(numbers,datetime20.) instead.
Solution
‎12-07-2017 05:06 PM
Super User
Posts: 10,577

Re: Formatting a Macro Variable 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
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

Re: Formatting a Macro Variable with Put?

Posted in reply to KurtBremser

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.

Super User
Posts: 10,577

Re: Formatting a Macro Variable with Put?


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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

Re: Formatting a Macro Variable with Put?

Posted in reply to KurtBremser

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.

Super User
Posts: 10,577

Re: Formatting a Macro Variable with Put?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
SAS Super FREQ
Posts: 4,275

Re: Formatting a Macro Variable with Put?

Posted in reply to KurtBremser

 

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"

 

Super User
Posts: 10,577

Re: Formatting a Macro Variable with Put?

Ah, that's like it was in SAS SCL.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
SAS Super FREQ
Posts: 4,275

Re: Formatting a Macro Variable with Put?

Posted in reply to KurtBremser

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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 272 views
  • 4 likes
  • 4 in conversation