Would you like to format your macro variables?
Easy! Just use the %FORMAT function, like this:
What?! You never heard of the %FORMAT function?
Of course not, cuz it doesn't exist! No problem.
Just create it, like this:
%macro format(value,format); %if %datatyp(&value)=CHAR %then %sysfunc(putc(&value,&format)); %else %left(%qsysfunc(putn(&value,&format))); %mend format;
The %FORMAT function also accepts user-defined formats, like this:
The PROC SQL step below creates the macro variable HIGHSALARY, which is referenced in the TITLE statement and formatted with the user-defined %FORMAT function.
proc sql noprint; select 5*avg(salary) into :highsalary from orion.staff; reset print; title "Salaries over %format(&highsalary,dollar11.2)"; select employee_ID, salary from orion.staff where salary > &highsalary order by salary desc; quit;
Be sure to save your user-defined %FORMAT function in your stored compiled macro or autocall library for convenient re-use!
If you enjoyed this tip on how to format a macro variable in SAS, and want more like it, consider taking our SAS Macro Language 1: Essentials course and/or our SAS Macro Language 2: Advanced Techniques, offered in the classroom, as Live Web classes, or as self-paced e-Learning. These courses are part of our SAS Programming curriculum.
You can also find more programming tips here. And, check back often. New tips, from a variety of SAS blogs, are added regularly!