Data visualization with SAS programming

Plotting with a Special X variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

Plotting with a Special X variable

Hi,

Can some one please let me know how to plot Var(y variable) in the attached data set Vs. Week ( X variable)?

Please Note that 1997-1 refers to the 1st week of 1997, 1997- 40 refers to the 40th week of 1997.

Because of this special format of the x variable a normal sas code cannot be youed.

Thanks in advance.

Attachment

Accepted Solutions
Solution
‎12-08-2012 02:13 PM
SAS Super FREQ
Posts: 8,642

Re: Plotting with a Special X variable


Hi:

  What kind of plot do you want? Scatter plot, line plot, dot plot, series plot? What code have you tried? SAS/GRAPH and PROC GPLOT or ODS GRAPHICS and PROC SGPLOT? Can you show the code that you have tried?


  Is your WEEK variable character or numeric? You have data that represents a lot of years, do you want every YEAR to be a separate scatter symbol or a separate LINE color? If so, you will need to have a YEAR variable in your data.


  Is this question related to this other question? https://communities.sas.com/thread/39336 You had some suggestions in that posting, that may help you with this question.

  For example, in the image created in code #1, only 20-21 values for X are shown for 1 year. With more than 1 year or more weeks on image #1, the X axis would become unreadable. On the other hand, with code example #2, since a separate YEAR variable is used as a GROUP= variable, then you can make multiple plot lines, one for each year. (If you changed to BY year (code #3) or used SGPANEL (code #4), you would get different output.)

cynthia

 

data dateval;

  infile datalines;

  input week $ var;

  year = input(scan(week,1,'-'),4.);

  weeknum = input(scan(week,2,'-'),2.);

return;

datalines;

1997-1 0

1997-2 0.003142039

1997-3 0.011184462

1997-4 0.003932907

1997-5 0.004176446

1997-6 0.003766596

1997-7 0.002643895

1997-8 0.00960544

1997-9 0.001378906

1997-10 0.001248404

1997-11 0.000216714

1997-12 0.000714136

1997-13 0.000786933

1997-14 0.001784908

1997-15 0.000475066

1997-16 7.03E-05

1997-17 0.001656748

1997-18 0.000707953

1997-19 0.000308805

1997-20 0.000579109

1997-21 0.000353416

1998-1 0

1998-2 0.001424841

1998-3 0.000622539

1998-4 0.000155405

1998-5 0.000239838

1998-6 0.000744065

1998-7 0.000467641

1998-8 0.000132295

1998-9 0.000348177

1998-10 0.000383507

1998-11 0.00042303

1998-12 0.00010362

1998-13 0.000473917

1998-14 0.000566873

1998-15 0.00090204

1998-16 0.000530124

1998-17 0.000418762

1998-18 0.000514736

1998-19 0.001233775

1998-20 0.000619137

1998-21 0.000462966

;

run;

    

ods html path='c:\temp' (url=none) file='diff_examp.html';

proc sgplot data=dateval;

where scan(week,1,'-') = '1997';

title '1) Using 1 var for X and no group';

title2 'for partial weeks in 1997';

series x=week y=var;

xaxis fitpolicy=staggerthin;

run;

      

proc sgplot data=dateval;

title '2) Using Separate Numeric Variables for X and GROUP var';

series x=weeknum y=var/group=year;

xaxis values=(0 to 21 by 1) fitpolicy=stagger;

run;

  

proc sort data=dateval;

  by year weeknum;

run;

       

proc sgplot data=dateval;

title '3) Using BY group processing';

by year;

series x=weeknum y=var;

xaxis values=(0 to 21 by 1) fitpolicy=stagger;

run;

   

proc sgpanel data=dateval;

title '4) Using SGPANEL and PANELBY';

panelby year;

series x=weeknum y=var;

colaxis values=(0 to 21 by 1) fitpolicy=thin;

run;

ods html close;

View solution in original post


All Replies
Respected Advisor
Posts: 4,606

Re: Plotting with a Special X variable

What is the definition of week 1 in a given year? The first week containing a given weekday, the first complete week, the first week containing at least 4 days? - PG

PG
Frequent Contributor
Posts: 91

Re: Plotting with a Special X variable

Actually I had the attached data set. I calculated the weekly variances using excel. I got the week number from the date using WEEKNUM in excel.

So, weeks like 1st week did not have all five days in the weekly variance calculation.

Thanks.

Attachment
Solution
‎12-08-2012 02:13 PM
SAS Super FREQ
Posts: 8,642

Re: Plotting with a Special X variable


