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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

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.

ChrisNZ
Tourmaline | Level 20

why are there several HEIGHT variables?

YS12
Calcite | Level 5
it is follow up data. So you can see that their age and height are increased.
ballardw
Super User

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: 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 16. 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
  • 4 replies
  • 675 views
  • 1 like
  • 4 in conversation