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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 762 views
  • 3 likes
  • 3 in conversation