## Swaping observations

Solved
Frequent Contributor
Posts: 79

# Swaping observations

Hi Experts, Is there any way to swap two or more observations within a dataset like I want to swap 15th observation with 83rd. Please help. Regards

Accepted Solutions
Solution
‎08-11-2016 12:22 AM
Super User
Posts: 6,785

## Re: Swaping observations

You would need to re-create the data set.  And it's clumsy.  This would be one way.

data want;

do until (done1);

set have (obs=14) end=done1;

output;

end;

set have (firstobs=83 obs=83);

output;

do until (done2);

set have (firstobs=16 obs=82) end=done2;

output;

end;

set have (firstobs=15 obs=15);

output;

do until (done3);

set have (firstobs=84) end=done3;

output;

end;

run;

This would be easier (less clumsy), but probably takes longer to run:

data want;

do _n_=1 to 14, 83, 16 to 82, 15, 84 to _nobs_;

set have nobs=_nobs_ point=_n_;

output;

end;

stop;

run;

The code is untested, but should be OK.

All Replies
Super Contributor
Posts: 271

## Re: Swaping observations

You may have to write a macro to swap row.

Super User
Posts: 9,599

## Re: Swaping observations

Thats not logical.  Why the 15th and why the 83rd?  You can do it by using the _n_ automatic variable but I would really advise against it as any change to that data (sorting, logic etc.) would mess up that change.  You would be better off looking at why you select the 15th and 83rd rows and then use that selection criteria to pull out the rows you want and swap them over.  Also bear in mind that there are sort procedures and such like that sort the data based on key variables, not position in the dataset.  You may be thinking in Excel terms, don't, SAS datasets are logically ordered and manipulated.

Solution
‎08-11-2016 12:22 AM
Super User
Posts: 6,785

## Re: Swaping observations

You would need to re-create the data set.  And it's clumsy.  This would be one way.

data want;

do until (done1);

set have (obs=14) end=done1;

output;

end;

set have (firstobs=83 obs=83);

output;

do until (done2);

set have (firstobs=16 obs=82) end=done2;

output;

end;

set have (firstobs=15 obs=15);

output;

do until (done3);

set have (firstobs=84) end=done3;

output;

end;

run;

This would be easier (less clumsy), but probably takes longer to run:

data want;

do _n_=1 to 14, 83, 16 to 82, 15, 84 to _nobs_;

set have nobs=_nobs_ point=_n_;

output;

end;

stop;

run;

The code is untested, but should be OK.

Frequent Contributor
Posts: 79

## Re: Swaping observations

hi

could you please explain a bit how POINT options is working here??

-Rahul

Super Contributor
Posts: 271

## Re: Swaping observations

Point options make set statement to read data from observation number mentioned with point = .

Super User
Posts: 10,784

## Re: Swaping observations

```Why not make a index variable N and proc sort it ?

data have;
do i=1 to 100;
output;
end;
run;

data want;
set have;
n=_n_;
if n=15 then n=84;
else if n=84 then n=15;
run;
proc sort data=want;by n;run;

```
Super Contributor
Posts: 271

## Re: Swaping observations

@Ksharp Simple and very effective.

☑ This topic is solved.