BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

Sir/Madam,

 

I am trying to plot a graph of trend for longitudinal data such that, at each visit I have the sample size, standard deviation at the point. The attached file  "mycode", contains  is my code the runs with error.  This link  " http://www2.sas.com/proceedings/forum2007/041-2007.pdf"  has the plot the way i want it to loo like with some code that I have tried to adapt. My first plots have sample sizes all over the place reather than at specific visit, there are two extra vertical lines. Please, also let me know is there is a simple way to correct my code.

 

Thank you,

Here is the code that doesnot work well:

 

%let t = 11; /* sample size */
%let NumSamples = 100; /* number of samples */
data Sim;
call streaminit(123);
do SampleID = 1 to &NumSamples; /* ID variable for each sample */
do PD=ranbin(0,1,.5);
do visit = 1 to &t;

JerKAP = rand("Uniform");
output;
end;
end;
end;
run;

 


proc sort data=sim;
by PD visit;
run;
proc summary data=sim;
by PD visit;
var JerKAP;
output out=mean1 (drop = _:) mean=mean n = n stderr = stderr ;
run;
*data mean1;
*set mean 1;
*keep visit PD Jer;
*run;

*symbol1 v=dot i=join;
symbol1 i = j value = W font = marker c = blue h = 0.5 line = 1 width = 1;
symbol2 i = j value = W font = marker c = red h = 0.5 line = 1 width = 1;
*axis1 label=(angle=90 "Jer+B1:B46k (AP) (m^2/s^5)");
*title " Jer+B1:B46k (AP) (m^2/s^5) by visit";
proc plot data=mean1;
plot mean*visit=PD;;
run; quit;


data meanstd;
set mean1;
if PD= 1 then visit = visit + 0.1;
yvalue = mean; output;
yvalue = mean + stderr; output;
yvalue = mean - stderr; output;
run;
symbol1 i = hiloj value = W font = marker c = blue h = 0.5 line = 1 width = 1;
symbol2 i = hiloj value = W font = marker c = red h =0.5 line = 1 width = 1;
proc gplot data = meanstd;
plot yvalue*visit =PD / vref = 110 135 lvref =2 href = 2 4 lhref = 1;
run; quit;


data numlabel;
length function sytle text $ 8;
retain function 'label' xsys ysys '2' hsys '3' style 'swissb' size 2 when 'a';
set mean1;
y = mean;
text = compress(n);
if PD = 0 then do;
color = 'blue'; x = visitnum + 0.1;
end;
else if PD = 1 then do;
color = 'red'; x = visitnum + 0.2;
end;
output;
run;
proc gplot data = meanstd;
plot yvalue*visit=PD/vref = 110 135 lvref = 2
href = 2 4 lhref = 1 annotate = numlabel;
*format armcd $armf.;
run; quit;

data anno1;
length function $8 text $8;
set mean1;
retain xsys ysys '2' hsys '1';
if n > 0 then do;
function = "Label"; x = visit; position = "5"; size = 3.5;
text = put(n, 2.); style = "swissb"; angle = 0;
if PD = 0 then do;
y = 94; color = "blue"; output;
end;
else do;
y = 92; color = "red"; output;
end;
end;
run;

data anno2;
length function $8 text $8 style $6;
retain xsys hsys "1" ysys "2" postion "5" angle 0 size 3.5;
PD = 0; x = 1; y = 94; style = "none"; color = "blue"; text = 'dot';
function = 'symbol'; output;
PD = 0; x = 4; y = 94; style = "swissb"; color = "blue"; text = "n =";
function = 'label'; output;
PD=1; x = 1; y = 92; style = "none"; color = "red"; text = 'dot';
function = 'symbol'; output;
PD=1; x = 4; y = 92; style = "swissb"; color = "red"; text = "n =";
function = 'label'; output;
run;

