Home > Mobile >  how to Sort negative and positive data in SAS
how to Sort negative and positive data in SAS

Time:08-01

I have bellow data in variable NUM

-3 1 0 1 3 2 -2 5 -5 -6 4 6 -4

i want data NUM in bellow sorting order

0 -1 1 -2 2 -3 3 -4 4 -5 5 -6 6

How can we sort negative and positive values together? please help

data have;
input NUM @@;
cards;
-3 1 0 1 3 2 -2 5 -5 -6 4 6 -4
;
run;

CodePudding user response:

Make a new variable with the absolute value and include it in the sort.

data want;
  set have;
  absolute=abs(num);
run;
proc sort data=want;
  by absolute num;
run;

CodePudding user response:

Sort by abs(num), num if you want the negative values to appear before the positive within the same absolute value as in the requested data.

data have;
input NUM @@;
cards;
-3 1 0 -1 3 2 -2 5 -5 -6 4 6 -4
;
run;

proc sql;
   create table want as
   select * from have 
   order by abs(num), num
   ;
quit;
  • Related