I have a question related to GCHART procedure. I use it to generate a set of charts, see fragment of code below:
PROC GCHART DATA =
BY VARIABLE1 VARIABLE2;
The problem is that for some combinations of VARIABLE1 and VARIABLE2 there are no data available (which I am aware of), but it results in the following error(s):
ERROR: There are no valid observations. Could be caused by SUMVAR= or FREQ= variable.
NOTE: The above message was for the following BY group:
VARIABLE1 = X VARIABLE2 = Y
NOTE: The SAS System stopped processing this step because of errors.
In such cases I'd like to display "No data" text instead of a graph or draw an empty graph (but without throwing these errors).
Could you kindly please give me some suggestions how I should handle that problem properly?
Thank you in advance,
Somebody might have a more elegant solution, but here's one possible way ...
Rather than using Gchart's built-in by-processing, select out all the unique 'by' values, and loop through a data step and 'call ececute' a macro for each by-value. In the macro you would create a temporary data-set applying the by-variable(s) in a where-clause, and then you could use macro-logic to see if that data set has obsns in it and if so do the gchart - if the test finds no obsns in it then run Proc Gslide with the desired text in a 'title' statement (you could also have a title2 statement that shows what the by-variables were).
I've got sample code with bits-n-pieces of the above spread out in different examples, but I don't think I have it all in 1 nice/clean/simple example. I might could write up something if you want to go that route...
Thank you very much for your prompt response!
This makes sense to me and I'd really appreciate it if you could provide more details about this solution, such as the sample code you mentioned. I'm still learning programming in SAS and it would be of a great help to me.
This isn't my strongest area of programming expertise, so some of the macro-mavens & experts might provide some other suggestions, but something like the following might do what you're wanting.
This example uses the 'sashelp.class' sample data, which has sex=M and sex=F in it (plus one extra sex=O, so you'll see how this works when there's no matching data). Rather than doing the gchart 'by sex', I create a data set with all the possible sexes, and then use 'data _null_' to loop through all those values and call the do_plot macro. This macro creates a temporary data set containing all the data of the desired sex, and then if there are >0 observations in that temporary data set it creates a bar chart, and if there are 0 observations it creates a gslide with a message instead.
If you're a beginner, this code might be a little challenging, but I've tried to keep it as simple/minimal as possible.
data temp_data; set sashelp.class(where=(sex="&sex"));
proc sql noprint;
select count(*) into :obscnt from temp_data;
%if (&obscnt ne 0) %then %do;
title "Here is a plot of the data for &sex (obscnt = &obscnt)";
proc gchart data=temp_data;
hbar name / type=sum sumvar=weight descending nostats;
title "There was no data for &sex (obscnt = &obscnt)";
input sex $1;
data _null_; set loop;
call execute('%nrstr(%do_plot)('|| sex ||');');
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.