Data visualization with SAS programming

scatter plot in a loop

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

scatter plot in a loop

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;

Accepted Solutions
Solution
‎09-15-2017 12:19 PM
Super User
Posts: 19,080

Re: scatter plot in a loop

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


All Replies
Super User
Posts: 19,080

Re: scatter plot in a loop


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?

SAS Super FREQ
Posts: 196

Re: scatter plot in a loop

You need to call the macro after defining it.

Super User
Posts: 11,118

Re: scatter plot in a loop

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.

 

Contributor
Posts: 40

Re: scatter plot in a loop

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) ;
Solution
‎09-15-2017 12:19 PM
Super User
Posts: 19,080

Re: scatter plot in a loop

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) ;
Contributor
Posts: 40

Re: scatter plot in a loop

Thank you Reeza. This worked right away and I was able to easily automate it.

capam
SAS Super FREQ
Posts: 925

Re: scatter plot in a loop

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

Contributor
Posts: 40

Re: scatter plot in a loop

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;
Super User
Posts: 19,080

Re: scatter plot in a loop

Macro variables DO NOT resolve within single quotes. 

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

 

 

Super User
Posts: 11,118

Re: scatter plot in a loop

[ Edited ]
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.

 

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 180 views
  • 0 likes
  • 5 in conversation