Hello,
I have been having troubles with density plots in sgplot/sgpanel. My dataset has 4 subgroups and, when I use the density statement with the group option, a density within each group is computed without taking in consideration the proportion of each subgroup with the whole dataset. How can I "resize" these curves, so that each density reflects its actual size with respect to the whole dataset?
Thanks in advance.
I am not sure I understand the request. But please look at this example and scroll down to the second example, is that what you want? https://documentation.sas.com/?docsetId=grstatproc&docsetTarget=p0o7d7xxvzs9qmn1vctlufpz1448.htm&doc...
Well, probably I still don't understand. Can you explain why the example I mentioned is NOT what you want?
@marghe wrote:
Hello,
I have been having troubles with density plots in sgplot/sgpanel. My dataset has 4 subgroups and, when I use the density statement with the group option, a density within each group is computed without taking in consideration the proportion of each subgroup with the whole dataset. How can I "resize" these curves, so that each density reflects its actual size with respect to the whole dataset?
Thanks in advance.
Please at least show the code you have attempted. Without that we have no clue what options you may already be using (or not using) that might be part of a solution. Post the code in a code box opened with the forum's </> or "running man" icon to preserve formatting.
And it would help a lot to have a set of data to display.
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.
Only include the variables needed to make the plot.
Thanks for your replies, here is my code (it's easier for me to use a sashelp dataset):
data dataset; set sashelp.class; run; /*Age categorization*/ data class; set dataset; if Age<=13 then Age_cat='<=13'; else Age_cat='>13'; run; /*Sex and Age category combined to get a group with 4 levels*/ data class_group; set class; if Sex='M' then do; if Age_cat='<=13' then cat='M - <=13'; else cat='M - >13'; end; else do; if Age_cat='<=13' then cat='F - <=13'; else cat='F - >13'; end; run; proc sort data=class_group; by cat; run; /*Density plot by group (Plot1)*/ title 'Height of boys and girls according to age category'; proc sgplot data=class_group; density height /group=cat type=kernel; xaxis label='HEIGHT'; run; /*From class_group, 5 subjects have cat='F - <=13' and the total dataset has 19 subjects*/ /*In a histogram, frequencies are computed within each subgroup (Plot 2)*/ title 'Height of boys and girls according to age category'; proc sgplot data=class_group(where=(cat='F - <=13')); histogram height /group=cat binwidth=10; xaxis label='HEIGHT' values=(50 to 70 by 10); run;
Plot1
Plot 2
The red lines indicate the bars' heights if frequency of 'F - <=13' was computed on the total dataset (19 subjects) instead of the size of the subgroup (5 subjects).
I would like that in the computation of densities for each group these 19 subjects were used, not only those belonging to that particular subgroup. Do I have to use weights?
Hope this clarifies a bit my question, thanks you all!!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.