BookmarkSubscribeRSS Feed
AmitKB
Fluorite | Level 6

Hi all,

           I have 2 variables, which capture similiar information but their distributions are different. I want to plot their distribution on same axis. The distribution could be a histogram.

          Thanks for help.

Amit

    output wanted:

Output.png

Sample Data:

IndexVar1Var2
10.74691.025641
20.37970.474684
30.46030.854430
40.56700.974729
50.51900.622837
60.59410.693069
70.43480.869565
80.65481.014085
90.31750.476190
100.41670.555556
9 REPLIES 9
Jay54
Meteorite | Level 14

TwoDist.png


data TwoVar;
  input Index Var1 Var2;
  datalines;
1  0.7469  1.025641
2  0.3797  0.474684
3  0.4603  0.854430
4  0.5670  0.974729
5  0.5190  0.622837
6  0.5941  0.693069
7  0.4348  0.869565
8  0.6548  1.014085
9  0.3175  0.476190
10 0.4167  0.555556
;
run;

ods graphics / reset width=5in height=3in imagename='TwoDist';

title 'Comparative Density';

proc sgplot data=TwoVar;

  density var1 / legendlabel='Var1';

  density var2  / legendlabel='Var2';

  keylegend / across=1 location=inside position=topright;

  xaxis display=(nolabel);

  run;


AmitKB
Fluorite | Level 6

Hi Sanjay,

                 Thanks for your reply. I have  a follow up question.

                     Which option made the two density on same axis?

                    Also could you do a histogram on same axis?

Regards,

Amit

Jay54
Meteorite | Level 14

The axis are a union of the data automatically.  You don't have to specify anything extra. Smiley Happy

Yes, you can have histograms.  Just replace the "density" statements with the "histogram" statements.

You can have histograms AND density plots at the same time.  You just have to a bit more to control the transparencies, colors and legends.

See below.

TwoDistHist.png

title 'Comparative Density';

proc sgplot data=TwoVar nocycleattrs;

  histogram var1 / legendlabel='Var1' fillattrs=graphdata1  transparency=0.3;

  density var1  / legendlabel='Var1'  lineattrs=graphdata1(pattern=solid) name='1';

  histogram var2 / legendlabel='Var2' fillattrs=graphdata2  transparency=0.3;

  density var2  / legendlabel='Var2' lineattrs=graphdata2(pattern=solid) name='2';

  keylegend '1' '2' / across=1 location=inside position=topright;

  xaxis display=(nolabel);

  run;

Haikuo
Onyx | Level 15

Hi Sanjay@SAS,

Great demonstration! I am wondering if this can be easily done using EG? I am new to EG and have played with it aiming at this target with no avail.

Thanks in advance!

Haikuo

Jay54
Meteorite | Level 14

Yes, you can write proc SGPLOT code programs in EG.

Haikuo
Onyx | Level 15

Hi Sanjay,

Sorry I did not make myself clear at the first place. I am curious if the same can be done using menu-driven method in EG. If possible, how?

Thanks,

Haikuo

Jay54
Meteorite | Level 14

May be easier if you post the question to the EG track.

esraa
Fluorite | Level 6

What if I need to statistically compare these two curves or find the area under them. Could you please advise with this 

Rick_SAS
SAS Super FREQ

For additional options, see Overlay density estimates on a plot - The DO Loop

Also, notice that the default SGPLOT behavior is to use separate bin widths for each variable, which is often not what you want. In 9.3, you can control this behavior from SGPLOT. If you are still using 9.2, see this post to set the bin widths to be equal: Overlaying two histograms in SAS - The DO Loop

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 6011 views
  • 0 likes
  • 5 in conversation