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

>> I originally posted this on the ODS board and was recommended to repost this here.

I hope I'm not missing an easy answer, but it's not for lack of diligence. I'm trying to use GTL to define a fairly basic horizontal bar chart. For the life of me I can't find an option to allow me to sort the bars by frequency. Is there an equivalent GTL option to CATEGORYORDER=RESPDESC? I suspect there is since this is would be a common need.

Bob

proc template;

define statgraph FlagBarCharts;

    dynamic byvar;

    begingraph;

        entrytitle "Flag Variable:  "  byvar ;

        layout overlay

                /     border=false

                    WALLDISPLAY=none

                    xaxisopts=(

                        label=' '

                        linearopts=(viewmax=10));

            BARCHARTPARM x=subcat y=percent

                / orient=horizontal;

        endlayout;

    endgraph;

end;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Jay54
Meteorite | Level 14

Such an option is coming soon.  In the meantime, you can do this is by getting the frequencies using PROC FREQ, sort as needed, then use the SGPLOT HBAR or GTL BARCHART to draw the bars.  GTL will retain data order by default.  For SGPLOT will order your categories by default.  You have to set XAXIS DISCRETEORDER to DATA.

View solution in original post

5 REPLIES 5
Rick_SAS
SAS Super FREQ

From the doc: "By default, if the category is character, the bars in the chart appear in the order in which the category values are present in the input data."

bob_pearson
Calcite | Level 5

If it's termed the default behavior, does that mean that there are options? ...or is that what BARCHART always does? If so then the user must manually sort the data. It seems as though that would be more easily handled at graph time, moreso for BARCHART than BARCHARTPARM, and the option would be frequently needed.

Bob

Jay54
Meteorite | Level 14

Such an option is coming soon.  In the meantime, you can do this is by getting the frequencies using PROC FREQ, sort as needed, then use the SGPLOT HBAR or GTL BARCHART to draw the bars.  GTL will retain data order by default.  For SGPLOT will order your categories by default.  You have to set XAXIS DISCRETEORDER to DATA.

bob_pearson
Calcite | Level 5

Just saw your post Sanjay after I replied. Thanks

Quentin
PROC Star

Hi  Bob,

Copying my response from the original thread, did you try it and get different results that you wanted?

I saw your response that the linearopts applied to the response axis.  But since your BARCHARTPARM was x=subcat, I think xaxisopts applies to the x axis, which in this case is the vertical axis because it's a horizontal plot.  When I ran the code with linearopts removed from the xaxisopts, and then sorted the data, it seemed to give the result you wanted (consistent with Sanjay's note that GTL would user data order by default).

My original response:

I'm new to GTL, but I think if you remove the linearopts= option, then the categories will be ordered according to the order of the input dataset.  I guess when you added linearopts=, it forced the xaxis to be linear, instead of categorical.

proc template;
define statgraph FlagBarCharts;
    dynamic byvar;
    begingraph;
        entrytitle "Flag Variable:  "  byvar ;
        layout overlay 
                /     border=false 
                    WALLDISPLAY=none
                    xaxisopts=(
                        label=' ' 
                         );
            BARCHARTPARM x=subcat y=percent 
                / orient=horizontal ;
        endlayout;
    endgraph;
end;

run;

data in;
  subcat='1'; percent=2; output;
    subcat='2'; percent=6; output;
    subcat='3'; percent=4;output;
run;

proc sort data=in;
  by percent;
run;

ods pdf file="d:\junk\me.pdf";
proc sgrender data=in template=FlagBarcharts;
run;
ods pdf close;
Check out the Boston Area SAS Users Group (BASUG) video archives: https://www.basug.org/videos.

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1306 views
  • 0 likes
  • 4 in conversation