BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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;

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

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;
FreelanceReinh
Jade | Level 19

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.

GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

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.

FreelanceReinh
Jade | Level 19

@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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 851 views
  • 1 like
  • 3 in conversation