BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?

 

dimpz429_0-1632844673068.png

proc sort data=exsrc2 out=exsrc3;
	by STUDYID USUBJID EXTRT EXSTDTC;
run;

data exsrc4;
	EXSEQ=0;
	format &varlist;
	do until (last.USUBJID);
	set exsrc3;
	by USUBJID;
	EXSEQ+1;
	output;
	end;
run;

 

I feel like the solution should be really easy (PROC SQL; update? or a data step & multiple DO loops) but I'm struggling. Please help!! Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@anonymous_user wrote:

How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?

 

dimpz429_0-1632844673068.png

proc sort data=exsrc2 out=exsrc3;
	by STUDYID USUBJID EXTRT EXSTDTC;
run;

data exsrc4;
	EXSEQ=0;
	format &varlist;
	do until (last.USUBJID);
	set exsrc3;
	by USUBJID;
	EXSEQ+1;
	output;
	end;
run;

 

I feel like the solution should be really easy (PROC SQL; update? or a data step & multiple DO loops) but I'm struggling. Please help!! Thank you.


One way using Firstobs and Obs= dataset options:

data example;
   do row= 1 to 15;
   output;
   end;
run;

data want;
  set example (firstobs=1 obs=7)
      example (firstobs=9 obs=9)
      example (firstobs=8 obs=8)
      example (firstobs=10)
  ;
run;

Firstobs sets the observation number (row or _n_ value) as the first read, obs= sets the observation number to end getting data from the data set. With no Obs set the default value (max) means the remainder of the data is read.

Note the OBS= is the observation number, not the number of records to read. So when Firstobs=Obs value then one record is read.

 

View solution in original post

4 REPLIES 4
ballardw
Super User

@anonymous_user wrote:

How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?

 

dimpz429_0-1632844673068.png

proc sort data=exsrc2 out=exsrc3;
	by STUDYID USUBJID EXTRT EXSTDTC;
run;

data exsrc4;
	EXSEQ=0;
	format &varlist;
	do until (last.USUBJID);
	set exsrc3;
	by USUBJID;
	EXSEQ+1;
	output;
	end;
run;

 

I feel like the solution should be really easy (PROC SQL; update? or a data step & multiple DO loops) but I'm struggling. Please help!! Thank you.


One way using Firstobs and Obs= dataset options:

data example;
   do row= 1 to 15;
   output;
   end;
run;

data want;
  set example (firstobs=1 obs=7)
      example (firstobs=9 obs=9)
      example (firstobs=8 obs=8)
      example (firstobs=10)
  ;
run;

Firstobs sets the observation number (row or _n_ value) as the first read, obs= sets the observation number to end getting data from the data set. With no Obs set the default value (max) means the remainder of the data is read.

Note the OBS= is the observation number, not the number of records to read. So when Firstobs=Obs value then one record is read.

 

THANK YOU so much!!! It worked. OBS vs FIRSTOBS is such a beginning programming topic --- I forgot about it.

Tom
Super User Tom
Super User

Why?

From your photograph is looks like there is a variable EXSEQ that is controlling the order with-in the higher order sorting.

So is the real question how to change EXSEQ=1 to 2 and EXSEQ=2 to 1 for that given subject?

Thank you for pointing that out! I followed the metadata instructions for creating the EXSEQ variable, so those programming instructions don't line up with the original dataset. I was trying to recreate the "ex" dataset. That's why I asked how to flip the order of 2 rows distinctly.

 

I appreciate the help highlighting the constraint of the EXSEQ ordering---gah!

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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
  • 4 replies
  • 1140 views
  • 3 likes
  • 3 in conversation