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

Hi, I am trying to get the histogram with density overlayed for the counts on Y axis and time on X-axis. However somehow, I am not able to get the density curve close to the histogram. Is density by default take the percent into consideration to plot the curve? Can you please help me on this?

 

xY
1830
2155
365
445
552
635
720
815
910
105
1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi @mahi263 and welcome to the SAS Support Communities!

 

You need to specify the count variable in the FREQ= option of the DENSITY statement, as it wouldn't be used by default.

 

Example:

data have;
input x y;
cards;
 1 830
 2 155
 3  65
 4  45
 5  52
 6  35
 7  20
 8  15
 9  10
10   5
;

proc sgplot data=have;
histogram x / freq=y scale=count binwidth=1;
density x / freq=y type=kernel(c=1.7);
xaxis values=(1 to 10) offsetmin=0.06 offsetmax=0.06;
run;

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hi @mahi263 and welcome to the SAS Support Communities!

 

You need to specify the count variable in the FREQ= option of the DENSITY statement, as it wouldn't be used by default.

 

Example:

data have;
input x y;
cards;
 1 830
 2 155
 3  65
 4  45
 5  52
 6  35
 7  20
 8  15
 9  10
10   5
;

proc sgplot data=have;
histogram x / freq=y scale=count binwidth=1;
density x / freq=y type=kernel(c=1.7);
xaxis values=(1 to 10) offsetmin=0.06 offsetmax=0.06;
run;
mahi263
Calcite | Level 5

Thanks a lot, FreelanceReinh, really helps. Can you please confirm how do we define the kernel (1.7) option? Can you guide on this?

FreelanceReinh
Jade | Level 19

If you try a few different values for that bandwidth, some greater than 1.7, some less than 1.7, you'll get the impression that some are too small (e.g., produce peaks around the integer values 1, 2, 3, ..., as if x were a discrete variable) and some are too large (i.e., deviate considerably from parts of the histogram). That's how I ended up with 1.7. But your knowledge of the data, the subject matter and the scientific literature may suggest a different bandwidth. Or even a different type of density: With a bit more programming effort you can overlay an arbitrary density curve (e.g., exponential, gamma, etc.) on a histogram. See Rick Wicklin's blog post "How to overlay a custom density curve on a histogram in SAS" for details.

mahi263
Calcite | Level 5

Thanks a lot for the resolution. I'll go through the documentation.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 4 replies
  • 411 views
  • 2 likes
  • 2 in conversation