Hello,
Using SAS 9.4.
I am creating a line graph of the average satisfaction score for a group of people. Is it possible to have the number of people (n) who contributed a score displayed on the line? Meaning I want to see essentially the number of people contributing a score and not the acutal average across the multiple time points. Below is a copy of my code, I know how to put the average at each time point on the line but am not sure how to put the number of respondents or if that is even possible. Thank you
PROC SGPLOT DATA = have;
where event_name = 'A' or 'G' or 'L'
or 'M' or 'N';
SERIES X = event_name Y = AVG_SATISTFACTION / group= failure datalabel = AVG_SATISTFACTION
MARKERS LINEATTRS = (THICKNESS = 2);
TITLE 'Average Satistfaction by Failure';
yaxis label = 'Average Satistfaction Captured';
RUN;
See the article "Label markers in graphs by using the values of several variables", which shows how to concatenate two values into one string.
You didn't post data, so I don't know what form your data are in, but here is a complete example that computes the means, forms a label variable, and plots the data:
/* compute average mpg_city for each type and origin of vehicles */
proc means data=Sashelp.cars(where=(Type^="Hybrid"));
class Type Origin;
var mpg_city;
output out=MeanOut(where=(not missing(Origin))) mean=;
run;
/* create a new label variable. See
https://blogs.sas.com/content/iml/2015/11/16/label-markers-several-vars.html
*/
data Want;
set MeanOut;
length labl $15; /* NAME holds 8 characters. Allow for delimiter and age */
labl = "Avg=" || put(MPG_City, 4.1) || ": N=" || put(_FREQ_, 3.); /* convert to character */
run;
proc sgplot data=Want;
series x=Type y=MPG_City / group=Origin markers datalabel=labl
curvelabel curvelabelpos=min;
xaxis grid;
yaxis grid;
run;
See the article "Label markers in graphs by using the values of several variables", which shows how to concatenate two values into one string.
You didn't post data, so I don't know what form your data are in, but here is a complete example that computes the means, forms a label variable, and plots the data:
/* compute average mpg_city for each type and origin of vehicles */
proc means data=Sashelp.cars(where=(Type^="Hybrid"));
class Type Origin;
var mpg_city;
output out=MeanOut(where=(not missing(Origin))) mean=;
run;
/* create a new label variable. See
https://blogs.sas.com/content/iml/2015/11/16/label-markers-several-vars.html
*/
data Want;
set MeanOut;
length labl $15; /* NAME holds 8 characters. Allow for delimiter and age */
labl = "Avg=" || put(MPG_City, 4.1) || ": N=" || put(_FREQ_, 3.); /* convert to character */
run;
proc sgplot data=Want;
series x=Type y=MPG_City / group=Origin markers datalabel=labl
curvelabel curvelabelpos=min;
xaxis grid;
yaxis grid;
run;
Hello @GS2,
I think you would need to create a variable containing the number of respondents in a preliminary step (typically in the same step in which you calculated the average) and then use this variable in the DATALABEL= option -- as has been suggested already by Rick_SAS.
Moreover, you should correct the serious logical error in your WHERE statement, which should read:
where event_name in ('A' 'G' 'L' 'M' 'N');
I would also avoid typos such as "Satistfaction" in titles, labels and variable names.
Thank you for your thoughts. The "logical error" was intentional as their are other time points and I wanted to see across all of the timepoints not just the ones with observations.
I did end up creating a variable prior to the sgplot to work it out.
@GS2 wrote:
The "logical error" was intentional as their are other time points and I wanted to see across all of the timepoints not just the ones with observations.
Just to clarify: Your WHERE condition uses 'G', 'L', etc. as Boolean values and these are interpreted as TRUE because they are not missing. As a result, the condition is always true, which is normally pointed out in the SAS log:
WHERE 1 /* an obviously TRUE WHERE clause */ ;
So, writing where 1; or just omitting/commenting out the WHERE statement would have had the same effect. This is why it seemed strange to me that you wrote a rather complicated condition to achieve that.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.