data anno3;
length function $8 text $8;
retain xsys "3" ysys "2" function "label" size 3 color "gray" position "6" x 90;
y = 135; text = "ULN"; output;
y = 110; text = "LLN"; output;
run;

length xsys ysys hsys when position $1 function $8 style color $20 text $50;
retain xsys "2" ysys "1" hsys "1";
function = "move"; x = 2; y = 0; output;
function = "bar"; color = "grayE8"; style = "solid"; line = 3; when = "b";
x = 4; y = 100; output;
function = "move"; x = 3; y = 2; output;
function = "label"; text = "Treatment Period"; color = "black"; position = "b";
style = "'Arial'"; size = 5; when = "a"; output;
run;

data anno;
set anno4 anno1 anno2 anno3 ;
run;

symbol1 i = hiloctj c = blue line = 1 width = 5;
symbol2 i = hiloctj c = red line = 3 width = 5;
symbol3 i = none value = W font = marker c = blue h = 2.5;
symbol4 i = none value = W font = marker c = red h = 2.5;

axis1 label = (h = 12 a = 90 "Jer+B1:B46k (AP) (m^2/s^5")
minor = none value = (height = 4)
major = (width = 3 c = black)
width = 3 value = (t=1 " ")
order = (0 to 1 by 0.1);
axis2 label = (h = 12 "Visit")
minor = none value = (height = 4)
major = (width = 3 c = black)
value = (t=12 " ") width = 3
order = (1 to 6 by 1)
offset = (6,0);
axis3 label = none
order = (0 to 1 by 0.1)
value = none major = none
minor = none style = 0;
legend1 label = none
position = (top left inside)
offset = (0.5, -0.25)
across = 1
value = none

shape = symbol(8,3.5)
mode = share;
legend2 label = none
position = (top left inside)
offset = (0.5)
across = 1
shape = symbol(8,3.5)
mode = share
value = (h =4 t= 1 "Control" t = 2 "PD");

proc gplot data = meanstd;
plot yvalue*visit=PD /vaxis = axis1 haxis = axis2 legend = legend1
vref = 110 135 lvref = 2
href = 2 4 lhref = 1 annotate = anno;
plot2 mean*visit =PD / vaxis = axis3 haxis = axis2 legend = legend2;
*format armcd $armf.;
run; quit;

 

1 ACCEPTED SOLUTION

Accepted Solutions
SuzanneDorinski
Lapis Lazuli | Level 10

In the code that you shared, you have a set statement that refers to the anno4 data set, but there isn't an anno4 data set.  My guess is that there's a line missing in the code.  Right after the run statement for the anno3 data set is a length statement.  That's where the error is in the log when I run the code. 

 

I tried using PROC SGPLOT instead.

 


%let t = 11;

/* sample size */
%let NumSamples = 100;

/* number of samples */
data Sim;
	call streaminit(123);

	do SampleID=1 to &NumSamples;

		/* ID variable for each sample */
		do PD=ranbin(0, 1, .5);

			do visit=1 to &t;
				JerKAP=rand("Uniform");
				output;
			end;
		end;
	end;
run;

proc sort data=sim;
	by PD visit;
run;

proc summary data=sim;
	by PD visit;
	var JerKAP;
	output out=mean1 (drop=_:) mean=mean n=n stderr=stderr;
run;

* want treatment period displayed as 
* visits 2 through 4.  if we use 4
* as upper value in data step below,
* block is shaded between visits 2 and 
* 5.  so subtract 1 for upper value. ;

data meanstdREVISED;
	set mean1;
	yvalue=mean;
	high=yvalue + stderr;
	low=yvalue - stderr;
	if 2 le visit le 3 then treatment=1;
	else treatment=0;
run;

* have to sort if using block plot! ;

proc sort data=meanstdREVISED;
  by visit;
run;

proc format ;
	value pd_userfmt 0='Control' 1='PD';
	value treat 0=' ' 1='Treatment period';
