hello,
I am trying to display several regression plots, using 9.4. Each plot needs to have the regression line and r2 value. An error in my code shows that each plot has the same R2. Eventually I want to save/export all of the plots in a pdf. I don't want to display the chart data that my code is generating. I have found several examples, online, and this is the shortest code, so I'm submitting it to you. I'm new to ods and macros, so I'm not sure how to trouble shoot this code. I've attached some sample code, for 2 stations. I have 132 stations in the full dataset. Thank you!!
proc sort data=testing; by station_nm; run;
ods graphics off;
proc reg data=testing ;
model q_CFS = EstQ;
by station_nm;
ods output fitstatistics=R2;
run;
data _null_;
set R2;
if _n_ = 1 then call symput('R2', cValue2);
run;
proc sgplot data=testing noautolegend ;
by station_nm;
reg y=q_CFS x=EstQ;
inset "R2 = &R2"/ position=topleft;
run;
I think you need to make a macro to get job done.
proc import datafile='C:\Users\xiakeshan\Documents\Downloads\Testing.csv' out=testing dbms=csv replace;
run;
proc sort data=testing; by station_nm; run;
proc freq data=testing noprint;
table station_nm/out=station_nm;
run;
%macro reg_plot(station_nm=);
ods select none;
proc reg data=testing ;
where station_nm="&station_nm.";
model q_CFS = EstQ/rsquare ;
ods output fitstatistics=R2;
run;
ods select all;
data _null_;
set R2;
if _n_=1 then call symputx('R2', cValue2);
run;
proc sgplot data=testing noautolegend ;
where station_nm="&station_nm.";
reg y=q_CFS x=EstQ;
inset "R2 = &R2"/ position=topleft;
run;
%mend;
ods pdf file='c:\temp\want.pdf' style=htmlblue;
data _null_;
set station_nm;
call execute(catt('%nrstr(%reg_plot)(station_nm=',station_nm,')'));
run;
ods pdf close;
Is your question about the error or about getting charts into PDF, or both?
If it is about the error, you need to show us the log with the error (we need to see the entire log, not just the error messages). Copy the log as text (not screen capture) and then paste the log into the window that appears when you click on the </> icon
I think you need to make a macro to get job done.
proc import datafile='C:\Users\xiakeshan\Documents\Downloads\Testing.csv' out=testing dbms=csv replace;
run;
proc sort data=testing; by station_nm; run;
proc freq data=testing noprint;
table station_nm/out=station_nm;
run;
%macro reg_plot(station_nm=);
ods select none;
proc reg data=testing ;
where station_nm="&station_nm.";
model q_CFS = EstQ/rsquare ;
ods output fitstatistics=R2;
run;
ods select all;
data _null_;
set R2;
if _n_=1 then call symputx('R2', cValue2);
run;
proc sgplot data=testing noautolegend ;
where station_nm="&station_nm.";
reg y=q_CFS x=EstQ;
inset "R2 = &R2"/ position=topleft;
run;
%mend;
ods pdf file='c:\temp\want.pdf' style=htmlblue;
data _null_;
set station_nm;
call execute(catt('%nrstr(%reg_plot)(station_nm=',station_nm,')'));
run;
ods pdf close;
BTW, Can not you use the default graph of PROC REG?
proc import datafile='C:\Users\xiakeshan\Documents\Downloads\Testing.csv' out=testing dbms=csv replace; run; proc sort data=testing; by station_nm; run; ods pdf file='c:\temp\want.pdf' style=htmlblue; ods select FitPlot; proc reg data=testing ; by station_nm; model q_CFS = EstQ/rsquare ; run; ods pdf close;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.