Programming the statistical procedures from SAS

Struggling with sorting in sas.

Reply
Contributor
Posts: 33

Struggling with sorting in sas.

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.

Super Contributor
Posts: 1,636

Re: Struggling with sorting in sas.

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

Super User
Super User
Posts: 6,122

Struggling with sorting in sas.

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

Ask a Question
Discussion stats
  • 2 replies
  • 95 views
  • 0 likes
  • 3 in conversation