run;

ods graphics / attrpriority=none;

proc sgplot data=meanstdREVISED;
	block x=visit block=treatment / valueattrs=(weight=bold size=10)
        valuevalign=bottom filltype=alternate
        fillattrs=(color=white) 
        altfillattrs=(color=yellow)
        transparency=.8;
	scatter x=visit y=yvalue / group=pd yerrorlower=low yerrorupper=high 
		markerattrs=(size=12) errorbarattrs=(thickness=3) name='scatter';
	styleattrs datasymbols=(circlefilled diamondfilled) datalinepatterns=(solid 
		shortdash);
	series x=visit y=yvalue / group=pd name='series';
	yaxis values=(0 to 1 by .1);
	xaxis label='Visit' values=(1 to 11 by 1);
	keylegend 'scatter' 'series' / location=inside down=2;
	format pd pd_userfmt. treatment treat. ;
	xaxistable n / class=pd location=outside colorgroup=pd valueattrs=(weight=bold 
		size=10) labelattrs=(weight=bold size=10) title='Sample size at each visit' 
		titleattrs=(weight=bold size=10);
run;

 

This is the output:

 

ODS Graphics version of plot with sample size and standard deviation at each visit.png

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You know the drill by now, attached office files will not be donwloaded, they are a security risk.  Post test data in the form of a datastep, show the code you have used, and the output you want.

desireatem
Pyrite | Level 9

%let t = 11; /* sample size */
%let NumSamples = 100; /* number of samples */
data Sim;
call streaminit(123);
do SampleID = 1 to &NumSamples; /* ID variable for each sample */
do PD=ranbin(0,1,.5);
do visit = 1 to &t;

JerKAP = rand("Uniform");
output;
end;
end;
end;
run;

 


proc sort data=sim;
by PD visit;
run;
proc summary data=sim;
by PD visit;
var JerKAP;
output out=mean1 (drop = _:) mean=mean n = n stderr = stderr ;
run;
*data mean1;
*set mean 1;
*keep visit PD Jer;
*run;

*symbol1 v=dot i=join;
symbol1 i = j value = W font = marker c = blue h = 0.5 line = 1 width = 1;
symbol2 i = j value = W font = marker c = red h = 0.5 line = 1 width = 1;
*axis1 label=(angle=90 "Jer+B1:B46k (AP) (m^2/s^5)");
*title " Jer+B1:B46k (AP) (m^2/s^5) by visit";
proc plot data=mean1;
plot mean*visit=PD;;
run; quit;


data meanstd;
set mean1;
if PD= 1 then visit = visit + 0.1;
yvalue = mean; output;
yvalue = mean + stderr; output;
yvalue = mean - stderr; output;
run;
symbol1 i = hiloj value = W font = marker c = blue h = 0.5 line = 1 width = 1;
symbol2 i = hiloj value = W font = marker c = red h =0.5 line = 1 width = 1;
proc gplot data = meanstd;
plot yvalue*visit =PD / vref = 110 135 lvref =2 href = 2 4 lhref = 1;
run; quit;


data numlabel;
length function sytle text $ 8;
retain function 'label' xsys ysys '2' hsys '3' style 'swissb' size 2 when 'a';
set mean1;
y = mean;
text = compress(n);
if PD = 0 then do;
color = 'blue'; x = visitnum + 0.1;
end;
else if PD = 1 then do;
color = 'red'; x = visitnum + 0.2;
end;
output;
run;
proc gplot data = meanstd;
plot yvalue*visit=PD/vref = 110 135 lvref = 2
href = 2 4 lhref = 1 annotate = numlabel;
*format armcd $armf.;
run; quit;

data anno1;
length function $8 text $8;
set mean1;
retain xsys ysys '2' hsys '1';
if n > 0 then do;
function = "Label"; x = visit; position = "5"; size = 3.5;
text = put(n, 2.); style = "swissb"; angle = 0;
if PD = 0 then do;
y = 94; color = "blue"; output;
end;
else do;
y = 92; color = "red"; output;
end;
end;
run;

