BookmarkSubscribeRSS Feed
Voytek
Calcite | Level 5

Hello,

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,

Voytek

4 REPLIES 4
GraphGuy
Meteorite | Level 14

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

Voytek
Calcite | Level 5

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.

Thanks again!

GraphGuy
Meteorite | Level 14

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.

%macro do_plot(sex);

data temp_data; set sashelp.class(where=(sex="&sex"));

run;

%local obscnt;

proc sql noprint;

select count(*) into :obscnt from temp_data;

quit; run;

%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;

run;

%end;

%else %do;

title "There was no data for &sex (obscnt = &obscnt)";

proc gslide;

run;

%end;

%mend;

data loop;

input sex $1;

datalines;

M

F

O

;

run;

data _null_; set loop;

call execute('%nrstr(%do_plot)('|| sex ||');');

run;

Voytek
Calcite | Level 5

Thank you again!

I'll try to apply it to my problem next week and will let you know how how it goes Smiley Happy

Thanks again!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 2988 views
  • 0 likes
  • 2 in conversation