Data visualization with SAS programming

How do we change order of a categorical variable for sgpanel?

Reply
Contributor gsk
Contributor
Posts: 23

How do we change order of a categorical variable for sgpanel?

For x-axis, I want to correct the order to: Week 0, Week 8, Week 16, Week 24, Week 32 instead of otherwise. 

 

how to change order code.JPGchange order graph.JPG

Super User
Posts: 22,820

Re: How do we change order of a categorical variable for sgpanel?

Create a numerical variable with the value of week and then use a format to have it appear as WEEK 8 etc. 

 

Note that because its a character variable its sorting alphabetically, which is the correct approach. 

Contributor gsk
Contributor
Posts: 23

Re: How do we change order of a categorical variable for sgpanel?

I tried this but it's still not working:


proc format;
value week_sorted
2 = "Week 8"
3 = "Week 16"
4 = "Week 24"
5 = "Week 32";
run;

proc sgpanel data = percents;
panelby VSTEST / columns=2;
format visitnum week_sorted.;
vbar visitnum / group= arm groupdisplay=cluster response=percent;
where responder='Y';

colaxis label = "Study Visit" DISCRETEORDER=data;
rowaxis label = "Percent of Subjects with >=5 mmHg BP Decrease";
label arm = "Treatment Group";
title 'Percentage of Treatment Responders by Visit and Treatment Group';
run;

 

Any idea? 

Contributor gsk
Contributor
Posts: 23

Re: How do we change order of a categorical variable for sgpanel?

It worked when I sorted variables in different order:

proc sort data = visit_bp out = visit_bp_sorted;
by vstest arm visitnum visit;
run;

proc freq data= visit_bp_sorted noprint;
by vstest arm visitnum visit;
table responder / nocum out = percents;
run;
Do you know why it didn't work with
by visit vstest arm visitnum;
but worked with
by vstest arm visitnum visit;
Super User
Posts: 13,004

Re: How do we change order of a categorical variable for sgpanel?

Or make sure the values will sort correctly for character: "Week 8" is after "Week 32" because 8 comes after 3. So use "Week 08", then the 0 comes before the 1 in "Week 16".

SAS Super FREQ
Posts: 1,263

Re: How do we change order of a categorical variable for sgpanel?

With ODS Graphics Procedures, there are often multiple ways to achieve the result.  In addition to the previous suggestions, you could also put the values in the order you want in your data, and use DISCRETEORDER=data on the COLAXIS.

Contributor gsk
Contributor
Posts: 23

Re: How do we change order of a categorical variable for sgpanel?

Posted in reply to Sanjay_SAS

(I slightly modified my code) 

I added DISCRETEORDER=data and it doesn't do anything.. What do you mean by "put the values in the order you want in your data"..?

I sorted by visit (Week 0, and so on) in proc sort and proc freq steps. 


proc sgpanel data = percents;
panelby VSTEST / columns=2;
vbar visit / group= arm groupdisplay=cluster response=percent;
colaxis display=(nolabel) DISCRETEORDER=data;
where responder='Y';
title 'Percentage of Treatment Responders by Visit and Treatment Group';
run;

SAS Super FREQ
Posts: 1,263

Re: How do we change order of a categorical variable for sgpanel?

Attach your sample (non-confidential) data so we can run the program to see what is going on.  It is hard to guess what is going on without your data.  

Super User
Posts: 13,004

Re: How do we change order of a categorical variable for sgpanel?


gsk wrote:

(I slightly modified my code) 

I added DISCRETEORDER=data and it doesn't do anything.. What do you mean by "put the values in the order you want in your data"..?

I sorted by visit (Week 0, and so on) in proc sort and proc freq steps. 


proc sgpanel data = percents;
panelby VSTEST / columns=2;
vbar visit / group= arm groupdisplay=cluster response=percent;
colaxis display=(nolabel) DISCRETEORDER=data;
where responder='Y';
title 'Percentage of Treatment Responders by Visit and Treatment Group';
run;


If you have "Week 8" proc sort unless you use the correct linguistic options will place 'Week 8' after 'Week 16' 'Week 24' and 'Week 36'.

Show us the Proc Sort.

Ask a Question
Discussion stats
  • 8 replies
  • 363 views
  • 7 likes
  • 4 in conversation