BookmarkSubscribeRSS Feed
Melk
Lapis Lazuli | Level 10

Hello,

I am running a stacked hbar and my data is structured longitudinally:

ID GR TIME  COMP

1    TX    2     X

1     P     3     X

2     TX   6

2     P     3

 

Thus each bar in my figure represents each subject's total duration on study stacked by treatment group. What I would like to do is:

1) Add a symbol to the end of the bar to indicate those in compliance only. I created a new variable COMP that was X if compliance is met and blank if not. I was planning to run DATALABEL=COMP to have that "symbol" show up but it is not working for some reason.. I just ran DATALABEL and it did work, so I am not sure why the = does not. I am using SAS 9.4.

2) Add another "SEGLABEL" to indicate the % of the total time that the subject was taking TX. So for ID 1, it should be 66.7%. How may I do this?

 

proc sgplot data=dat;

     hbar id / response = time group = gr groupdisplay=stack stat=sum datalabel=comp;

run;

 

Thanks to all who help!

2 REPLIES 2
ballardw
Super User

From the documentation for HBAR in Sgplot for Datalabel

DATALABEL <=variable>

displays a label for each bar. If you specify a variable, then the values of that variable are used for the data labels. If you do not specify a variable, then the values of the calculated response are used for the data labels.

Interactions This option has no effect if you also specify the GROUPDISPLAY=STACK option.

If you do STAT=SUM and want a seglabel to show something other than sum you are moving into the world of pre-summarizing your data so you have the values you want in other variables. You may want to consider using summarized data to build an Annotate set which will require you to provide coordinates, ID and Time value to display each of the values you want. One thing about the annotate is that you could do the segment label and the "cap" value in the same the data set.

 

There are other options for providing text to overlay a plot but the TEXT plot is not compatible with HBAR/VBAR.

 

Providing data in the form of a data step that works makes it much easier for us to 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 <> icon or attached as text to show exactly what you have and that we can test code against.

 

 

DanH_sas
SAS Super FREQ

Given your data, there is not really any summarization occurring. Therefore, you could use HBARPARM instead of HBAR. Doing this will give you ability to use a TEXT plot to overlay custom text at the end of each bar segment. Below is an example:

data testdata;
input ID GR $ TIME COMP $ COMPX;
cards;
1    TX    2     X   2
1    P     3     X   5
2    TX    6     .   .
2    P     3     .   .
;
run;

proc sgplot data=testdata;
hbarparm category=ID response=TIME / group=GR groupdisplay=stack seglabel;
text y=ID x=compx text=comp / position=left;
run;

 Hope this helps!

Dan

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 1462 views
  • 0 likes
  • 3 in conversation