Dear programmers,
I like to give a character name in a SCATTER PLOT to some points which their position are in a certain area in the plot (with respect to X- and Y-axis).
In the following dataset, I want the points between 3<x<8 and 3<y<8 to have the label D, E, F and G.
data person;
infile datalines delimiter=',';
input name $ X Y;
datalines;
A, 1, 1
B, 2, 2
C, 3, 3
D, 4, 4
E, 5, 5
F, 6, 6
G, 7, 7
H, 8, 8
I, 9, 9
J, 10, 10
K, 11, 11
;
My code is:
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.X, T.Y
FROM WORK.PERSON as T
;
QUIT;
SYMBOL1
INTERPOL=NONE
HEIGHT=3pt
VALUE=CIRCLE
LINE=1
WIDTH=1
CV = _STYLE_
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE
;
Axis2
STYLE=1
WIDTH=1
MINOR=NONE
;
TITLE;
TITLE1 "Scatter Plot";
FOOTNOTE;
FOOTNOTE1 "Generated by the SAS System (&_SASSERVERNAME, &SYSSCPL) on %TRIM(%QSYSFUNC(DATE(), NLDATE20.)) at %TRIM(%SYSFUNC(TIME(), TIMEAMPM12.))";
PROC GPLOT DATA=WORK.SORTTempTableSorted
;
PLOT Y * X /
VAXIS=AXIS1
HAXIS=AXIS2
FRAME ;
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
GOPTIONS RESET = SYMBOL;
How can I modify the code to perform this job?
Best regards
Farshid Owrang
If I understand you correctly, here is a way
data person;
infile datalines delimiter=',';
input name $ X Y;
datalines;
A, 1, 1
B, 2, 2
C, 3, 3
D, 4, 4
E, 5, 5
F, 6, 6
G, 7, 7
H, 8, 8
I, 9, 9
J, 10, 10
K, 11, 11
;
data temp / view=temp;
set person;
if 3<x<8 and 3<y<8 then text=name;
else do; sx=x; sy=y; end;
run;
proc sgplot data=temp;
scatter x=sx y=sy;
text x=x y=y text=text;
run;
Result:
If I understand you correctly, here is a way
data person;
infile datalines delimiter=',';
input name $ X Y;
datalines;
A, 1, 1
B, 2, 2
C, 3, 3
D, 4, 4
E, 5, 5
F, 6, 6
G, 7, 7
H, 8, 8
I, 9, 9
J, 10, 10
K, 11, 11
;
data temp / view=temp;
set person;
if 3<x<8 and 3<y<8 then text=name;
else do; sx=x; sy=y; end;
run;
proc sgplot data=temp;
scatter x=sx y=sy;
text x=x y=y text=text;
run;
Result:
Thank you very much!
Is it possible that I get the letters above the points?
Best regards
Farshid
Like this?
data person;
infile datalines delimiter=',';
input name $ X Y;
datalines;
A, 1, 1
B, 2, 2
C, 3, 3
D, 4, 4
E, 5, 5
F, 6, 6
G, 7, 7
H, 8, 8
I, 9, 9
J, 10, 10
K, 11, 11
;
data temp / view=temp;
set person;
if 3<x<8 and 3<y<8 then text=name;
run;
proc sgplot data=temp;
scatter x=y y=y / datalabel=text datalabelpos=top;
run;
Thank you very much!
Best regards
Farshid
Anytime 🙂
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.