BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rahul_SAS
Quartz | Level 8
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
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
RahulG
Barite | Level 11

You may have to write a macro to swap row.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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.

Rahul_SAS
Quartz | Level 8

hi 

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

 

-Rahul

RahulG
Barite | Level 11

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

Ksharp
Super User
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;

RahulG
Barite | Level 11

@Ksharp Simple and very effective.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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