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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1203 views
  • 0 likes
  • 3 in conversation