Hello. I want paired box plots next to each other and the following code is not getting me there. It seems like a simple task but I cannot get it done. There are 12 CAT_#S and 2 values for fy--17 and 16; 3000 rows of data. How can I make this work using macros? Thanks.
CAT_9 | CAT_10 | CAT_11 | CAT_12 | CASES | CASE_HOURS | WebTA_Hours | FYTD_DWPI | FY |
4 | 3 | 14 | 2 | 200 | 1454.81 | 1397.00 | 104.1 | 17 |
1 | 1 | 25 | 2 | 174 | 1217.02 | 1458.75 | 83.4 | 17 |
6 | 0 | 19 | 1 | 193 | 1431.06 | 1753.50 | 81.6 | 17 |
0 | 0 | 26 | 0 | 178 | 1336.35 | 1147.50 | 116.5 | 17 |
4 | 2 | 10 | 2 | 174 | 1363.49 | 1386.75 | 98.3 | 17 |
%macro test;
%do m=1 %to 12;
proc sgplot data=work.dwpi_raw_data;
vbox cat_%m/group=fy;
%end;
%mend;
%test;
run;
@Mbs270 wrote:
This
proc sgplot data=work.dwpi_raw_data;
vbox cat_1/group=fy;
run;
yields exactly what I want. I need to iterate through all variables to get similar box plots next to each other. Running the code as I have it produces nothing. The log says it stopped because of errors but nothing shows up in red.
To get more details of what gets hidden by the macro processor you use the options MPRINT and possibly SYMBOLGEN (if macro variable values seem to be the issue) or MLOGIC (if macro logic %if or %do might be the issues).
I suggest running:
options mprint; %test; options nomprint;
To show all of the code generated by the macro %test; and the error messages generally will appear in conjunction with the problem generated code. Copy from the LOG and paste into a code box opened using the forum's {I} icon to preserve formatting and placement of error messages in relation to code.
If the issue is to get lots of plots "side by side" then you need a different approach as each SGPLOT will create a different plot down the page. The transposed data I suggested and proc SGPANEL using the indicator variable as a PANEL BY variable and setting the rows to one would attempt to place all of the plots on a single row. Each plot would have the value of the panel by variable at the top of the plot section. 12 plots side by side may not fit depending on your current ods graphics settings for width or row and column options on panel by could make a matrix of plots together.
If this were my project I would transpose the data so that I had a single variable CAT, another indicator variable 1 to 12, sort by the indicator and then use something like:
proc sgplot data=work.transposed_data; by indicator; vbox cat/group=fy; run;
One might guest that your cat_1 to cat_12 represent months.
Are you getting any plots? Are any of them as expected? We can't tell what might be missing if you don't tell us.
Also data is best provided as a data step so we can test code. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have.
This
proc sgplot data=work.dwpi_raw_data;
vbox cat_1/group=fy;
run;
yields exactly what I want. I need to iterate through all variables to get similar box plots next to each other. Running the code as I have it produces nothing. The log says it stopped because of errors but nothing shows up in red.
This is getting me closer to what I want but it does not put all of the box plots on the same page. I want them to be next to each other.
ods graphics on;
%macro box;
%do m=1 %to 12;
proc sgplot data=work.dwpi_raw_data;
vbox cat_&m/group=fy;
%end;
%mend;
%box;
run;
ods graphics off;
@Mbs270 wrote:
This
proc sgplot data=work.dwpi_raw_data;
vbox cat_1/group=fy;
run;
yields exactly what I want. I need to iterate through all variables to get similar box plots next to each other. Running the code as I have it produces nothing. The log says it stopped because of errors but nothing shows up in red.
To get more details of what gets hidden by the macro processor you use the options MPRINT and possibly SYMBOLGEN (if macro variable values seem to be the issue) or MLOGIC (if macro logic %if or %do might be the issues).
I suggest running:
options mprint; %test; options nomprint;
To show all of the code generated by the macro %test; and the error messages generally will appear in conjunction with the problem generated code. Copy from the LOG and paste into a code box opened using the forum's {I} icon to preserve formatting and placement of error messages in relation to code.
If the issue is to get lots of plots "side by side" then you need a different approach as each SGPLOT will create a different plot down the page. The transposed data I suggested and proc SGPANEL using the indicator variable as a PANEL BY variable and setting the rows to one would attempt to place all of the plots on a single row. Each plot would have the value of the panel by variable at the top of the plot section. 12 plots side by side may not fit depending on your current ods graphics settings for width or row and column options on panel by could make a matrix of plots together.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: