plotting all variables in dataset against time

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

plotting all variables in dataset against time

[ Edited ]

Hi!

Using SAS 9.4 under enterprise guide 7.1.

I´ve a dataset with the first column "year" and then several dozens of variables - suicides, homicides, average income etc . etc.

I want to plot each of them against the year, wrote a little macro:

 

%macro line_diagram(var);
PROC SORT
    DATA=WORK.DATA(KEEP=Jahr &var)
    OUT=WORK.SORTTempTableSorted1
    ;
    BY Jahr;
RUN;


....

PROC GPLOT DATA = WORK.SORTTempTableSorted1;
PLOT &var * Jahr  /
     VAXIS=AXIS1
    HAXIS=AXIS2

FRAME;
%mend line_diagram;



works when typing the names of the other variables separately -

 

 

%line_diagram(Homicides);


however, this is cumbersome,

I want to feed all the variable names into it but could not adapt the examples in helpthreads to related topics to my task.

KInd regards,

                       Christoph

 

 


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,400

Re: plotting all variables in dataset against time

Posted in reply to christoph8

Ah ok. If you want the lines in separate plots, make sure your data is properly sorted and use By-Group processing like this

 

data have;
input year suicides homicides;
datalines;
1998 100 70
1999 110 80
2000 130 70
2001 120 60
2002 140 50
;

proc transpose data=have out=plotdata;
   by year;
run;

proc datasets lib=work memtype=data nolist;
   modify plotdata;
     attrib _all_ label='';
     rename col1=value _NAME_=group;
run;quit;

proc sort data=plotdata;
   by group;
run;

proc sgplot data=plotdata;
   series x=year y=Value;
   by group;
run;

View solution in original post


All Replies
PROC Star
Posts: 1,400

Re: plotting all variables in dataset against time

Posted in reply to christoph8

I think the easiest way is to transpose the data around your year variable (or Jahr Smiley Wink ) and plot it like this

 

data have;
input year suicides homicides;
datalines;
1998 100 70
1999 110 80
2000 130 70
2001 120 60
2002 140 50
;

proc transpose data=have out=plotdata;
   by year;
run;

data plotdata;
   set plotdata(rename=(col1=value _NAME_=group));
run;

proc sgplot data=plotdata;
   series x=year y=Value / group=group;
run;

Definitely, dont use a macro for the job 

Occasional Contributor
Posts: 6

Re: plotting all variables in dataset against time

Thank you!
Almost there - however: all lines are plotted into one single diagram!

 

 

Solution
2 weeks ago
PROC Star
Posts: 1,400

Re: plotting all variables in dataset against time

Posted in reply to christoph8

Ah ok. If you want the lines in separate plots, make sure your data is properly sorted and use By-Group processing like this

 

data have;
input year suicides homicides;
datalines;
1998 100 70
1999 110 80
2000 130 70
2001 120 60
2002 140 50
;

proc transpose data=have out=plotdata;
   by year;
run;

proc datasets lib=work memtype=data nolist;
   modify plotdata;
     attrib _all_ label='';
     rename col1=value _NAME_=group;
run;quit;

proc sort data=plotdata;
   by group;
run;

proc sgplot data=plotdata;
   series x=year y=Value;
   by group;
run;
Occasional Contributor
Posts: 6

Re: plotting all variables in dataset against time

Many thanks!

Super User
Posts: 13,868

Re: plotting all variables in dataset against time

Posted in reply to christoph8

No macro necessary:

 

Proc gplot data=work.data;
 plot (var1 var2 var3 var4)*jahr/
      vaxis=axis1
      haxis=axis2
      frame
 ;
run;
quit;

will produce separate plots for var1* jahr, var2*jahr etc.

 

 

A potential issue is that your vaxis definition may not be appropriate for all of the variables. In which case I might remove the vaxis

Occasional Contributor
Posts: 6

Re: plotting all variables in dataset against time

In your solution: is it possible to read all the variable names out from the data?

O.k. - when I do not need a macro I can of course simply copy & paste them from the original excel sheet -

