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;
Thanks to all who help!
From the documentation for HBAR in Sgplot for Datalabel
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.
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!
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.
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.