SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 956 views
  • 2 likes
  • 2 in conversation