BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mbs270
Obsidian | Level 7

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_9CAT_10CAT_11CAT_12CASESCASE_HOURSWebTA_HoursFYTD_DWPIFY
431422001454.811397.00104.117
112521741217.021458.7583.417
601911931431.061753.5081.617
002601781336.351147.50116.517
421021741363.491386.7598.317

 

%macro test;

%do m=1 %to 12;

proc sgplot data=work.dwpi_raw_data;

vbox cat_%m/group=fy;

%end;

%mend;

%test;

run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@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.

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

 

Mbs270
Obsidian | Level 7

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.

 

 
Mbs270
Obsidian | Level 7

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;

 

ballardw
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4521 views
  • 2 likes
  • 2 in conversation