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

The following histogram uses the year labels of observations:

Histogram.png

Can I do something similar to this in SAS? Here is a non-labeled replication using SGPLOT HISTOGRAM.

Example.png

Here I attach the snippet; in addition, I also wonder if the vertical bars can be drawn not centered around tick marks but in between two tick marks.

filename raw url 'https://query1.finance.yahoo.com/v7/finance/download/%5EGSPC?period1=-9999999999&period2=99999999999';

proc import file=raw dbms=csv replace out=raw;
run;

data return;
	set raw;
	year=year(date);
data return;
	set return;
	by year;
	if last.year;
	return=adj_close/lag(adj_close)-1;
run;

ods results=off;
ods listing gpath='!userprofile\desktop';
ods graphics/reset noborder imagename='Example';

proc sgplot;
	where 1926<=year<=2017;
	histogram return/binstart=-.8 binwidth=.1 nbins=17 scale=count;
	xaxis values=(-.8 to .9 by .1) label='Return';
run;

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
/*
I like this picture.
*/
data have;
call streaminit(123);
do year=1926 to 2022;
 return=rand('normal');
 output;
end;
run;
proc sql;
create table have2 as
select year,(return-min(return))/(range(return)+0.5)-0.4 as return
 from have;
quit;
data have3;
 set have2;
 x=round(100*return,10)-5;
run;
proc sort data=have3 out=have4;
by x year;
run;
data have5;
 set have4;
 by x;
 if first.x then y=0;
 y+1;
run;





proc sgplot data=have5;
scatter x=x y=y/markerchar=year markercharattrs=(size=10 ) labelstrip;
xaxis values=(-90 to 90 by 10) min=-90 max=90 valueshint;
yaxis display=none offsetmin=0 offsetmax=0.1 min=1.6 valueshint;
run;

Ksharp_0-1667907334963.png

 

View solution in original post

2 REPLIES 2
ballardw
Super User

Your going to need to look at the histogram to set some limits but see if this gets you close. Since you want to display Year values then the obvious choice is a TEXT plot, which uses an X, Y pair for coordinates and allows some offset position values. So round the Return value to get plot groups, sort, add in a Y value that increments for each year of data.

Caution: the amount of space you use may not allow all the text and you may have to adjust the increment for the Yval.

 

data return2;
	set return;
	by year;
	if last.year;
	return=adj_close/lag(adj_close)-1;
   xplot= round(return,0.1);
run;
proc sort data=return2;
   by xplot year;
run;


data toplot;
   set return2;
   by xplot year;
   retain yval ;
   if first.xplot then yval=0;
   else yval+1;
run;

proc sgplot data=toplot;
   text x=xplot y=yval text=year/ 
             position=topright;
	xaxis values=(-.8 to .9 by .1) label='Return';
   label yval='Year count';
run;
Ksharp
Super User
/*
I like this picture.
*/
data have;
call streaminit(123);
do year=1926 to 2022;
 return=rand('normal');
 output;
end;
run;
proc sql;
create table have2 as
select year,(return-min(return))/(range(return)+0.5)-0.4 as return
 from have;
quit;
data have3;
 set have2;
 x=round(100*return,10)-5;
run;
proc sort data=have3 out=have4;
by x year;
run;
data have5;
 set have4;
 by x;
 if first.x then y=0;
 y+1;
run;





proc sgplot data=have5;
scatter x=x y=y/markerchar=year markercharattrs=(size=10 ) labelstrip;
xaxis values=(-90 to 90 by 10) min=-90 max=90 valueshint;
yaxis display=none offsetmin=0 offsetmax=0.1 min=1.6 valueshint;
run;

Ksharp_0-1667907334963.png

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 2 replies
  • 483 views
  • 3 likes
  • 3 in conversation