but there might a be a sligthly less shirt-sleeve way to do it ...

 

 

Super User
Posts: 13,868

Re: plotting all variables in dataset against time

Posted in reply to christoph8

@christoph8 wrote:

In your solution: is it possible to read all the variable names out from the data?

O.k. - when I do not need a macro I can of course simply copy & paste them from the original excel sheet -

but there might a be a sligthly less shirt-sleeve way to do it ...

 

 


proc sql noprint;
   select name into: varlist separated by " "
   from dictionary.columns
   where libname='WORK' and memname='DATA' and 
      upcase(name) ne 'JAHR'
   ;
quit;


plot (&varlist)* jahr 

The name of the library and data set name (memname) are stored by SAS in uppercase so you have to search for them that way. The variable names can be mixed case. Since you seem not to want JAHR as the "time" variable I am excluding that.

 

The proc sql places the variable names into a space delimited list.

The plot statement example shows how to use that list.

If you need to exclude more variable then use something like this:

upcase(name) not in ('JAHR' 'OTHERVAR')

Occasional Contributor
Posts: 6

Re: plotting all variables in dataset against time

 

plot (&valist)*jahr

(as in your example) produces an error (ERROR 180-322: Statement is not valid or it is used out of proper order.),

 

and simply putting a "proc" in front and a semicolon in the end of the line does not help either

(ERROR 22-322: Syntax error, expecting one of the following: ;, DATA, FORMCHAR, HPERCENT, MISSING, NOLEGEND, NOMISS, UNIFORM,
              VPERCENT, VTOH.  
ERROR 76-322: Syntax error, statement will be ignored.)

 

Super User
Posts: 13,868

Re: plotting all variables in dataset against time

Posted in reply to christoph8

@christoph8 wrote:

 

plot (&valist)*jahr

(as in your example) produces an error (ERROR 180-322: Statement is not valid or it is used out of proper order.),

 

and simply putting a "proc" in front and a semicolon in the end of the line does not help either

(ERROR 22-322: Syntax error, expecting one of the following: ;, DATA, FORMCHAR, HPERCENT, MISSING, NOLEGEND, NOMISS, UNIFORM,
              VPERCENT, VTOH.  
ERROR 76-322: Syntax error, statement will be ignored.)

 


 

Show the code and messages from the log. Copy and paste into a code box opened with the forum's {I} menu icon to preserve formatting.

 

We cannot diagnose problems unless the entire problem is presented. We cannot tell from your error message what code you ran. We need both.

 

You did not provide the actual proc gplot code you used so I could not either. I was hoping you would realize the only change needed is to replace YOUR plot statement with the one provided.

 

Note this works to create two plots, the numeric variables in SASHELP.CLASS other than AGE. Age variable is used similar to your Jahr variable.

Note that gplot only plots numeric values with the code provided. So the "and type='num'" part of my code as the data set contains variables that are not numeric and selecting everything except age would generate gplot errors.

 

proc sql noprint;
   select name into: varlist separated by " "
   from dictionary.columns
   where libname='SASHELP' and memname='CLASS' and 
      upcase(name) ne 'AGE' and type='num' 
   ;
quit;

Proc gplot data=sashelp.class;
 plot (&varlist)*age/
      frame
 ;
run;
quit;

I purposely do not include any axis definitions because I have no idea what yours might be.

 

Occasional Contributor
Posts: 6

Re: plotting all variables in dataset against time

[ Edited ]

Indeeds - I omitted the "proc gplot" statement in the code snippet with the misled macro I included into my first question - corrected by now.

However - adapting your example to

 

proc sql noprint;
   select name into: varlist separated by " "
   from dictionary.columns
   where libname='WORK' and memname='DATA' and 
      upcase(name) ne 'JAHR'
   ;
quit;


proc gplot data=DATA; 
plot (&varlist)*Jahr/
frame;
run;
quit;

my original problem is solved (I have only numeric variables in the data set).

Thank you! (Apparently I can mark only one answer as solution.)

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 124 views
  • 0 likes
  • 3 in conversation