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

Hi member,

I am trying to plot a curve to analyze trend during the years 2005 to 2008 for a categorical variable. I have three datasets for years 2005,2006 and 2008 respectively. So first i combined them to create the super set. I want to plot variables (year vs died) to observe mortality rate. But the values the variable died has are 0,1 and missing data. With gchart i was able to get a block view but i want to plot as a curve or bar for a particular value say died=1 for all three years ?  Please can you help me

proc gchart data=data-name;

block year/group=died discrete subgroup=year;

run;

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
SAS Super FREQ

Hi:

  I am not sure what you mean by not being able to use the SUMVAR option. Usually, with PROC GPLOT, you might need to "pre-summarize" your data using PROC FREQ or PROC MEANS. I would not put your DIED variable on the Y axis. I would put DIED as the value of the "group"...so I'd have one plot line for DIED=1 and a second line in a different color for DIED=0.

  I would probably use PROC FREQ to get a variable called COUNT for each YEAR/DIED value and then have a plot with one line for each unique value of DIED. (I would probably drop the missing.) I made some FAKE data using SASHELP.HEART, which already has a STATUS variable for ALIVE or DEAD, which is similar to your DIED variable. It always helps to have code that people can run. Since SASHELP.HEART was added to the SASHELP data in SAS 9.2, you should be able to run the code below.

cynthia

ods _all_ close;

** make some fake data with more obs for;
** 2006 and 2008, so line is interesting;
** STATUS var is character values are Alive or Dead;
data heart (keep=year status);
  set sashelp.heart;
  year = 2005;
  output;
  year = 2006;
  output; output;
  year = 2008;
  output; output; output;
run;
   
** summarize data for graph procedure;
proc freq data=heart;
tables year*status/list out=work.frq;
run;
   
ods listing;
proc print data=work.frq;
title 'What does Data Look Like?';
run;

** now use output from PROC FREQ in plot procedures;
  
options nodate number pageno=1;
   
** create report with page for every example;
ods listing close;
   
ods pdf file='c:\temp\plot_statuss.pdf' nogtitle notoc;
title 'SAS/GRAPH GPLOT';
** SAS/GRAPH example;
symbol1 interpol=join  value=dot ;
axis2 label=("Count" )
      order=(0 to 10000 by 1000);
proc gplot data=work.frq;
plot count*year=status / vaxis=axis2;
run;
quit;
   
** ODS GRAPHICS example;
title 'ODS GRAPHICS SGPLOT';
proc sgplot data=work.frq;
  series x=year y=count/group=status datalabel markers;
  xaxis values=(2005 to 2008 by 1);
  yaxis values=( 0 to 10000 by 1000);
run;
   
title 'PROC SGPANEL';
proc sgpanel data=work.frq;
  panelby status;
  series x=year y=count/datalabel markers;
  colaxis values=(2005 to 2008 by 1);
  rowaxis values=( 0 to 10000 by 1000);
run;
ods pdf close;
title;

View solution in original post

4 REPLIES 4
Cynthia_sas
SAS Super FREQ

Hi:

  Have you looked at PROC GPLOT instead of PROC GCHART? For example, this documentation example shows how to plot 3 variables:

SAS/GRAPH(R) 9.4: Reference, Second Edition (Example 8: Plotting Three Variables). The results are show on the documentation page.

cynthia

shanthab
Calcite | Level 5

Hi Cynthia,

Thank you for your reply. I did try using plot. But in my case since the x and y axis variables have discrete data values (died as 0 and 1) and (year as 2005,2006 and 2008) only, the graph will build upon based on _FREQ_ value, so i am unable to use plot statement with sumvar option. Please correct me if my understanding is wrong.

Thanks,

Shantha B

Cynthia_sas
SAS Super FREQ

Hi:

  I am not sure what you mean by not being able to use the SUMVAR option. Usually, with PROC GPLOT, you might need to "pre-summarize" your data using PROC FREQ or PROC MEANS. I would not put your DIED variable on the Y axis. I would put DIED as the value of the "group"...so I'd have one plot line for DIED=1 and a second line in a different color for DIED=0.

  I would probably use PROC FREQ to get a variable called COUNT for each YEAR/DIED value and then have a plot with one line for each unique value of DIED. (I would probably drop the missing.) I made some FAKE data using SASHELP.HEART, which already has a STATUS variable for ALIVE or DEAD, which is similar to your DIED variable. It always helps to have code that people can run. Since SASHELP.HEART was added to the SASHELP data in SAS 9.2, you should be able to run the code below.

cynthia

ods _all_ close;

** make some fake data with more obs for;
** 2006 and 2008, so line is interesting;
** STATUS var is character values are Alive or Dead;
data heart (keep=year status);
  set sashelp.heart;
  year = 2005;
  output;
  year = 2006;
  output; output;
  year = 2008;
  output; output; output;
run;
   
** summarize data for graph procedure;
proc freq data=heart;
tables year*status/list out=work.frq;
run;
   
ods listing;
proc print data=work.frq;
title 'What does Data Look Like?';
run;

** now use output from PROC FREQ in plot procedures;
  
options nodate number pageno=1;
   
** create report with page for every example;
ods listing close;
   
ods pdf file='c:\temp\plot_statuss.pdf' nogtitle notoc;
title 'SAS/GRAPH GPLOT';
** SAS/GRAPH example;
symbol1 interpol=join  value=dot ;
axis2 label=("Count" )
      order=(0 to 10000 by 1000);
proc gplot data=work.frq;
plot count*year=status / vaxis=axis2;
run;
quit;
   
** ODS GRAPHICS example;
title 'ODS GRAPHICS SGPLOT';
proc sgplot data=work.frq;
  series x=year y=count/group=status datalabel markers;
  xaxis values=(2005 to 2008 by 1);
  yaxis values=( 0 to 10000 by 1000);
run;
   
title 'PROC SGPANEL';
proc sgpanel data=work.frq;
  panelby status;
  series x=year y=count/datalabel markers;
  colaxis values=(2005 to 2008 by 1);
  rowaxis values=( 0 to 10000 by 1000);
run;
ods pdf close;
title;

shanthab
Calcite | Level 5

Hi,

I was missing on the pre-summarized data. So i created the data set with count for each level of variable 'died' and then used that as input to sgpanel to plot the graph.

**sorting at two levels --> year & died;

proc sort data=work.bcdata out=work.bcdata_year;

by year died;

run;

**pre-summarizing based on 'died';

data work.bc_died;

set work.bcdata_year (keep=died year);

by year died;

if first.died then count=0;

count+1;

if last.died then output;

run;

**ODS graph;

proc sgpanel data=work.bc_died;

panelby died;

series x=year y=count/datalabel markers;

colaxis values=(2005 to 2008 by 1);

rowaxis values=(0 to 20000 by 5000);

run;

Your suggestion of PROC FREQ to eliminate the missing sounds apt. Thanks again!

Shantha B

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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