DATA Step, Macro, Functions and more

Swaping observations

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

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: 5,511

Re: Swaping observations

Posted in reply to Rahul_SAS

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.

View solution in original post


All Replies
Super Contributor
Posts: 266

Re: Swaping observations

Posted in reply to Rahul_SAS

You may have to write a macro to swap row.

Super User
Super User
Posts: 7,970

Re: Swaping observations

Posted in reply to Rahul_SAS

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: 5,511

Re: Swaping observations

Posted in reply to Rahul_SAS

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.

Contributor
Posts: 64

Re: Swaping observations

Posted in reply to Astounding

hi 

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

 

-Rahul

Super Contributor
Posts: 266

Re: Swaping observations

Posted in reply to Rahul_SAS

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

Super User
Posts: 10,035

Re: Swaping observations

Posted in reply to Rahul_SAS
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: 266

Re: Swaping observations

@Ksharp Simple and very effective.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 406 views
  • 3 likes
  • 5 in conversation