BookmarkSubscribeRSS Feed
lueryy2000
Calcite | Level 5
Hi,

Please see the below for my data:
data temp;
input ID $ score section;
datalines;
1 90 1
1 89 1
1 92 1
1 70 2
1 65 2
1 80 2
1 88 3
1 70 3
1 75 3
2 95 1
2 88 1
2 93 1
2 87 2
2 83 2
2 70 2
***more data;
;
run;

I want to get the plot of score by section for each ID, that is,one page for each ID, and draw a line to label the mean. I have many many IDs so I used a macro to do it. Here is my code:

%macro test(id);

data temp2;
set temp;
if ID="&id";
run;

PROC MEANS DATA=temp2 NOPRINT N MEAN STD;
VAR score;
CLASS section;
OUTPUT OUT=summary N=TOT MEAN=mean_score STD=std_score;
RUN;

data summary2;
set summary;
if section='' then delete;
run;

proc sort data=summary2;
by section;
run;

DATA _NULL_;
SET summary2;
CALL SYMPUT('a'!!TRIM(LEFT(_N_)),mean_score);
RUN;

DATA anno_data;
%SYSTEM(2,2);
%ANNOMAC;
%LINE(0.7,&a1,1.3,&a1,BLACK,1,4);
%LINE(1.7,&a2,2.3,&a2,BLACK,1,4);
%LINE(2.7,&a3,3.3,&a3,BLACK,1,4);
RUN;

AXIS1 ORDER=(0 TO 4 BY 1);
PROC GPLOT DATA=temp2 ANNO=anno_data;
PLOT score*section / haxis=axis1;
RUN;
QUIT;

%mend;

%test(1);
%test(2);

The first plot (ID=1) is correct. For ID=2, the data doesn't have section 3, but sas still draw a line at y=77.6, I guess this is the mean for a3 when ID=1, so it seemed it passed the value of last run. I don't understand how this happen. Thanks in advance.

Lu
2 REPLIES 2
GraphGuy
Meteorite | Level 14
I would recommend using just a simple 'by' statement, rather than going to the trouble of looping through all the id's and calling a macro each time.

I would do it something like this...


data temp;
input ID $ score section;
datalines;
1 90 1
1 89 1
1 92 1
1 70 2
1 65 2
1 80 2
1 88 3
1 70 3
1 75 3
2 95 1
2 88 1
2 93 1
2 87 2
2 83 2
2 70 2
;
run;

proc sql;
create table avg_anno as
select unique id, avg(score) as avg_score
from temp group by id;
quit; run;
data avg_anno; set avg_anno;
length function $8;
xsys='1'; ysys='2'; hsys='3';
function='move'; x=0; y=avg_score; output;
function='draw'; x=100; color='blue'; size=.1; output;
function='label'; position='a'; size=.;
text='avg = '||trim(left(put(avg_score,comma10.1))); output;
run;

goptions gunit=pct htitle=5 htext=3.5;

AXIS1 ORDER=(0 TO 4 BY 1) minor=none offset=(0,0);
axis2 order=(50 to 100 by 10) minor=none offset=(0,0);

symbol value=circle h=2 i=none color=red;

PROC GPLOT DATA=temp;
by id;
PLOT score*section / haxis=axis1 vaxis=axis2 anno=avg_anno;
RUN;
lueryy2000
Calcite | Level 5
Thank you very much. Although it is not what I expected, it is still a good way to do. Thanks again.
Lu

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 826 views
  • 0 likes
  • 2 in conversation