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;
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.
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?
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.