Hi all,
I have a macro where the input variables are the names of the variables from a dataset to be plotted using sgplot.
I want to automate the axis labeling, so that the input variable name can be formatted to a string to be used in xaxis label.
I have made a proc format to convert variable names into axis labels, but not sure what to do with it.
If my explanation doesn't seem clear, here's psuedocode to illustrate what I'm looking for.
%macro my_macro(data, variable_name); %include 'path to variable name formats' /* Some way of using format to turn variable_name into string */ proc sgplot /* Plot stuff */ xaxis label = /* String */ ; run; %mend my_macro
Hope this is clear enough, thanks in advance for your help.
Hi @RoddyJ and welcome to the SAS Support Communities!
Try this simplified example:
proc format;
value $vlblfmt
'age'='Age in years'
'sex'='Gender';
run;
%macro test(var=);
proc sgplot data=sashelp.class;
vbar &var;
xaxis label="%sysfunc(putc(%lowcase(&var),$vlblfmt.))";
run;
%mend test;
%test(var=Age)
%test(var=Sex)
Hi @RoddyJ and welcome to the SAS Support Communities!
Try this simplified example:
proc format;
value $vlblfmt
'age'='Age in years'
'sex'='Gender';
run;
%macro test(var=);
proc sgplot data=sashelp.class;
vbar &var;
xaxis label="%sysfunc(putc(%lowcase(&var),$vlblfmt.))";
run;
%mend test;
%test(var=Age)
%test(var=Sex)
That's exactly what I wanted, thank you so much!
I have a few questions about this snippet of code and would be really grateful if you could answer them too:
"%sysfunc(putc(%lowcase(&var),$vlblfmt.))"
Thanks again for your help.
- Why is it enclosed in quotes, I thought putc would return the format as a string?
The LABEL= option requires quotes according to the documentation. A string is not quoted by default.
- What is %lowcase doing?
The %LOWCASE autocall macro changes uppercase letters such as the "A" in "Age" to lowercase so that the assignment of the labels is case-insensitive. (Without that change "Age" or "AGE" would not be found in the format definition, which in my example uses only lowercase values like "age.")
- What is %sysfunc doing?
%SYSFUNC executes the SAS function in its argument, here: the PUTC function.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.