How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?
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.
@anonymous_user wrote:
How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?
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.
@anonymous_user wrote:
How do I switch the order of 2 rows (_n_=8 & _n_=9) without compromising the rest of the dataset?
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.
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!
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!
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.