Hi:

  What kind of plot do you want? Scatter plot, line plot, dot plot, series plot? What code have you tried? SAS/GRAPH and PROC GPLOT or ODS GRAPHICS and PROC SGPLOT? Can you show the code that you have tried?


  Is your WEEK variable character or numeric? You have data that represents a lot of years, do you want every YEAR to be a separate scatter symbol or a separate LINE color? If so, you will need to have a YEAR variable in your data.


  Is this question related to this other question? https://communities.sas.com/thread/39336 You had some suggestions in that posting, that may help you with this question.

  For example, in the image created in code #1, only 20-21 values for X are shown for 1 year. With more than 1 year or more weeks on image #1, the X axis would become unreadable. On the other hand, with code example #2, since a separate YEAR variable is used as a GROUP= variable, then you can make multiple plot lines, one for each year. (If you changed to BY year (code #3) or used SGPANEL (code #4), you would get different output.)

cynthia

 

data dateval;

  infile datalines;

  input week $ var;

  year = input(scan(week,1,'-'),4.);

  weeknum = input(scan(week,2,'-'),2.);

return;

datalines;

1997-1 0

1997-2 0.003142039

1997-3 0.011184462

1997-4 0.003932907

1997-5 0.004176446

1997-6 0.003766596

1997-7 0.002643895

1997-8 0.00960544

1997-9 0.001378906

1997-10 0.001248404

1997-11 0.000216714

1997-12 0.000714136

1997-13 0.000786933

1997-14 0.001784908

1997-15 0.000475066

1997-16 7.03E-05

1997-17 0.001656748

1997-18 0.000707953

1997-19 0.000308805

1997-20 0.000579109

1997-21 0.000353416

1998-1 0

1998-2 0.001424841

1998-3 0.000622539

1998-4 0.000155405

1998-5 0.000239838

1998-6 0.000744065

1998-7 0.000467641

1998-8 0.000132295

1998-9 0.000348177

1998-10 0.000383507

1998-11 0.00042303

1998-12 0.00010362

1998-13 0.000473917

1998-14 0.000566873

1998-15 0.00090204

1998-16 0.000530124

1998-17 0.000418762

1998-18 0.000514736

1998-19 0.001233775

1998-20 0.000619137

1998-21 0.000462966

;

run;

    

ods html path='c:\temp' (url=none) file='diff_examp.html';

proc sgplot data=dateval;

where scan(week,1,'-') = '1997';

title '1) Using 1 var for X and no group';

title2 'for partial weeks in 1997';

series x=week y=var;

xaxis fitpolicy=staggerthin;

run;

      

proc sgplot data=dateval;

title '2) Using Separate Numeric Variables for X and GROUP var';

series x=weeknum y=var/group=year;

xaxis values=(0 to 21 by 1) fitpolicy=stagger;

run;

  

proc sort data=dateval;

  by year weeknum;

run;

       

proc sgplot data=dateval;

title '3) Using BY group processing';

by year;

series x=weeknum y=var;

xaxis values=(0 to 21 by 1) fitpolicy=stagger;

run;

   

proc sgpanel data=dateval;

title '4) Using SGPANEL and PANELBY';

panelby year;

series x=weeknum y=var;

colaxis values=(0 to 21 by 1) fitpolicy=thin;

run;

ods html close;

Frequent Contributor
Posts: 91

Re: Plotting with a Special X variable

Hi,

I need a line graph and all the values in the week column should be in the x axis of the graph not just the year 97..

I have posted this question before and had difficulties when I tried it.

Thanks.

SAS Super FREQ
Posts: 8,642

Re: Plotting with a Special X variable

Hi:

  Did you try any of the code that's been posted? And, if so, what error messages did you get?

   If you run the code below on the dataset that I created in my original response to your post, you will get output, as shown in the screenshot. Eventually, with ALL years and ALL weeks, the X axis will become unreadable. This image is for 21 weeks in 97 and 21 weeks in 98 and the X axis is already crowded ... much more data and you couldn't even read the X axis.

  When you try to present data with so many data points on the X axis -- and I have only 42 for 2 years -- by my calculation, you will have 9 years of data (from your CSV file) for 1997-2005, with at least 52 weeks per year, which will make at least 9*52=468+ (probably more since some years have 53 weeks) tick marks on the X axis. That is simply too many data points to be meaningful. In my opinion, you may need to rethink your method of presenting this data.

cynthia

ods html path='c:\temp' (url=none) file='x_axis_alt.html';
proc sgplot data=dateval;
  title '5) Using all data for X';
  title2 'X axis will become unreadable';
  series x=week y=var;
run;

  

