Graphics Programming

Data visualization using SAS programming, including ODS Graphics and SAS/GRAPH. Charts, plots, maps, and more!
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
capam
Pyrite | Level 9

I am using the code below to generate a series of plots with sgplot in a loop. The process doesn't generate any plots. 

 

data _null_;

	array veh_no[2] _temporary_ (3725:3726)
;
	call execute('proc sql;');

	do i=1 to dim(veh_no) while (veh_no(i) ne .);

		call execute(cats('%nrstr(%rept_plot3xxx)(',veh_no(i),')'));
	end;

	call execute('quit;');
run;

%macro rept_plot3xxx(num);
proc sgplot data=DWM_Min_list_3xxx_psi;
  title "&num scatter plot";
  yaxis label= "psi";
  scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
  scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;
  where VEHICLE_NO = '&num' and
  DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and
  DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25
;
run;
%mend rept_plot3xxx;
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Submit the following and then post your full log back please if it doesn't work. Note I replaced the single quotes with double quotes to allow the macro variable to resolve. It's a good practice to put your WHERE at the top of your PROC then its clear that your data is filtered somehwo.

 

options MPRINT SYMBOLGEN MLOGIC;

%macro rept_plot3xxx(num); title "&num scatter plot";
proc sgplot data=DWM_Min_list_3xxx_psi;  where VEHICLE_NO = "&num" and DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25 ;
  yaxis label= "psi";
  scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
  scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;

run;
%mend rept_plot3xxx;

%rept_plot3xxx (3725) ;

View solution in original post

10 REPLIES 10
Reeza
Super User

@capam wrote:

I am using the code below to generate a series of plots with sgplot in a loop. The process doesn't generate any plots. 

 


Why not?

WarrenKuhfeld
Ammonite | Level 13

You need to call the macro after defining it.

ballardw
Super User

Since your data _null_ step comes before the macro definition then when it executed you may not have had the macro to call. What does your LOG look like? I suspect, at least the first time, the you may have had messages about "Apparent invocation of macro rep_plot3xxx not resolved".

 

Or verify that you have values in this range in the data set DWM_Min_list_3xxx_psi:

 

where VEHICLE_NO = '&num' and
  DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and
  DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25

 

Does anything happen with

%rep_plot3xxx (3725) ?

 

And what is that quit statement doing? Sgplot doesn't require one.

 

capam
Pyrite | Level 9

Ballardw,

 

Thanks for the quick reply. I tried the following with no plot output. 

 

%macro rept_plot3xxx(num);
proc sgplot data=DWM_Min_list_3xxx_psi;
  title "&num scatter plot";
  yaxis label= "psi";
  scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
  scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;
  where VEHICLE_NO = '&num' and
  DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and
  DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25
;
run;
%mend rept_plot3xxx;

%rept_plot3xxx (3725) ;
Reeza
Super User

Submit the following and then post your full log back please if it doesn't work. Note I replaced the single quotes with double quotes to allow the macro variable to resolve. It's a good practice to put your WHERE at the top of your PROC then its clear that your data is filtered somehwo.

 

options MPRINT SYMBOLGEN MLOGIC;

%macro rept_plot3xxx(num); title "&num scatter plot";
proc sgplot data=DWM_Min_list_3xxx_psi;  where VEHICLE_NO = "&num" and DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25 ;
  yaxis label= "psi";
  scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
  scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;

run;
%mend rept_plot3xxx;

%rept_plot3xxx (3725) ;
capam
Pyrite | Level 9
Thank you Reeza. This worked right away and I was able to easily automate it.

capam
DanH_sas
SAS Super FREQ

I think the problem is in your WHERE clause. You have '&num', which will not resolve the macro variable. You need to use double quotes there, i.e. "&num".

 

Hope this helps!

Dan

capam
Pyrite | Level 9

Running the following code produces the plots..

 

proc sgplot data=DWM_Min_list_3xxx_psi;
	title "3725 scatter plot";
	yaxis label= "psi";
	scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
	scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;
	where VEHICLE_NO = '3725' and
		DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and
		DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25
	;
run;
Reeza
Super User

Macro variables DO NOT resolve within single quotes. 

This allows the the processor to not resolve the & when it's needed for text.

 

 

ballardw
Super User
proc sgplot data=DWM_Min_list_3xxx_psi;
  title "&num scatter plot";
  yaxis label= "psi";
  scatter x=OCCUR_DATE y=DWM_TRUCK_1_PRESS_2_FDBK_8451;
  scatter x=OCCUR_DATE y=DWM_TRUCK_2_PRESS_2_FDBK_8453;
  where VEHICLE_NO ="&num" and
  DWM_TRUCK_1_PRESS_2_FDBK_8451 between 0 and 25 and
  DWM_TRUCK_2_PRESS_2_FDBK_8453 between 0 and 25
;
run;You likely do not have values of '&num' in your data. "&num" to resolve the macro variable.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 10 replies
  • 2704 views
  • 0 likes
  • 5 in conversation