BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
camper
Fluorite | Level 6

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

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

PaigeMiller_0-1715196634946.png

--
Paige Miller
camper
Fluorite | Level 6
Thank you for replying! The 'error' was not in the log. Better stated: Mistakes in my coding was causing each plot to have the same R2 as each other. Thank you!
Ksharp
Super User

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;
camper
Fluorite | Level 6
Thank you so much!! The output is exactly what I was trying to do! Many, many thanks.
Ksharp
Super User

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;

Ksharp_0-1744185336190.png

 

camper
Fluorite | Level 6
This is a great and clean solution, but your other solution was perfect. Thank you so very much for taking the time to help me!
camper
Fluorite | Level 6
Upon reflection... I think the charts with the most info will be terrific. So thanks for offering 2 solutions!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1473 views
  • 1 like
  • 3 in conversation