data anno2;
length function $8 text $8 style $6;
retain xsys hsys "1" ysys "2" postion "5" angle 0 size 3.5;
PD = 0; x = 1; y = 94; style = "none"; color = "blue"; text = 'dot';
function = 'symbol'; output;
PD = 0; x = 4; y = 94; style = "swissb"; color = "blue"; text = "n =";
function = 'label'; output;
PD=1; x = 1; y = 92; style = "none"; color = "red"; text = 'dot';
function = 'symbol'; output;
PD=1; x = 4; y = 92; style = "swissb"; color = "red"; text = "n =";
function = 'label'; output;
run;

data anno3;
length function $8 text $8;
retain xsys "3" ysys "2" function "label" size 3 color "gray" position "6" x 90;
y = 135; text = "ULN"; output;
y = 110; text = "LLN"; output;
run;

length xsys ysys hsys when position $1 function $8 style color $20 text $50;
retain xsys "2" ysys "1" hsys "1";
function = "move"; x = 2; y = 0; output;
function = "bar"; color = "grayE8"; style = "solid"; line = 3; when = "b";
x = 4; y = 100; output;
function = "move"; x = 3; y = 2; output;
function = "label"; text = "Treatment Period"; color = "black"; position = "b";
style = "'Arial'"; size = 5; when = "a"; output;
run;

data anno;
set anno4 anno1 anno2 anno3 ;
run;

symbol1 i = hiloctj c = blue line = 1 width = 5;
symbol2 i = hiloctj c = red line = 3 width = 5;
symbol3 i = none value = W font = marker c = blue h = 2.5;
symbol4 i = none value = W font = marker c = red h = 2.5;

axis1 label = (h = 12 a = 90 "Jer+B1:B46k (AP) (m^2/s^5")
minor = none value = (height = 4)
major = (width = 3 c = black)
width = 3 value = (t=1 " ")
order = (0 to 1 by 0.1);
axis2 label = (h = 12 "Visit")
minor = none value = (height = 4)
major = (width = 3 c = black)
value = (t=12 " ") width = 3
order = (1 to 6 by 1)
offset = (6,0);
axis3 label = none
order = (0 to 1 by 0.1)
value = none major = none
minor = none style = 0;
legend1 label = none
position = (top left inside)
offset = (0.5, -0.25)
across = 1
value = none

shape = symbol(8,3.5)
mode = share;
legend2 label = none
position = (top left inside)
offset = (0.5)
across = 1
shape = symbol(8,3.5)
mode = share
value = (h =4 t= 1 "Control" t = 2 "PD");

proc gplot data = meanstd;
plot yvalue*visit=PD /vaxis = axis1 haxis = axis2 legend = legend1
vref = 110 135 lvref = 2
href = 2 4 lhref = 1 annotate = anno;
plot2 mean*visit =PD / vaxis = axis3 haxis = axis2 legend = legend2;
*format armcd $armf.;
run; quit;

 

SuzanneDorinski
Lapis Lazuli | Level 10

In the code that you shared, you have a set statement that refers to the anno4 data set, but there isn't an anno4 data set.  My guess is that there's a line missing in the code.  Right after the run statement for the anno3 data set is a length statement.  That's where the error is in the log when I run the code. 

 

I tried using PROC SGPLOT instead.

 


%let t = 11;

/* sample size */
%let NumSamples = 100;

/* number of samples */
data Sim;
	call streaminit(123);

	do SampleID=1 to &NumSamples;

		/* ID variable for each sample */
		do PD=ranbin(0, 1, .5);

			do visit=1 to &t;
				JerKAP=rand("Uniform");
				output;
			end;
		end;
	end;
run;

proc sort data=sim;
	by PD visit;
run;

