BookmarkSubscribeRSS Feed
Nupur20
Calcite | Level 5

Hi I have a dataset in sas like this:

ColA      ColB    ColC

Red         6        10

Blue        5         24

Average    4         5

Orange    4         10

Purple     4          11

I have to sort this data by colB in a way that "average" would be the last row like this. If I do this in usual way using "proc sort by colB" procedure, the average remains at the same place.

ColA      ColB    ColC

Red         6        10

Blue        5         24

Orange    4         10

Purple     4          11

Average    4         5

I would appreciate your help and suugestions and regarding the same.

Thanks.

2 REPLIES 2
Linlin
Lapis Lazuli | Level 10

Hi,

I modified my code after I saw Tom's code.  Thank you Tom! I have learned a lot from you.

data have;

input ColA $ : ColB  : ColC;

cards;

Red         6        10

Blue        5         24

Average    4         5

Orange    4         10

Purple     4          11

;

data temp;

  set have;

  if upcase(ColA)='AVERAGE' then group=2;

    else group=1;

proc sort data=temp out=want(drop=group);

by descending colb group;

run;

proc print;run;

                      Obs    ColA       ColB    ColC

                           1     Red          6      10

                           2     Blue         5      24

                           3     Orange       4      10

                           4     Purple       4      11

                           5     Average      4       5

Linlin

Tom
Super User Tom
Super User

You probably want to create another variable to allow the average to be in its own group.

if colA='average' then group=2;

else group=1;

....

proc sort;

   by group colA ....

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1471 views
  • 0 likes
  • 3 in conversation