Data visualization with SAS programming

SGPlot using Mean

Reply
Frequent Contributor
Posts: 98

SGPlot using Mean

[ Edited ]

Hi!

 

I would like some help creating a graph.  I am basically trying to create three lines, bars

 

1.  Bars show the mean of all ER visits by variable 'patientid' during during each year.

2.  Line1 shows the same (mean) but just with 'married' marital status.

3. Line2 show same but with single marital status.

 

I seemed to get the sum (default) to work ok, but when I change the stat to 'mean' it doesnt work correctly.   All response variables are numeric (1) for event...but I gues I want it to sum the events and then graph the mean.  It is just giving me 'mean' of 1 since all events are coded 1.  Should calulate the mean in a separate data step and then do the plot or can i do that within the proc sgplot?  I guess all the visuals could be lines since the means might be similar.

 

 

 

goptions reset=goptions device=png300;  
ods _all_ close;
ods rtf file='asthma.graph13.rtf';
proc sgplot data=asthma.graphvar;
by patientid;
where Visit_type='ER';
vbar year/ response=visit stat=mean fillattrs=(color=gray);
vline year/ response=Married stat=mean lineattrs=(color=black thickness=10);
vline year/ response=Single stat=mean lineattrs=(pattern=solid color=red thickness=10);
run;
ods rtf close;
ods listing;

Grand Advisor
Posts: 9,719

Re: SGPlot using Mean

You may want to summarize the data so that you have your value and another variable that counts the number of each level of the categorical variable. Then use the Freq= option to tell the procedure to use that value to use the correct number of original observations when calculating the mean. 

Respected Advisor
Posts: 4,606

Re: SGPlot using Mean

Something like

 

goptions reset=goptions device=png300;  
ods _all_ close;
ods rtf file='asthma.graph13.rtf';

proc sql;
create table graphvar as
select 
	year,
	sum(Visit) as Visits,
	sum(Married) as Married,
	sum(Single) as Single
from asthma.graphvar
where visit_type = "ER"
group by year;
quit;

proc sgplot data=graphVar;
vbarparm category=year response=Visits / fillattrs=(color=gray);
series x=year y=Married / lineattrs=(color=black thickness=10);
series x=year y=Single / lineattrs=(pattern=solid color=red thickness=10);
run;
ods rtf close;
ods listing;

maybe?

PG
Respected Advisor
Posts: 4,606

Re: SGPlot using Mean

Or preferably, for simplicity:

 

goptions reset=goptions device=png300;  
ods _all_ close;
ods rtf file='asthma.graph13.rtf';

data graphData;
set asthma.graphvar;
if Married then status="Married";
if Single then status = "Single";
keep year status;
run;

proc sgplot data=graphData;
vbar year / stat=freq group=status groupdisplay=stack;
run;

ods rtf close;
ods listing;
PG
Post a Question
Discussion Stats
  • 3 replies
  • 264 views
  • 0 likes
  • 3 in conversation