Seems simple enough – An hour later and various attempts, no further forward, so bow to the experts:-
Data _Null_;
call symputx('C_Time',%sysfunc(time(),timeampm.));
%put The current time is &=C_Time;
%put The Non-macro time is %sysfunc(time(),timeampm.);
Run;
Excerpt from the Log
33 ! Data _Null_;
34 call symputx('C_Time',%sysfunc(time(),timeampm.));
NOTE: Line generated by the macro function "SYSFUNC".
34 11:43:04 AM
_
388
200
ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.
35 %put The current time is &=C_Time;
WARNING: Apparent symbolic reference C_TIME not resolved.
The current time is C_Time
36 %put The Non-macro time is %sysfunc(time(),timeampm.);
The Non-macro time is 11:43:04 AM
37 Run;Even if I wrap the second argument with a Put to specifically convert Numeric to Character – same result.
What is the answer?
The second argument to the call symput routine must be a string, so use double quotes.
And move the %put statements after the run, as they would otherwise be resolved before the data step runs, and throw an error.
Data _Null_;
call symputx('C_Time',"%sysfunc(time(),timeampm.)");
Run;
%put The current time is &=C_Time;
%put The Non-macro time is %sysfunc(time(),timeampm.);
The second argument to the call symput routine must be a string, so use double quotes.
And move the %put statements after the run, as they would otherwise be resolved before the data step runs, and throw an error.
Data _Null_;
call symputx('C_Time',"%sysfunc(time(),timeampm.)");
Run;
%put The current time is &=C_Time;
%put The Non-macro time is %sysfunc(time(),timeampm.);
If you are inside a macro, then using
%let C_Time = %sysfunc(time(),timeampm.);
should solve the problem. If you want to use a data step, replacing %sysfunc with
call symputx('C_Time', put(time(),timeampm.));
should be all you need.
You seem a little confused about what the macro processor does and when it does it. It modifies the code and passes on to SAS to execute. So your %PUT statements in the middle of a data step will execute BEFORE the data step executes.
You could either use macro code to make your macro variable.
%let C_Time=%sysfunc(time(),timeampm.);
%put The current time is &=C_Time;
Or use SAS code to make the macro variable.
data _null_;
now=time();
put 'The Non-macro time is ' now timeampm.;
call symputx('C_Time',put(now,timeampm.));
run;
If you did want to use the value of the macro variable to generate some SAS code them make sure it is generating valid SAS code.
data _null_;
put "The value of C_TIME is &c_time".
run;
I'm slowly completing the Advanced SAS training and trying to work things out in my head as to what works and why - Thanks for the input
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.
Ready to level-up your skills? Choose your own adventure.