## Sort rows while keeping one fixed

Solved
Occasional Contributor
Posts: 11

# Sort rows while keeping one fixed

Hello everyone,

I was wondering how to first sort by one variable then, keeping that sorting fixed, sort by another variable. For example, first sorting by NET then sorting by AGE as below

``````HAVE
====

Up to 40 obs WORK.HAVE total obs=4

Obs    NAME   AGE  NET  REG  LOSS

1     home   300  200   50     .
2     home   100  300    .     5
3     TDR     80  500    .     3
4     TDR    100  200    5     .

WANT
====``````
``````Obs    NAME   AGE  NET  REG  LOSS
1     TDR    100  200    5     .
2     home   300  200   50     .
3     home   100  300    .     5
4     TDR     80  500    .     3
``````
` `

Jack

Accepted Solutions
Solution
‎03-15-2017 07:40 AM
Super User
Posts: 9,789

## Re: Sort rows while keeping one fixed

That is just a proc sort then, position in the by statement indicates precendance:

```proc sort data=have out=want;
by net age;
run;```

Sorts the data by age within the sort of data net.

All Replies
Solution
‎03-15-2017 07:40 AM
Super User
Posts: 9,789

## Re: Sort rows while keeping one fixed

That is just a proc sort then, position in the by statement indicates precendance:

```proc sort data=have out=want;
by net age;
run;```

Sorts the data by age within the sort of data net.

Occasional Contributor
Posts: 11

## Re: Sort rows while keeping one fixed

Thank you
Occasional Contributor
Posts: 6

## Re: Sort rows while keeping one fixed

Hi,

If we need to sort any variable within another variable (nested sorting) ,we could get this done with the help of PROC SORT.
We can use the By variable and get the task done.

Example:

Data try;
Input NAME\$ AGE NET REG LOSS;
Datalines;
home 300 200 50 .
home 100 300 . 5
TDR 80 500 . 3
TDR 100 200 5 .
;
run;

Proc sort data=try out=try2;by net age;
run;

Now the variable NET is sorted first within which the variable Age is sorted.

Thanks ,
GP

Occasional Contributor
Posts: 11

## Re: Sort rows while keeping one fixed

Thank you )
☑ This topic is solved.