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!
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.
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.
Ready to level-up your skills? Choose your own adventure.