proc summary data=sim;
	by PD visit;
	var JerKAP;
	output out=mean1 (drop=_:) mean=mean n=n stderr=stderr;
run;

* want treatment period displayed as 
* visits 2 through 4.  if we use 4
* as upper value in data step below,
* block is shaded between visits 2 and 
* 5.  so subtract 1 for upper value. ;

data meanstdREVISED;
	set mean1;
	yvalue=mean;
	high=yvalue + stderr;
	low=yvalue - stderr;
	if 2 le visit le 3 then treatment=1;
	else treatment=0;
run;

* have to sort if using block plot! ;

proc sort data=meanstdREVISED;
  by visit;
run;

proc format ;
	value pd_userfmt 0='Control' 1='PD';
	value treat 0=' ' 1='Treatment period';
run;

ods graphics / attrpriority=none;

proc sgplot data=meanstdREVISED;
	block x=visit block=treatment / valueattrs=(weight=bold size=10)
        valuevalign=bottom filltype=alternate
        fillattrs=(color=white) 
        altfillattrs=(color=yellow)
        transparency=.8;
	scatter x=visit y=yvalue / group=pd yerrorlower=low yerrorupper=high 
		markerattrs=(size=12) errorbarattrs=(thickness=3) name='scatter';
	styleattrs datasymbols=(circlefilled diamondfilled) datalinepatterns=(solid 
		shortdash);
	series x=visit y=yvalue / group=pd name='series';
	yaxis values=(0 to 1 by .1);
	xaxis label='Visit' values=(1 to 11 by 1);
	keylegend 'scatter' 'series' / location=inside down=2;
	format pd pd_userfmt. treatment treat. ;
	xaxistable n / class=pd location=outside colorgroup=pd valueattrs=(weight=bold 
		size=10) labelattrs=(weight=bold size=10) title='Sample size at each visit' 
		titleattrs=(weight=bold size=10);
run;

 

This is the output:

 

ODS Graphics version of plot with sample size and standard deviation at each visit.png

desireatem
Pyrite | Level 9

Thank you Suzzanne, how do I take out the yellow marked "treatment period"?

desireatem
Pyrite | Level 9

I want to delete the treatment period from the code. Once I take out treatment, this is what I get: 

327 ods graphics / attrpriority=none;
328 title" "
329 proc sgplot data=meanstdREVISED;
330 block x=visit / valueattrs=(weight=bold size=10)
-
79
76
ERROR 79-322: Expecting a BLOCK.
ERROR 76-322: Syntax error, statement will be ignored.
331 valuevalign=bottom filltype=alternate
332 fillattrs=(color=white)
333 altfillattrs=(color=yellow)
334 transparency=.8;
335 scatter x=visit y=yvalue / group=pd yerrorlower=low yerrorupper=high
336 markerattrs=(size=12) errorbarattrs=(thickness=3) name='scatter';
337 styleattrs datasymbols=(circlefilled diamondfilled) datalinepatterns=(solid
338 shortdash);
339 series x=visit y=yvalue / group=pd name='series';
340 yaxis values=(0 to 1 by .1);
341 xaxis label='Visit' values=(1 to 11 by 1);
342 keylegend 'scatter' 'series' / location=inside down=2;
343 format pd pd_userfmt. ;
344 xaxistable n / class=pd location=outside colorgroup=pd valueattrs=(weight=bold
345 size=10) labelattrs=(weight=bold size=10) title='Sample size at each visit'
346 titleattrs=(weight=bold size=10);
347 run;

NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE SGPLOT used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds

SuzanneDorinski
Lapis Lazuli | Level 10

The BLOCK statement in PROC SGPLOT is what produced the yellow shading for the treatment period.  If you delete that statement or comment it out, you won't see the treatment period in the graphic.

desireatem
Pyrite | Level 9

Thank You!!!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4534 views
  • 3 likes
  • 3 in conversation