I have a dataset with about 10 variables in it that I am creating from an input pipe delimited file. The problem is one of the values is at the bottom of the dataset (obs 4) and I need it to be at (obs 2) for example. Is there a dataset step that I could use find this specific row and move it to the correct obs?
Here is an example to show what I am saying. I would like to move "total 4 2 3" to the second line.
header a b c
5 8 4 8
67 7 4 9
3 11 4 0
total 4 2 3
The code either does what you want or I don't understand what you want. The code keeps the first obs as order=_n_, which would be one. Then, it sets all of the other records values for order to equal _n_+1, or one greater than their current location. Then, finally, it sets the last record to have a value of order equal to 2.
Then, when sorted, you get the order I think that you were looking for.
data have;
input header $ a b c;
cards;
5 8 4 8
67 7 4 9
3 11 4 0
total 4 2 3
;
data want;
set have end=eof;
if _n_ eq 1 then order=_n_;
else order=_n_+1;
if eof then order=2;
run;
proc sort data=want out=want (drop=order);
by order;
run;
proc print data=want;
run;
You could always use a multidimensional array to manipulate a file, but I think that the following would be a lot easier:
data have;
input header $ a b c;
cards;
5 8 4 8
67 7 4 9
3 11 4 0
total 4 2 3
;
data want;
set have end=eof;
order=_n_+1;
if eof then order=1;
run;
proc sort data=want out=want (drop=order);
by order;
run;
What logic is that (obs 4) moved to be at (obs 2).
Was that a question for me or the OP? I took the header to represent obs 1. If that assumption was not correct, one would only have to use something like:
if _n_ eq 1 then order=1;
else order=_n_+1;
if eof then order=2;
However, I'm not sure what the OP really wants.
Hi.Art. I understand your code.
It is for OP. I do not understand which obs4 moves to be at which obs2.
Ksharp
Sorry for any confusion. The line "header a b c" are the variable names only. The four lines below are the values in the dataset under each variable (so there are only 4obs in this example). i appreciate the help, but like Ksharp said I do not understand how this will move the last row which is "obs4" up so that it would be "obs2"
The code either does what you want or I don't understand what you want. The code keeps the first obs as order=_n_, which would be one. Then, it sets all of the other records values for order to equal _n_+1, or one greater than their current location. Then, finally, it sets the last record to have a value of order equal to 2.
Then, when sorted, you get the order I think that you were looking for.
data have;
input header $ a b c;
cards;
5 8 4 8
67 7 4 9
3 11 4 0
total 4 2 3
;
data want;
set have end=eof;
if _n_ eq 1 then order=_n_;
else order=_n_+1;
if eof then order=2;
run;
proc sort data=want out=want (drop=order);
by order;
run;
proc print data=want;
run;
Thank you art297...I used your method to figure it out. At first I didn't quite understand the concept but I got it now.
Do you mean to move the last obs where the second obs is ?
then what about the second obs? keep it or delete it? The following is keeping it.
data have; input header $ a b c; cards; 5 8 4 8 67 7 4 9 3 11 4 0 total 4 2 3 ; run; data want; if _n_ eq 2 then do;set have point=_nobs nobs=_nobs;output;end; set have end=last; if last then stop; output; run;
Ksharp
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.