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

I have following data1(abc) which are representing coordinates-

data abc;
input index Cor1 Cor2 Cor3 Cor4 Cor5 Cor6 Cor7 Cor8;
datalines;
1 1 2 4 12 6 10 5 11
2 1 1.5 4 11.5 6.5 10.5 5 13
3 1.5 2 4 12 6 10 5 11
;

and data2(abc1) which represents classes w.r.t abc(data)

data abc1;
input class COORDINATE$;
datalines;
0 Cor1
0 Cor2
1 Cor3
2 Cor4
1 Cor5
2 Cor6 
1 Cor7
2 Cor8
;

For following code can we able to do it automatic, without writing series statement each time?

proc sgplot data=abc;
series X=index y=Cor1/lineattrs=(color=g);
series X=index y=Cor2/lineattrs=(color=g);
series X=index y=Cor3/lineattrs=(color=b);
series X=index y=Cor5/lineattrs=(color=b);
series X=index y=Cor7/lineattrs=(color=b);
series X=index y=Cor4/lineattrs=(color=r);
series X=index y=Cor6/lineattrs=(color=r);
series X=index y=Cor8/lineattrs=(color=r);
run;

if yes how? 

Note:-

1. Maximum(class)=3

2. If classes increase it should be increase in colors for line.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Where do you get the color mapping:

 

0 = g

1 = b

2 = r

 

That's an important piece of the logic that you have in your head but is not in the data.

 

Assuming you could extend that list if you needed to, here is a way to automate the process:

 

data _null_;

call execute ('proc sgplot data=abc;');

do until (done);

   set abc1 end=done;

   y + 1;

   call execute ('series x=index y=Cor ' || put(y, 1.);

   call execute('/lineattrs=(color=');

   if class=0 then call execute('g');

   else if class=1 then call execute('b');

   else if class=2 then call execute('r');

   call execute ( ');' ) ;

end;

call execute('run;');

stop;

run;

View solution in original post

11 REPLIES 11
Reeza
Super User

For following code can we able to do it automatic, without writing series statement each time?

Not with your current data structure. Change it so that CORR is one value and then the correlation ID (1 to 😎 is in another column:

 

Index ID Corr_Value

1 1 2

1 2 4

1 3 12

....

3 1 1.5

....

3 8 11

;

 

Then when you make your graph you can use the GROUP = to get separate lines:

 

proc sgplot data=abc;
series x=index y=corr_value / group = ID;

run;

You can control the colours with your separate data set, using a data attribute map.

 


@K_coder wrote:

I have following data1(abc) which are representing coordinates-

data abc;
input index Cor1 Cor2 Cor3 Cor4 Cor5 Cor6 Cor7 Cor8;
datalines;
1 1 2 4 12 6 10 5 11
2 1 1.5 4 11.5 6.5 10.5 5 13
3 1.5 2 4 12 6 10 5 11
;

and data2(abc1) which represents classes w.r.t abc(data)

data abc1;
input class COORDINATE$;
datalines;
0 Cor1
0 Cor2
1 Cor3
2 Cor4
1 Cor5
2 Cor6 
1 Cor7
2 Cor8
;

For following code can we able to do it automatic, without writing series statement each time?

proc sgplot data=abc;
series X=index y=Cor1/lineattrs=(color=g);
series X=index y=Cor2/lineattrs=(color=g);
series X=index y=Cor3/lineattrs=(color=b);
series X=index y=Cor5/lineattrs=(color=b);
series X=index y=Cor7/lineattrs=(color=b);
series X=index y=Cor4/lineattrs=(color=r);
series X=index y=Cor6/lineattrs=(color=r);
series X=index y=Cor8/lineattrs=(color=r);
run;

if yes how? 

Note:-

1. Maximum(class)=3

2. If classes increase it should be increase in colors for line.


 

K_coder
Fluorite | Level 6

How to create such data transformation?

K_coder
Fluorite | Level 6

Now how to join abc1 to abc12?

proc transpose data=abc out=abc12;
by index;
run;

proc print data=abc12;
run;

To get required transformation...

 

 

Reeza
Super User

@K_coder wrote:

Now how to join abc1 to abc12?

proc transpose data=abc out=abc12;
by index;
run;

proc print data=abc12;
run;

To get required transformation...

 

 


Why do you need to join it? 

The data attribute map should take care of the colour groupings, which is what you were after, doesn't it?

Astounding
PROC Star

Where do you get the color mapping:

 

0 = g

1 = b

2 = r

 

That's an important piece of the logic that you have in your head but is not in the data.

 

Assuming you could extend that list if you needed to, here is a way to automate the process:

 

data _null_;

call execute ('proc sgplot data=abc;');

do until (done);

   set abc1 end=done;

   y + 1;

   call execute ('series x=index y=Cor ' || put(y, 1.);

   call execute('/lineattrs=(color=');

   if class=0 then call execute('g');

   else if class=1 then call execute('b');

   else if class=2 then call execute('r');

   call execute ( ');' ) ;

end;

call execute('run;');

stop;

run;

K_coder
Fluorite | Level 6

Getting error at put when Cor increases

call execute ('series x=index y=Cor ' || put(y, 1.);

After Cor9 and above Error Message-

 + series x=date y=Cor*
                                    _
                                    22
                                    200
 ERROR 22-322: Syntax error, expecting one of the following: ;, /.  
 ERROR 200-322: The symbol is not recognized and will be ignored.

 

Astounding
PROC Star
Sorry my fault.

Line 105 needs a closing parenthesis before the final semicolon.
K_coder
Fluorite | Level 6

consider following condition

data abc;
input index Abc Pqr Xyz A C D E xyz1;
datalines;
1 1 2 4 12 6 10 5 11
2 1 1.5 4 11.5 6.5 10.5 5 13
3 1.5 2 4 12 6 10 5 11
;

data abc1;
input class COORDINATE$;
datalines;
0 Abc
0 Pqr
1 Xyz
2 A
1 C
2 D 
1 E
2 xyz1
;

In above case what should we do?

We have to rename the column names or have another option...

 

 

K_coder
Fluorite | Level 6

Got the solution-

data _NULL_;
call execute('proc sgplot data=abc;');
do until (done);
set abc1;
length s1 $100;
s1 = catx(' ','series x=index y=',COORDINATE);
call execute(s1);
call execute('/lineattrs=(color=');
if class=0 then call execute('g');
else if class=1 then call execute('b');
else if class=2 then call execute('y');
else if class=3 then call execute('r');
call execute ( ');' ) ;
end;
call execute('run;');
run;

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 11 replies
  • 2832 views
  • 4 likes
  • 3 in conversation