Dear. experts
I have data like
ID Sex 1_age 1_height 2_age 2_height 3_age 3_height
A M 10 140 12 144 13 146
B M 20 175 21 176 23 177
C F 15 154 17 160 19 162
D F 18 163 19 165 21 165
E F 12 152 13 153 15 158
.......
I want to make linear plot and scatter plot with x axle: age and y axle: height and I want to put different color by sex .
Thank you.
Your plot is going to much easier to understand and use if you have a single age and height variable with each age on a different observation per ID.
The variable names you show aren't generally valid in SAS either, digits aren't acceptable by default as the first character.
Three different plots using your shown values.
data have; input ID $ Sex $ age_1 height_1 age_2 height_2 age_3 height_3; datalines; A M 10 140 12 144 13 146 B M 20 175 21 176 23 177 C F 15 154 17 160 19 162 D F 18 163 19 165 21 165 E F 12 152 13 153 15 158 ; data toplot; set have; array a(*) Age_1-Age_3; array h(*) Height_1-Height_3; do i=1 to dim(a); age=a[i]; height=h[i]; output; end; keep id sex age height; run; proc sgplot data=toplot; /*simple scatter plot*/ scatter x=age y=height; run; proc sgplot data=toplot; /* scatter plot showing difference by sex*/ scatter x=age y=height/group=sex; run; Proc sgplot data=toplot; /*series grouped by Id to trace individuals*/ series x=age y=height/ group=id; run;
Have a look at the docs of proc sgplot: https://support.sas.com/sassamples/graphgallery/PROC_SGPLOT.html
You will, most likely have to transpose the data into a format suitable for using proc sgplot.
why are there several HEIGHT variables?
Your plot is going to much easier to understand and use if you have a single age and height variable with each age on a different observation per ID.
The variable names you show aren't generally valid in SAS either, digits aren't acceptable by default as the first character.
Three different plots using your shown values.
data have; input ID $ Sex $ age_1 height_1 age_2 height_2 age_3 height_3; datalines; A M 10 140 12 144 13 146 B M 20 175 21 176 23 177 C F 15 154 17 160 19 162 D F 18 163 19 165 21 165 E F 12 152 13 153 15 158 ; data toplot; set have; array a(*) Age_1-Age_3; array h(*) Height_1-Height_3; do i=1 to dim(a); age=a[i]; height=h[i]; output; end; keep id sex age height; run; proc sgplot data=toplot; /*simple scatter plot*/ scatter x=age y=height; run; proc sgplot data=toplot; /* scatter plot showing difference by sex*/ scatter x=age y=height/group=sex; run; Proc sgplot data=toplot; /*series grouped by Id to trace individuals*/ series x=age y=height/ group=id; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.