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
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;
I think the easiest way is to transpose the data around your year variable (or Jahr 😉 ) 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
Thank you!
Almost there - however: all lines are plotted into one single diagram!
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;
Many thanks!
Dear @PeterClemmensen thank you for answering, it helped me a lot. In case I want to make simple bar graphs to all vars in the dataset (for example suicides and homicides), in separate plots, but not by year (just simple count or percentage bar graph), how could I do it? I tried to use a mock year variable with a constant value, and I almost got it, but only the first value and case in each variable are presented. For example, the below graph is for variable "FASE"; this variable has two values, 1 and 2, but only 1 was presented, and the count is one (should be 422):
The code I used was:
proc transpose data=merge0 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;
hbar Value;
by group;
run;
Thank you very much for your attention.
Best wishes
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
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 ...
@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')
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.)
@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.
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.)
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.