proc sgplot data=dateval;
  title '6) Using REFLINE';
  title2 'suppress all the ticks and use REFLINE';
  series x=week y=var;
  refline '1997-1' / axis=x label='1997';
  refline '1997-5' / axis=x label='Wk5';
  refline '1997-10' / axis=x label= 'Wk10';
  refline '1997-15' / axis=x label= 'Wk15';
  refline '1998-1' / axis=x label='1998';
  refline '1998-5' / axis=x label='Wk5';
  refline '1998-10' / axis=x label= 'Wk10';
  refline '1998-15' / axis=x label= 'Wk15';
  xaxis display=none;
run;
ods html close;

Attachment
Frequent Contributor
Posts: 91

Re: Plotting with a Special X variable

Cynthia,

When I used your code I get the following errors. I am using SAS 9.0. Is it be cause of that. yes, I understand what you say about the x axis becoming unreadable. So, I am planning to present it for each year.

Thank you ALL for your valuable time and inputs.

870

871

872

873  ods html path="F:\data\test.csv" (url=none) file='diff_examp.html';

NOTE: Writing HTML Body file: diff_examp.html

ERROR: Physical file does not exist, F:\data\test.csv\diff_examp.html.

WARNING: No body file.  HTML output will not be created.

874

875  proc sgplot data=dateval;

ERROR: Procedure SGPLOT not found.

876

SAS Super FREQ
Posts: 8,642

Re: Plotting with a Special X variable

Hi:

  Well, you have 2 problems. #1 applies for both SAS/GRAPH and ODS GRAPHICS; but #2 is based on your SAS version:

1) PATH= is pointing to a .CSV file and it should point to a high-level directory. PATH= is supposed to indicate the folder where your output (graph and HTML file) will be stored. So you probably want:

ods html path="F:\data" (url=none) file='diff_examp.html';

I made some FAKE data in my program. I called the FAKE data file WORK.DATEVAL -- if you are testing my program, then you would need to run my program to make WORK.DATEVAL before you run my code. Or, alternately, if you want to try my program on your data, then you will need to use one of the other examples to read YOUR CSV file into a SAS dataset and then change the name in the DATA= option accordingly. But, no matter what you do PATH= is meant to be a FOLDER directory.

2) Your biggest challenge is that if you are using SAS 9.0, then SGPLOT was not production or available until SAS 9.2. When and if you do get the ODS invocation working, you will probably get an error message using PROC SGPLOT. So you would have to use SAS/GRAPH and PROC GPLOT, instead. I believe that someone has already posted some examples of PROC GPLOT for you in a different post based on this same question.

cynthia

Frequent Contributor
Posts: 91

Re: Plotting with a Special X variable

Cynthian,

Thank you very much. That's a really nice explanation.

Respected Advisor
Posts: 4,606

Re: Plotting with a Special X variable

It can be quite hard to emulate function WEEKNUM from EXCEL with SAS formats. But it might not be very useful to do so anyway. The following represents the dates appropriately as the Wednesday of the week.

data wtest(keep=wDate Var);
infile "&sasforum.\datasets\wtest.csv" dsd firstobs=2;
input txtdate $ Var;
year = input(scan(txtdate,1),4.0);
week = input(scan(txtdate,2),2.0);
wDate = intnx("WEEK.4", mdy(1,1,year), week-1);
format wDate date9.;
run;

proc sgplot data=wTest;
series y=var x=wDate;
xaxis type=time label="Date";
run;

PG

PG
SAS Employee
Posts: 963

Re: Plotting with a Special X variable

Here's another technique that assigns a sequential numeric variable (x_num) to all the x-values, and then plots by that value (to keep the x-axis in the proper order) - I generate a user-defined-format from the data (xfmt.) and I apply that user-defined format so that the x-values "print" on the graph with the character date value.  I use "xpixels=6000" to make the plot *really* wide, so the 477 x-values are spread out enough that they don't overlap.

data wtest;

infile "Wtest.csv" dsd firstobs=2;

input txtdate $ Var;

run;

data wtest; set wtest;

x_num=_n_;

run;

proc sql;

create table foo as

select unique x_num as start, txtdate as label

from wtest;

quit; run;

data control; set foo;

fmtname = 'xfmt';

type = 'N';

end = START;

run;

proc format lib=work cntlin=control;

run;

axis1 order=(0 to .2 by .025) minor=none;

axis2 order=(0 to 477 by 1) value=(angle=90);

symbol1 value=dot interpol=join;

goptions xpixels=6000 ypixels=500;

proc gplot data=wtest;

format x_num xfmt.;

plot var*x_num=1 / vaxis=axis1 haxis=axis2;

run;

Post a Question
Discussion Stats
  • 10 replies
  • 543 views
  • 8 likes
  • 4 in conversation