- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What logic is that (obs 4) moved to be at (obs 2).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi.Art. I understand your code.
It is for OP. I do not understand which obs4 moves to be at which obs2.
Ksharp
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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"
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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